Sub topic 3
Adding develop.watch to a Service
This chapter assumes you can read a Dockerfile, write a Compose file with build and ports, and run commands in a terminal. You will add a develop.watch section to a service so Compose can update the running container when host files change.
What develop.watch does
develop is a section of a Compose service that holds settings for local development. Its watch key holds a list of rules. Each rule tells Compose three things:
- Which host path to monitor (
path). - What to do when a file there changes (
action). - Where the file belongs inside the container (
target), for actions that copy files.
Compose monitors the paths on the host. When something changes, it applies the matching rule to the running container. You no longer rebuild the image and restart the service by hand.
Start with a Dockerfile that caches dependencies
Watch works best when the image is built in a sensible order. This Dockerfile installs dependencies in their own layer. A change to source files does not repeat npm install on rebuild.
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]The COPY package*.json ./ line comes before COPY . .. Docker reuses the cached npm install layer until package.json changes.
A first watch configuration
Here is a service with two rules.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.jsonThe first rule uses sync. When a file under ./src changes, Compose copies it into /app/src in the container. The ignore list excludes node_modules/ from this rule.
The second rule uses rebuild. When package.json changes, Compose rebuilds the image and recreates the container. A new dependency needs a new image, so copying the file would not be enough.
A rebuild rule needs no target, because Compose replaces the whole container.
Requirements for the service
Watch only applies to services that Compose builds. The service needs a build section, as web has above. A service that uses only image: has nothing to rebuild or sync into, so watch rules on it have no effect.
The paths in a rule are relative to the project directory, which is where your Compose file lives.
Starting watch
There are two ways to start it.
# 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 watchUse up --watch when you want logs and watching in one terminal. Use up -d followed by watch when you want the services in the background and the watcher as a separate process.
How a change flows
The diagram shows what happens when you save a file.
A change that matches no rule is ignored. If an edit does not appear in the container, first check that some rule's path covers the file.
Common mistakes
- Missing
buildsection. Watch cannot update a service that has onlyimage:. - Wrong
target. Asynctarget that does not match the Dockerfile'sWORKDIRplaces files where the app never reads them. - Syncing
node_modules. Host-built modules may not suit the container's operating system. Exclude them withignore. - Forgetting to start watch. Plain
docker compose updoes not watch. Add--watchor rundocker compose watch.
Exercise
Your service has build: . and a rule with action: sync, path: ./src, target: /app/src. You edit ./src/index.js, but the running container does not change. You started it with docker compose up -d. What is the most likely cause, and how do you fix it?
Show answer
Watch mode was never started. docker compose up -d only starts the services. Run docker compose watch, or start with docker compose up --watch. After that, saving ./src/index.js copies it to /app/src/index.js.
Exercise
Why does the example use rebuild for package.json instead of sync?
Show answer
A changed package.json means dependencies changed. Those are installed into the image by RUN npm install, so copying the file does not install anything. The image must be rebuilt and the container recreated.