Sub topic 5
Starting Watch Mode
This chapter assumes you know Docker images, containers and Dockerfiles. It also assumes you can write a basic Compose file with build and ports, and that you are comfortable in a terminal. Here you will start watch mode, check that it is running, and read its output.
What you need before starting
Watch mode only works for services that Compose builds from a build section. A service that uses only image: has no build context, so Compose has nothing to sync into or rebuild. Your service also needs a develop.watch section with at least one rule.
This is the Compose file from the previous chapter, with one sync rule and one rebuild rule:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.jsonThe Dockerfile copies the dependency manifest first, so installing dependencies is cached in its own layer:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]Two ways to start watching
There are two commands. Both give the same result.
# 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 watchWith docker compose up --watch, Compose builds the images, starts the containers, and begins watching. Container logs print in your terminal, so you see the application output and Compose's watch messages together.
With docker compose up -d followed by docker compose watch, the containers run in the background. The watch command then runs in the foreground and watches for changes. Stopping watch with Ctrl+C ends the watching only. The containers keep running.
| Command | Containers | Terminal output | Ctrl+C |
|---|---|---|---|
| `docker compose up --watch` | Started in the foreground | App logs and watch messages | Stops the containers |
| `docker compose up -d` then `docker compose watch` | Already running in the background | Watch messages | Stops watching only |
Choose the second form if you want to stop and restart watching without restarting your application.
What happens after you start
The sequence is the same for both commands:
Compose watches the paths in your rules, relative to the Compose file's directory. Edit a file under ./src and Compose copies it to /app/src in the running container. Edit package.json and Compose rebuilds the image and replaces the container. The Dockerfile layout matters here. Because package*.json is copied before the rest of the source, a rebuild caused by package.json reruns npm install. A rebuild caused by anything else reuses the cached install layer.
Checking that it works
- Run
docker compose up --watchand openhttp://localhost:3000. - Edit a file in
./srcand save it. - Look at the terminal. Compose prints a message when it syncs the file.
- Reload the page, or check the container with
docker compose exec web cat /app/src/<your-file>.
If the file appears in the container but the application does not change, the application itself may not reload on file changes. That is common with servers that read code only at startup. A later chapter covers sync+restart for this case.
Common mistakes when starting
- Watching a service with no
buildsection. Compose has nothing to sync into or rebuild. Addbuildto the service. - Running
docker compose watchwith no running services. Start the services first withdocker compose up -d. - Having no
develop.watchrules. Compose has nothing to watch and reports an error. - Editing a file outside every
path. Only the listed paths are watched.
Exercise
You ran docker compose up -d and then docker compose watch. You press Ctrl+C in the terminal running watch. Are your containers still running, and why?
Show answer
Yes. In this form, up -d started the containers in the background, and watch only monitors files. Ctrl+C stops the watching, not the containers. With docker compose up --watch, Ctrl+C would stop the containers too.
Exercise
A service is defined with only image: node:20-alpine and a develop.watch section. Why will watch mode not work correctly, and what must you add?
Show answer
Watch mode needs Compose to build the service, so it can sync into or rebuild the image. Add a build section, such as build: ., with a Dockerfile that copies in your application.