Services and container instances
Read the Compose file
services: reader: image: busybox:1.37 pull_policy: never user: 10001:10001 network_mode: none command: - sh - -c - echo reader-ready; exec sleep 300 writer: image: busybox:1.37 pull_policy: never user: 10001:10001 network_mode: none command: - sh - -c - echo writer-ready; exec sleep 300The reader and writer services use the same BusyBox image but produce different messages. The top-level services mapping defines each service; image selects its template and command replaces the image default command. Compose normally creates one container per service in this example, but a scalable service can have multiple instances. Target a service by its YAML key in Compose commands rather than guessing generated container names. The list form of command here explicitly invokes sh because it needs shell sequencing. The command override does not remove an inherited ENTRYPOINT. Verify the approved base entrypoint accepts this list; the two services here do not communicate because network_mode is none.
Try it in a local lab
Manual lab only. Use a fresh approved disposable Linux VM with external network access blocked, local Docker Engine and modern Compose plugin. Check docker context show, docker version and docker compose version. Verify local BusyBox 1.37 image digest, native platform, inherited entrypoint, sh, sleep, wget and httpd where required, and writable /tmp for user 10001. Tags are mutable. Confirm every bm-v12 project and image tag is unused; review docker compose ls -a and project resources before mutation. For builds verify a local Buildx docker driver and preloaded base. No pulls, pushes, public ports, host mounts or real credentials. Check command/flag support locally; stop if missing. Run commands individually; unexpected failures mean stop, inspect and perform scoped cleanup. The negative probe is explicitly expected to fail. Dispose of the VM even if cleanup fails. Outputs are predictions, not recorded Docker runs. Before contacting Docker, verify the effective local endpoint including DOCKER_CONTEXT and DOCKER_HOST. Use a clean shell and reviewed fixture directory; check Compose environment overrides, .env, project labels and the resolved configuration. Explicit -f and -p select file and project, not the Engine. Run only on the approved VM; do not adopt existing resources.
Open lab-fixtures/docker-compose-fundamentals/02-services/ in the extracted archive. Work on a disposable copy and run the commands from that directory. The examples use the modern docker compose plugin, not legacy docker-compose.
docker compose -f compose.yaml -p bm-v12-services config --quietdocker compose -f compose.yaml -p bm-v12-services up -d --no-build --pull neverdocker compose -f compose.yaml -p bm-v12-services psdocker compose -f compose.yaml -p bm-v12-services logs --tail 20 readerdocker compose -f compose.yaml -p bm-v12-services logs --tail 20 writerExpect two service rows while both are running. logs reader should select only the reader output. Distinct containers can share an image without sharing a writable layer.
Remove only the project resources you created. No volume deletion, force removal or global prune. Confirm no lab containers remain; the base image is retained.
docker compose -f compose.yaml -p bm-v12-services downdocker compose -f compose.yaml -p bm-v12-services ps -aCommon mistake
Do not treat a service as a container name or ID. A container may be recreated; keep service identity separate.
Manual practice
Compare the two log messages. Predict what happens if writer uses the reader command. Explain why sharing an image does not imply shared process state.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Services and container instances — Knowledge check
Apply the Compose concept to a new scenario.
Can two services use one image?
What identifies the reader service in Compose?
If a container is recreated, which identity can remain stable?