Compose Watch and file synchronization
Compose Watch automates selected development updates. A sync rule copies changed files from a local path into a running service; it is not itself a language hot-reload engine. A rebuild rule instead triggers a build and replacement when its watched inputs change. The basic Watch feature requires Compose 2.22.0 or later; verify the exact action and options in your installed version. Our local build copies a writable message file into /app and watches only the src directory. The nonroot user needs write permission there and the image needs the documented stat, mkdir and rmdir executables. In an approved lab, keep the watcher foregrounded and change only the supplied file from a second terminal.
Read the development model
services: worker: image: bm-v14-watch:lab pull_policy: never user: 10001:10001 command: - sleep - '300' network_mode: none build: context: . network: none develop: watch: - action: sync path: ./src target: /appFROM 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/03-watch/ in the extracted archive and run from a disposable copy of that folder.
docker compose -f compose.yaml -p bm-v14-watch config --quietdocker compose -f compose.yaml -p bm-v14-watch build workerdocker compose -f compose.yaml -p bm-v14-watch up -d --no-build --pull neverdocker compose -f compose.yaml -p bm-v14-watch exec -T worker sh -c 'command -v stat && command -v mkdir && command -v rmdir && test -w /app'docker compose -f compose.yaml -p bm-v14-watch exec -T worker cat /app/message.txtdocker compose -f compose.yaml -p bm-v14-watch down# Terminal A: start watching; keep this command runningdocker compose -f compose.yaml -p bm-v14-watch up --watch --no-build --pull never# Terminal B: same fixture directory, after Watch reports it is active and worker is runningprintf "edited-v14\n" > src/message.txt# Wait for the sync event (at most 20 seconds), then:docker compose -f compose.yaml -p bm-v14-watch exec -T worker cat /app/message.txtAfter Watch reports synchronization, reading /app/message.txt should give edited-v14. The sleeping worker itself is not a hot-reload demonstration. Stop Watch with Ctrl+C, then clean the project. If unsupported or no sync arrives within 20 seconds, stop and use the read-only mount lesson instead. Verify that the watcher is active before the edit and record the worker ID; a sync-only rule should preserve that ID. Verify the watched source is not ignored by .dockerignore. If the event is missing or the worker was replaced, record the result as inconclusive rather than a successful sync.
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.
# First stop Watch in terminal A with Ctrl+C.docker compose -f compose.yaml -p bm-v14-watch downdocker compose -f compose.yaml -p bm-v14-watch ps -adocker image rm bm-v14-watch:labprintf "first-v14\n" > src/message.txtCommon mistake
Do not watch an entire home directory or secret files. Do not assume sync installs dependencies or automatically restarts a process.
Manual practice
Explain which action you would select for a dependency manifest change versus a text file read on demand.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compose Watch and file synchronization — Knowledge check
Reason about development workflows and their effects.
What does the sync action do in the local Watch fixture?
What must the target allow?
What is an appropriate rebuild trigger?