If you develop inside containers and keep rebuilding the image or restarting the stack every time you change a line of code, docker compose watch is the built-in fix. You declare a develop.watch block in your Compose file that maps host paths to one of three actions — sync (copy the file into the running container), sync+restart (copy then restart the process), or rebuild (rebuild the image). Run docker compose watch once and Compose watches those paths for you, applying the right action per change. This guide sets it up end to end and shows you how to confirm it is actually working.
What you’ll accomplish
- Add a
develop.watchblock to an existing service so host edits reach the container without a manual rebuild. - Choose the correct action (
sync,sync+restart, orrebuild) for each kind of file. - Exclude
node_modulesand other host-only directories so they never clobber what’s inside the container. - Run
docker compose watchand watch a code change land live. - Recognize the common failures — changes not syncing, rebuild loops, unsupported Compose versions — and fix them.
Prerequisites
- Docker Compose v2.22 or newer. File watch (
docker compose watch) reached general availability in v2.22. Check withdocker compose version— if it prints something older, upgrade Docker Desktop or the Compose plugin before continuing. - Docker Engine with the Compose v2 plugin. Invoke it as
docker compose(a subcommand), not the legacydocker-composePython script, which has nowatch. - A working
compose.yamlthat already builds and runs your app. Watch is a development add-on; it does not replace your build. - Your application should support live reload on its own for the smoothest result (for example nodemon, Vite,
flask run --debug,uvicorn --reload). Watch delivers the file; your process still has to notice it.
How it works
docker compose watch is a foreground command that monitors the host paths you list under a service’s develop.watch key. When a file under a watched path changes, Compose matches it against your rules and runs the action you declared for that rule. The diagram below shows the three outcomes a single saved file can trigger.
The three actions differ in cost and effect:
synccopies the changed file into the running container at thetargetpath. The container is not restarted and the image is not rebuilt. This is the fast path — it is what makes a Vite or nodemon dev server pick up your edit almost instantly.sync+restartcopies the file and then restarts the container’s main process. Use it for changes that a long-running process reads only at boot, such as a server config file or an entrypoint script.rebuildrebuilds the image and recreates the container. Reserve it for changes that alter the image itself — most often a dependency manifest likepackage.json,requirements.txt, orgo.mod, where a plain file copy would leave the installed packages out of date.
How sync differs from a bind mount. A bind mount maps a host directory straight into the container, so the container reads host files directly and inherits host file ownership and any host-only artifacts. sync instead performs a one-way copy from host to container on each change, into an image that was built normally. That means the container keeps the node_modules (or virtualenv) that was installed during the build, and your host’s node_modules never overwrites it. It also works cleanly when the host and container are different platforms, or when the container runs on a remote engine where a bind mount is not possible.
Ignore rules. Each watch rule takes an optional ignore list of glob patterns, evaluated relative to path. Compose also honors your .dockerignore. Ignoring node_modules is the single most important rule for JavaScript projects: without it, a host npm install would sync your host-built modules over the container’s and break the app.
Step-by-step
1. Confirm your Compose version supports watch. Watch is silently absent on older releases, so check first.
docker compose version
Expected output — the version must be v2.22.0 or higher:
Docker Compose version v2.35.1
2. Add a develop.watch block to your service. In your compose.yaml, add a develop key alongside the usual build/image keys. This example is a Node app with a Vite dev server:
services:
web:
build: .
command: npm run dev
ports:
- "5173:5173"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: ./package.json
The first rule syncs anything under host ./src to /app/src in the container. The second rebuilds the image whenever package.json changes.
3. Make sure the container has a live-reload process. sync only copies the file; something inside must react. Confirm your start command runs a watcher — here npm run dev maps to Vite:
docker compose config | grep -A1 command
Expected output shows the dev command Compose will run:
command:
- npm run dev
4. Start the stack, then start watch. Bring the services up first, then run watch in the same or a second terminal.
docker compose up -d
docker compose watch
Expected output — Compose reports which services it is watching and then waits:
[+] Running 1/1
✔ Container myapp-web-1 Started
watch enabled
5. Edit a file under a watched path. Change a source file on the host and save it. Compose logs the sync as it happens:
Syncing service "web" after changes were detected
The dev server inside the container then reloads on its own. Leave docker compose watch running for the whole session; press Ctrl+C to stop watching (your containers keep running).
Validate it works
Prove the round trip end to end. First confirm the file is actually inside the container after a sync:
docker compose exec web cat /app/src/version.txt
Edit ./src/version.txt on the host, save, wait for the Syncing service "web" log line, then run the same command again — the output must reflect your edit:
build-2026-07-14-edited
For a rebuild rule, touch the manifest and watch Compose rebuild rather than sync:
docker compose watch
# in another terminal: echo '' >> package.json
Expected — Compose reports a rebuild, not a sync:
Rebuilding service "web" after changes were detected
[+] Building 4.2s (10/10) FINISHED
Troubleshooting
Changes never sync. The most common cause is a path/target mismatch: the host path you’re editing is not the one in the rule, or target is not where your app reads from. Run docker compose exec web ls /app/src to see what the container actually has, and confirm your edit is under the exact path directory. Also confirm docker compose watch is still running in the foreground — if the process exited, nothing is watching.
node_modules gets clobbered (JS apps). If the app breaks right after a sync with “module not found” errors, your rule is copying the host node_modules over the container’s. Add node_modules/ to that rule’s ignore list and, if you sync the whole project root, prefer syncing ./src rather than .. Restart watch after editing the Compose file.
Rebuild loops or rebuilds on every save. A rebuild rule with too broad a path (for example path: .) fires on every edit, defeating the point. Scope rebuild rules narrowly to the manifest files that truly change the image (package.json, requirements.txt), and use sync for everything else.
watch is not a recognized command. You’re on Compose older than v2.22, or you’re calling the legacy docker-compose script. Run docker compose version (with a space) and upgrade if it prints an older release. There is no watch in the v1 Python client.
Edits sync but the browser doesn’t refresh. Watch did its job; your in-container process isn’t reloading. Confirm the dev server runs with its reload flag, and for file-watching tools that miss events on some filesystems, enable polling (for example Vite’s server.watch.usePolling, or CHOKIDAR_USEPOLLING=true).
For the health and startup ordering that surrounds a dev stack, see Docker Compose depends_on with healthcheck and container healthchecks. The official reference is the Compose file watch documentation and the develop.watch specification.
When this is the wrong approach
docker compose watch is a development tool, full stop. Do not run it against production or staging: it holds a foreground process, copies unbuilt host files into live containers, and has no place in a deployment pipeline where images should be immutable and built once in CI. For a remote or shared host, watch can work against a remote Docker context, but treat that as a convenience for a personal dev box, not a way to push code to servers — use your registry and deploy flow for that. And if your app has no live-reload mechanism of its own, sync will faithfully copy files that the running process simply ignores; add a watcher, or use sync+restart so the process actually re-reads them.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.