Sub topic 3
Adding develop.watch to a service
This chapter assumes you can write a Compose file with build, ports, and command. It also assumes you know how a Dockerfile turns into an image, and that you have a Node.js-style project with source files and a package.json. You also need a recent Docker Compose v2 (watch arrived in version 2.22, and some actions came later). Run docker compose version to check.
By the end you will be able to add a develop.watch section to a service, read each field in a rule, and recognize the mistakes that stop a rule from doing anything.
What develop.watch is
develop is a section of a service definition that holds settings meant only for development. Its watch key holds a list of rules. Each rule says: "when files at this host path change, perform this action on the running container."
Compose Watch does not mount your project into the container. It runs a file watcher on the host. When it sees a change, it either copies the file into the container or rebuilds the service, depending on the rule. The container keeps its own copy of the files, and the watcher keeps that copy current.
The section sits beside build and ports on the same service:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/srcThis does nothing until you start watch mode, which a later chapter covers. Nothing happens on a plain docker compose up.
Anatomy of a rule
Every item in the watch list is a mapping with these keys.
| Key | Required | Meaning |
|---|---|---|
| `action` | Yes | What to do on change: `sync`, `rebuild`, `sync+restart`, or `sync+exec` |
| `path` | Yes | Host file or directory to watch, relative to the folder containing the Compose file |
| `target` | For the `sync` family | Path inside the container that corresponds to `path` |
| `ignore` | No | Patterns, relative to `path`, that the rule skips |
| `include` | No | Patterns, relative to `path`, that narrow a rule to specific files (newer Compose versions) |
| `exec` | Only for `sync+exec` | The command to run in the container after the sync |
Two points about this table:
rebuildtakes notarget. It rebuilds the image from the build context, so there is no container path to map.pathcan name a directory or a single file. A directory is watched recursively. A single file such aspackage.jsontriggers the rule only when that file changes.
The four actions get their own sections later in this tutorial. This chapter uses sync and rebuild because they cover the most common case.
A first working configuration
Compose Watch needs the service to be built from a Dockerfile, so start with the image definition. This Dockerfile installs dependencies in their own layer:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]The order matters. npm install runs only when the COPY package*.json layer changes. When you edit a source file and rebuild, Docker reuses the cached dependency layer, so the rebuild is fast.
Now the Compose file. It pairs a sync rule for source code with a rebuild rule for the dependency manifest:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.jsonRead the rules in order:
- A change under
./srcon the host is copied to/app/srcin the container. Anynode_modules/folder under./srcis skipped. - A change to
package.jsonrebuilds the image, which re-runsnpm install, and then replaces the container with one from the new image.
A sync rule only copies files. Your application must notice the new files on its own. For Node.js, a tool such as nodemon does this. A minimal package.json for the example could be:
{
"name": "web",
"version": "1.0.0",
"scripts": {
"start": "nodemon src/index.js"
},
"dependencies": {
"express": "^4.19.0"
},
"devDependencies": {
"nodemon": "^3.1.0"
}
}If your program has no reloader of its own, use sync+restart instead. Compose then restarts the container after copying.
How Compose evaluates a change
Compose checks each changed path against the rules in the list. A rule applies when the path is inside the rule's path and does not match its ignore patterns (or does match its include patterns, if you set them). Then Compose runs that rule's action.
A few edge cases follow from this model:
- More than one rule can match. If one rule watches
./and another watches./src, a change in./srccan trigger both. Keep rules from overlapping unless you want both actions. - Order does not create priority. Every matching rule applies. Separate paths are the reliable way to give each file type one action.
- Deleted files are removed. With
sync, deleting a file on the host deletes the copy in the container. - Rules are independent per service. Each service has its own
develop.watchlist.
Why a build section is required
Compose Watch has to be able to rebuild and to know which files belong in the image. A service that uses only image: nginx has no build context, so there is nothing to rebuild and no reliable layout to sync into. Compose skips such services. If none of the selected services has a develop section, it tells you so and does nothing.
The sync side has its own container requirements:
- The
targetpath should match where your Dockerfile places the files. In the example,COPY . .withWORKDIR /appputs source files at/app/src, sotarget: /app/srclines up. - The container user needs write permission on the target directory. If the Dockerfile switches to a non-root user, make sure that user owns the application directory.
- The image should include basic utilities such as
stat,mkdir, andrmdir. Compose uses them to apply changes. Standard Alpine and Debian based images have them. Minimal or distroless images may not.
Common mistakes
These are the errors you are most likely to hit when you first add a develop.watch section.
- Wrong
target. Ifpath: ./srcmaps to/srcbut the app lives in/app/src, the copy succeeds and nothing visible changes. Open a shell in the container and check where the files really are. - Syncing the whole project. A rule with
path: ./andtarget: /appalso copies the host'snode_modulesover the container's. Native modules built for your laptop's operating system then break inside a Linux container. Narrow the path to./src, or addnode_modules/toignore. - No reloader. Files arrive in the container, but the running process still holds the old code in memory. Add a reloader, or use
sync+restart. - Dependency changes under
sync. Copying a newpackage.jsoninto the container does not runnpm install. Userebuildfor manifests. - Relative paths read from the wrong folder.
pathis relative to the Compose file, not to the directory you run the command from. If you use-fto point at a Compose file in another folder, paths shift with it. - YAML indentation.
developmust be a key of the service, at the same level asbuild.watchis a list, so each rule starts with-.
Putting the pieces in order
When you add watch to an existing service, work in this order:
- Confirm the service has a
buildsection and a Dockerfile that copies sources to a known path. - Add
develop.watchwith onesyncrule for the source directory. - Add a
rebuildrule for each dependency manifest (package.json, and lockfiles if you want them to trigger rebuilds). - Add
ignoreentries for folders that should never be copied. - Start watch mode, edit a file, and confirm the change appears in the container and the app reloads.
Exercise
A service in your Compose file has image: node:20-alpine and a command, but no build section. You add a develop.watch rule with action: sync, path: ./src, and target: /app/src. Why will watch not update this service, and what is the smallest fix? Also, a teammate sets path: ./ and target: /app for the same rule. What problem can that cause?
Show answer
Compose Watch only works on services that Compose builds from a Dockerfile. With only image:, there is no build context to rebuild or to define the file layout, so the service is skipped. The fix is to add a build section (for example build: .) and a Dockerfile that copies the sources into /app. The teammate's ./ to /app rule also syncs the host's node_modules over the container's, which can break native modules built for a different operating system. Narrow the path to ./src or add node_modules/ to ignore.