Sub topic 9
Ignoring files you should not sync
This chapter assumes you can write a Compose service with a build section and have added a develop.watch rule with the sync action. If not, review the earlier chapters on develop.watch and sync first.
Why some files must stay out
A sync rule copies everything under path into target. That is often more than you want. Take a Node.js project where npm install ran inside the image build. The node_modules folder in the container was built for the container's operating system. If you also have a node_modules folder on your host, syncing it would overwrite the container's copy with files built for a different platform. Native modules would then fail to load.
Other files cause noise instead of damage. Editor swap files, build output and logs each trigger a sync that does nothing useful.
Excluding paths with ignore
Add an ignore list to a watch rule. Each entry is a pattern, and matching files are skipped.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- "*.log"
- "**/*.tmp"
- action: rebuild
path: package.jsonPatterns are relative to the path of the rule they belong to, not to the project root. In the rule above, node_modules/ means ./src/node_modules/. If your node_modules folder sits at the project root, it is outside ./src and is never watched by this rule, so the entry is harmless but unnecessary. It matters when you watch . as the path:
- action: sync
path: .
target: /app
ignore:
- node_modules/
- .git/
- dist/Here the whole project is watched, so you must exclude everything that should not enter the container.
Selecting files with include
Sometimes it is easier to list what you want than what you do not. The include attribute does this. Only files matching an include pattern are watched under that path.
services:
web:
build: .
develop:
watch:
- action: sync
path: ./src
target: /app/src
include:
- "**/*.js"
- "**/*.css"With this rule, a change to src/notes.txt does nothing, while a change to src/app.js is synced. You can use include and ignore in the same rule. In that case, treat include as the wide net and ignore as the exceptions to carve out of it.
| Attribute | Question it answers | Best when |
|---|---|---|
| `ignore` | Which files should I skip? | Few things to exclude, such as `node_modules/` or logs |
| `include` | Which files should I watch? | Few file types matter, such as source and styles |
Checking that a rule works
Start watch mode and change a file that should be skipped. Nothing should appear in the watch output. Then change a file that should sync and confirm that a sync message appears.
docker compose up --watchIf an ignored file still shows up in the container, check these causes:
- The pattern is written relative to the project root instead of the rule's
path. - The file was already in the image from
COPY . .. Ignoring it in watch only stops later syncs. It does not remove what the build copied in. - A second watch rule with a wider
pathalso matches the file.
Exercise
Your rule is path: ./src, target: /app/src, with ignore: ["config/"]. Which host directory is excluded from syncing, and why might ./config at the project root still be unaffected?
Show answer
The excluded directory is ./src/config/, because ignore patterns are relative to the rule's path. The project-root ./config is outside ./src, so this rule never watches it. It is neither ignored nor synced by this rule.