Sub topic 9
Ignoring files you should not sync
This chapter covers the ignore and include attributes of a Compose Watch rule. It assumes you can already write a develop.watch section with a sync rule and start watch mode with docker compose watch. You also need to know what a .dockerignore file is.
Why a sync rule needs a filter
A sync rule copies every change under path into the container at target. A source directory is rarely clean. It can contain dependency folders, build output, editor swap files, test coverage reports, and logs. Without a filter, each of these causes one of three problems:
- Wrong contents in the container. If the host
node_modulesis synced, it replaces the one installed during the image build. Native modules compiled for macOS or Windows then land in a Linux container and fail to load. - Wasted work. Package managers write thousands of files. Syncing them one by one is slow and can trigger reload loops in the dev server.
- Noise. Temporary files such as
.swpor~backups get copied into the container and may be picked up by the application's own file watcher.
The ignore attribute removes these paths from consideration. The include attribute does the opposite and lists the only paths to watch.
The ignore attribute
ignore takes a list of patterns. Here is a rule that syncs ./src but skips node_modules/:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.jsonCompose Watch reads each pattern as a .dockerignore-style pattern. Four details matter in practice.
- Patterns are relative to
path, not to the project root. In the rule above,node_modules/means./src/node_modules/. It does not match./node_modules/, which is outside the watched directory anyway. This is the most common source of confusion. - A trailing slash marks a directory.
node_modules/excludes the directory and everything beneath it. - Wildcards work as in
.dockerignore.*matches within one path segment and**matches across directories. So*.logignores log files directly underpath, and**/*.logignores them at any depth. - Ignore applies per rule. Each watch rule has its own
ignorelist. A pattern in one rule has no effect on another rule, even if the two paths overlap.
The ignore list is part of the rule, so it works with every action. It is most often attached to sync, sync+restart and sync+exec rules. Those are the actions that copy files.
The common case: node_modules
A typical Node.js project has this layout:
project/
compose.yaml
Dockerfile
package.json
node_modules/ # installed on the host, if you run npm locally
src/
index.js
node_modules/ # rare, but possible in monoreposIn the Dockerfile from the earlier chapters, RUN npm install creates /app/node_modules inside the image. The watch rule targets /app/src, so the main node_modules is never involved. The ignore line is a guard. It covers nested node_modules folders and the case where you later widen the rule to path: . and target: /app.
That wider rule is where ignore becomes necessary:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: .
target: /app
ignore:
- node_modules/
- .git/
- "*.log"
- coverage/
- action: rebuild
path: package.jsonHere the whole project root is synced. The ignore list keeps the container's own node_modules intact and skips Git metadata, logs and coverage output. The pattern *.log is quoted because a bare YAML value that starts with * is parsed as an alias and causes a syntax error. Always quote patterns that begin with *.
Deciding what a change does
Think of ignore as a gate in front of the action. Compose Watch checks each changed path against it before it does anything else.
A path that is ignored in one rule can still trigger another rule. Suppose a sync rule on ./src ignores *.test.js, and a rebuild rule watches the whole project. A change to src/a.test.js is skipped by the first rule but still reaches the second.
The include attribute
Sometimes the list of things you want is shorter than the list of things you do not want. In that case, include is easier to maintain than a long ignore list. It names the patterns that are watched, and files that do not match are skipped.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
include:
- "**/*.js"
- "**/*.json"With this rule, only JavaScript and JSON files under ./src are copied. Editor backups, .map files and images are left alone.
Use this table to choose between the two.
| Situation | Prefer |
|---|---|
| A few known folders must be excluded from a large tree | `ignore` |
| Only a few file types matter, such as `*.py` or `*.css` | `include` |
| You want to exclude something that would otherwise match a broad include | `ignore`, and test the combination |
| The tree changes often and new junk files keep appearing | `include` |
include is newer than ignore. Check that your Compose version supports it by running docker compose version. If the attribute is rejected with a validation error, upgrade Compose. If you combine include and ignore in one rule, make a test edit to confirm the result before you rely on it.
Ignoring in one rule while watching in another
Every rule filters separately. This lets you carve out files that need a different action. The example below syncs ordinary source files but handles config/ through its own rule, so a restart can happen when configuration changes.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- "**/*.test.js"
- action: sync+restart
path: ./config
target: /app/config
- action: rebuild
path: package.jsonThe three paths do not overlap, so no change matches two rules. Avoid overlapping paths when you can, because a file that matches two rules runs both actions.
The relationship with .dockerignore
ignore in a watch rule and .dockerignore serve different stages.
.dockerignorecontrols which files are sent to the builder as the build context. It affectsCOPY . .in the Dockerfile and therefore what ends up in the image.ignoreindevelop.watchcontrols which host changes are acted on while the stack is running.
A file excluded by .dockerignore is missing from the image. If you also sync it with a watch rule, it still appears in the container after the first change. Conversely, a file that is in the image but listed in ignore stays at the version from the last build. Keep the two lists consistent for the paths you care about, such as node_modules/ and .git/.
Common mistakes
- Writing patterns relative to the project root. With
path: ./src, the patternsrc/tmp/matches nothing. Writetmp/. - Forgetting that a rebuilt image replaces synced files. After a
rebuild, the container starts from the image. Files that were synced and also ignored on the next build are gone. Make sure the Dockerfile copies what the app needs. - Leaving out an unquoted wildcard. A pattern such as
*.logmust be written as"*.log". - Ignoring the
targetside. Patterns match host paths underpath. They cannot filter by container path. - Expecting ignore to delete files. Ignoring a path stops future syncs. It does not remove a copy that is already in the container.
To check that a pattern works, edit a file that should be ignored while watch is running and look at the Compose output. A sync event is logged for files that are copied. If nothing appears for the ignored file, the filter worked. If the file appears in the container with docker compose exec web ls /app/src, the pattern did not match, usually because of the relative-path rule above.
# Start watch mode
docker compose up --watch
# In another terminal, touch a file that should be ignored
mkdir -p src/node_modules/demo
touch src/node_modules/demo/index.js
# Confirm it was not copied into the container
docker compose exec web ls /app/src/node_modulesIf the last command lists demo, the ignore pattern did not apply. Re-check which path the rule uses.
Exercise
A service has the rule path: ./app, target: /usr/src/app, ignore: [app/cache/]. Files in ./app/cache/ are still copied into the container. Why, and how do you fix it?
Show answer
Ignore patterns are relative to the rule's path, so app/cache/ is read as ./app/app/cache/, which does not exist. Change the pattern to cache/.
Exercise
You want to sync only .py and .html files under ./site, and skip everything else. Which attribute is the shorter way to write this, and what are its values?
Show answer
Use include with "**/*.py" and "**/*.html" on the sync rule with path: ./site. This is shorter than ignoring every other file type.