Sub topic 5
Starting watch mode
This chapter covers the two ways to start Compose Watch, what each one does when you stop it, and what to check when watch starts but nothing happens. It assumes you can write a Compose file with build, ports and command, and that you know how images, containers and Dockerfiles relate. It also assumes your Compose version supports develop.watch. Run docker compose version to check. Watch arrived in Compose 2.22, so any older version will not recognize it.
What you need before you start
Watch does nothing until three things are in place.
- The service has a
develop.watchsection with at least one rule. - The service has a
buildsection, so Compose can build an image for it. Watch does not apply to services that only useimage:. - The image layout matches the rule targets. A
syncrule withtarget: /app/srcassumes the Dockerfile puts the code at/app.
Here is the Dockerfile and Compose file used throughout this chapter. The Dockerfile copies the dependency manifest first and runs npm install before copying the rest of the source.
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.jsonThe rule for ./src copies changed files into the running container. The rule for package.json rebuilds the image, because a new dependency is baked into the image layer created by npm install. The next chapters cover the individual actions in detail. This chapter is about getting the process started and keeping it running.
Two ways to start watch
Compose offers two entry points.
# Start the services and watch for changes in one command
docker compose up --watch
# Or start the services first, then run watch separately
docker compose up -d
docker compose watchBoth commands end with the same file-watching process. They differ in what they do around it.
docker compose up --watch
docker compose up --watch builds the images if needed, creates and starts the containers, and then begins watching. It runs in the foreground, and the logs of all services stream to your terminal, mixed with messages from watch itself. This is the form most people use while developing, because one terminal shows everything.
When you press Ctrl+C, Compose stops the containers as it does for any foreground up. Watching ends at the same moment, because it was part of that process.
docker compose watch
docker compose watch is a separate command whose only job is to watch. Use it when the services are already running, for example after docker compose up -d, or when you want to keep service logs apart from watch output. You can read the logs in another terminal with docker compose logs -f web.
Because watch is its own process, stopping it with Ctrl+C ends only the watching. The containers keep running. You can then restart watch later without touching the services.
Recent Compose releases also let docker compose watch build and start the services itself if they are not running. A --no-up flag turns that off. Flags differ between releases, so run docker compose watch --help to see what your installed version supports.
Comparing the two
| Aspect | `up --watch` | `up -d` then `watch` |
|---|---|---|
| Terminals needed | One | One or two |
| Service logs | Streamed with watch messages | Read separately with `logs` |
| Ctrl+C effect | Stops containers and watching | Stops watching only |
| Best for | A quick start in a single terminal | Keeping services up while you restart watch |
What happens when watch starts
Watch reads the develop.watch rules from the Compose file, then uses a file watcher on the host to monitor each path. When a file changes, Compose matches it against the rules and runs the action for the matching rule.
The sequence is this.
I need the diagram to be in the required fenced form. Here it is:
The last branch matters for debugging. A change outside every path, or inside an ignore pattern, produces no output at all. Watch stays silent about files it does not care about.
A first session
Start watch with the Compose file above.
docker compose up --watchOpen http://localhost:3000 and confirm the app responds. Then edit a file under ./src, save it, and watch the terminal. You should see a message from Compose that it synced the file into the container. Whether the page changes by itself depends on the application. For Node.js, that means the process inside the container must reload code on change. A tool such as nodemon or a framework dev server does this. A plain node server.js loads files once at startup, so the new file is in the container but the running process has not read it.
That distinction is one of the most common sources of confusion. Watch's job is to get the file into the container, or rebuild the container. It does not make the application reload. If your process does not reload on its own, use sync+restart, which is covered in a later chapter, or run a development server with a reloader.
services:
web:
build: .
ports:
- "3000:3000"
command: npx nodemon --legacy-watch src/index.js
develop:
watch:
- action: sync
path: ./src
target: /app/src
- action: rebuild
path: package.jsonThis variant uses nodemon, which restarts the Node process when files under it change. It assumes nodemon is installed in the image, for example as a dependency listed in package.json. The --legacy-watch flag makes nodemon poll for changes, which is more reliable on file systems that do not deliver change events.
What a rebuild looks like
Now edit package.json, for example by adding a dependency, and save. Because the rule uses rebuild, Compose runs the equivalent of docker compose build web and then recreates the container with the new image. Docker's layer cache keeps this fast. Since the Dockerfile copies package*.json and runs npm install before COPY . ., a source-only change reuses the cached install layer. A change to package.json invalidates that layer and runs the install again.
You will notice that the rebuild takes noticeably longer than a sync, and that the container is replaced. Any state kept inside the old container's writable layer is gone. Put data you need in a volume, not in the container file system.
Stopping and restarting watch
How you stop watch depends on how you started it.
- With
up --watch, press Ctrl+C. The containers stop. - With
watchas a separate command, press Ctrl+C. The containers keep running, anddocker compose downstops them.
If you edit the Compose file itself, for example to add a rule, watch does not pick up the change. Stop watch and start it again. Rules are read once at startup.
Troubleshooting a start that does nothing
If you start watch and edits have no effect, check these in order.
- The service has no
buildsection. Watch only works with services that Compose builds. If the service usesimage:only, Compose warns that there is nothing to watch for it. - The Compose version is too old. Run
docker compose versionand upgrade if it is below 2.22. - There is no
develop.watchsection. A typo in the key name, or wrong indentation, means the rules are not read. Rundocker compose configand check that thedevelopblock appears under the service. - The
pathdoes not match where you edit. Paths are relative to the Compose file's directory. Editingapp/src/index.jswhen the rule watches./srctriggers nothing. - An
ignorepattern matches the file. Files matched byignoreare skipped without any message. - The
targetdoes not match the image layout. The sync succeeds, but the file lands in a directory the application does not read. Open a shell withdocker compose exec web shand check that the file is where you expect it. - The process does not reload. The file is present in the container, but the application is still running the old code. Use a reloader or
sync+restart.
Summary of the commands
| Command | What it does |
|---|---|
| `docker compose up --watch` | Builds, starts, and watches in the foreground |
| `docker compose up -d` | Starts the services in the background without watching |
| `docker compose watch` | Watches the services; stopping it leaves containers running |
| `docker compose config` | Shows the resolved file so you can check the rules |
| `docker compose logs -f web` | Follows the logs of the `web` service |
Exercise
You run docker compose up -d, then docker compose watch. You press Ctrl+C in the watch terminal. What state are the containers in, and what happens if you instead started with docker compose up --watch and pressed Ctrl+C?
Show answer
In the first case only watching stops. The containers keep running until you run docker compose down. With up --watch, Ctrl+C stops the containers as well, because watching was part of the foreground up process.
Exercise
You edit src/index.js while watch is running with a sync rule for ./src targeting /app/src. docker compose exec web cat /app/src/index.js shows your new code, but the browser still shows the old behavior. The container runs node src/index.js. What is the likely cause, and name two ways to fix it?
Show answer
The file reached the container, but Node loaded the code once at startup and does not reload it. Fix it by running the app under a reloader such as nodemon, or by changing the rule to sync+restart so Compose restarts the service after each sync.
Exercise
Watch starts without errors, but editing files in ./app/src triggers nothing. The Compose file has a rule with path: ./src. What is wrong, and how can you confirm what Compose resolved?
Show answer
The rule watches ./src relative to the Compose file's directory, so edits in ./app/src do not match any rule. Change the path to ./app/src, then stop and restart watch because rules are read at startup. Running docker compose config shows the resolved develop.watch rules.