Sub topic 14
Faster iteration with Compose Watch
This chapter assumes you can write a compose.yaml, build an image from a Dockerfile, and start a stack with docker compose up. It covers Compose Watch, a feature that updates running containers when you edit files.
The problem it solves
Without Watch, a code change means running docker compose up --build again. For a bind mount, you have to trust the mount and the process inside to pick up edits. Bind mounts work, but they expose your whole host directory to the container. They can also be slow on some platforms.
Watch takes a different approach. You list paths to monitor. When a file changes, Compose either copies it into the container or rebuilds the image.
Configuring watch rules
Rules live under develop.watch in a service.
services:
web:
build: .
develop:
watch:
- action: sync
path: ./src
target: /app/src
- action: rebuild
path: ./requirements.txtEach rule has these fields:
path: the host path to monitor, relative to the project directory.action: what to do when a file underpathchanges.target: the destination inside the container. Required forsyncactions.
The first rule uses sync, which copies changed files from ./src into /app/src in the running container. The second uses rebuild, which rebuilds the image and recreates the container when requirements.txt changes.
Choosing an action
| Action | What happens | Use it for |
|---|---|---|
| `sync` | Copies changed files into the running container | Source files read by a tool that reloads on change |
| `rebuild` | Rebuilds the image, then recreates the container | Dependency lists and the Dockerfile |
Use sync when the process inside reloads by itself, as many web frameworks do in debug mode. A sync does not restart the process. If your program does not reload, the new file sits on disk and nothing changes.
Use rebuild when the change must be baked into the image, such as installing new packages.
Running Watch
Start Watch in either of two ways:
docker compose watchdocker compose up --watchThe first command starts the stack and watches it. The second does the same alongside the normal up behavior, with logs in your terminal. Edit a file under ./src and Compose copies it. Edit requirements.txt and Compose rebuilds.
Watch only works for services that have a build section, because the rules act on files that the image would otherwise contain. A service that uses only image: cannot be rebuilt.
Common errors
- Using
syncfor dependencies. Copyingrequirements.txtinto the container does not install anything. Userebuild. - Wrong
target. Iftargetdoes not match where the Dockerfile placed your code, edits land in a directory the app never reads. - No auto-reload in the app. A synced file only takes effect if the process notices it.
- Watching generated folders. A path such as
node_modulesor build output can trigger constant updates. Narrow thepathso it covers only source files.
Exercise
Exercise
You edit a Python file under ./src and later add a new package to requirements.txt. With the rules shown in this chapter, which action runs for each change, and why?
Show answer
Editing the file under ./src triggers sync, which copies it into /app/src in the running container. Adding a package to requirements.txt triggers rebuild, because the package must be installed in the image, so Compose rebuilds the image and recreates the container.