Compose build versus image
Read the Compose file
services: worker: build: context: . network: none image: bm-v12-worker:lab pull_policy: never user: 10001:10001 network_mode: noneThis service combines build.context and image to build the supplied Dockerfile in the local directory and assign a known tag. The build network setting limits RUN networking; it is not an overall builder isolation guarantee. Compose build performs the explicit build, then up --no-build --pull never uses the local result. When both build and image exist, pull policy and CLI flags influence automatic behavior. A source-file edit does not update the already built image. Avoid implicit pulls and builds in this offline lab; verify the selected builder and preloaded base first.
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. Verify the effective Compose build driver and endpoint for this command, not just the current Buildx selection. Do not pass --pull. Ensure the reviewed base is locally resolvable by that builder. If build fails, never run a preexisting tag.
Open lab-fixtures/docker-compose-fundamentals/06-build-image/ 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.
FROM busybox:1.37CMD ["sh", "-c", "echo built-worker; exec sleep 300"]docker compose -f compose.yaml -p bm-v12-build-image config --quietdocker compose -f compose.yaml -p bm-v12-build-image build workerdocker image inspect bm-v12-worker:labdocker compose -f compose.yaml -p bm-v12-build-image up -d --no-build --pull neverdocker compose -f compose.yaml -p bm-v12-build-image ps -adocker compose -f compose.yaml -p bm-v12-build-image logs --tail 20 workerAfter a successful build, the local bm-v12-worker:lab tag should exist. The running worker should print built-worker. A build is not proof of the runtime behavior; inspect the log separately. Record the built image ID, then compare it with the running worker container image ID after up. A stale local tag is not successful delivery.
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-build-image downdocker compose -f compose.yaml -p bm-v12-build-image ps -adocker image rm bm-v12-worker:labCommon mistake
Do not assume up always rebuilds source changes. Build explicitly or use the documented build workflow, then recreate the service when the image changes.
Manual practice
Change the startup message in a disposable Dockerfile copy. Predict behavior with the old image and after an explicit rebuild and up. Remove only the lab tag after down.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compose build versus image — Knowledge check
Apply the Compose concept to a new scenario.
What does build.context specify?
What does up --no-build --pull never do?
What confirms the built worker actually ran its expected command?