Rebuild and recreate a development service
When source is copied into an image, editing the build context does not update an existing container. Build creates a new image from the revised context; up reconciles the running project with the new image or configuration. Restart alone uses the existing container and does not incorporate a new image. In this example a small image contains /app/message.txt. Read the initial text, edit the context file, build again and use up to adopt the image. In a real project, changes to dependency manifests or system packages also normally require a build.
Read the development model
services: worker: image: bm-v14-rebuild:lab pull_policy: never user: 10001:10001 command: - sleep - '300' network_mode: none build: context: . network: noneFROM busybox:1.37RUN mkdir -p /app && chown 10001:10001 /appCOPY --chown=10001:10001 src/message.txt /app/message.txtUSER 10001:10001CMD ["sleep", "300"]Manual lab
Manual lab only, not browser execution. Use a fresh approved disposable Linux VM with external egress blocked and a local Docker context. Check docker context show, docker version and docker compose version; verify the exact options and fields. Preload and verify a native-platform BusyBox 1.37 image digest, entrypoint and required applets; the tag is mutable. For builds verify a local Buildx docker driver using the reviewed base, with network blocked. Check all bm-v14 project and image names are unused and inherited Compose settings do not interfere. Use only a disposable copy of the supplied synthetic files; confirm UID 10001 can read source files. No public ports, external pulls/pushes, real secrets, broad host mounts or privileged containers. Run each line separately, stop on unexpected failures, clean only the lab and dispose of the VM. All results are predictions. Before Engine requests, verify the effective endpoint including DOCKER_HOST and DOCKER_CONTEXT; a project name does not select the daemon. Review inherited COMPOSE_ENV_FILES, COMPOSE_DISABLE_ENV_FILE, profile and project settings. Use a clean shell and the exact fixture directory. Inspect only synthetic config, logs and files. If a build is required, verify the effective builder can resolve the approved local base without egress; stop if build fails, never reuse a stale tag.
Open lab-fixtures/docker-compose-development/02-rebuild/ in the extracted archive and run from a disposable copy of that folder.
docker compose -f compose.yaml -p bm-v14-rebuild config --quietdocker compose -f compose.yaml -p bm-v14-rebuild build workerdocker compose -f compose.yaml -p bm-v14-rebuild up -d --no-build --pull neverdocker compose -f compose.yaml -p bm-v14-rebuild exec -T worker cat /app/message.txtprintf "edited-v14\n" > src/message.txtdocker compose -f compose.yaml -p bm-v14-rebuild restart workerdocker compose -f compose.yaml -p bm-v14-rebuild exec -T worker cat /app/message.txtdocker compose -f compose.yaml -p bm-v14-rebuild build workerdocker compose -f compose.yaml -p bm-v14-rebuild up -d --no-build --pull never --no-deps workerdocker compose -f compose.yaml -p bm-v14-rebuild exec -T worker cat /app/message.txtprintf "first-v14\n" > src/message.txtBefore rebuilding and reconciling, the running service should still read first-v14. After both steps it should read edited-v14. Inspect only the lab service; do not infer timing or image sizes. Record image ID and worker ID before restart, after restart, after build and after up. Restart should retain the old container and image; a successful changed build changes the image but not the existing container until reconciliation. Do not continue after a failed build.
Clean only the named lab projects; no global prune or force removal. Retain the reviewed base image. A failed cleanup requires VM disposal and review.
docker compose -f compose.yaml -p bm-v14-rebuild downdocker compose -f compose.yaml -p bm-v14-rebuild ps -adocker image rm bm-v14-rebuild:labCommon mistake
Building alone does not replace the running container. Do not remove all containers to update a single service.
Manual practice
Draw the three states: context, image and running container. Predict which state changes at each command.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Rebuild and recreate a development service — Knowledge check
Reason about development workflows and their effects.
Which step incorporates COPY source changes?
What adopts a changed service image?
What does restart alone reuse?