Sub topic 12
Choosing the right action for each file type
This chapter assumes you can read a Compose file, know what a Dockerfile layer is, and have already used develop.watch with the sync and rebuild actions. Here you will learn how to pick an action for each kind of file in a project.
The decision in one question
Each watch rule answers one question: what must happen inside the container for this change to take effect? Start with the cheapest action and move up only when the cheaper one fails.
| File type | What the app does with it | Action |
|---|---|---|
| Source code with a dev server that reloads itself | Reads files while running | `sync` |
| Configuration read only at startup | Reads files once, at boot | `sync+restart` |
| Dependency manifest such as `package.json` | Installed at image build time | `rebuild` |
| Files that need a command to take effect | Needs a step such as a migration | `sync+exec` |
A rule for each kind of file
The following service combines three actions. It uses the Dockerfile from the earlier chapters, where npm install runs in its own layer.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: sync+restart
path: ./config
target: /app/config
- action: rebuild
path: package.jsonEach rule maps to a file type:
./srcusessync. The files are copied into the container and the dev server's own reload mechanism picks them up. Nothing restarts, so this is the fastest option../configusessync+restart. The files are copied and then the service restarts. This suits settings that the application reads only when it starts. A plainsynchere would copy the file, but the running process would keep its old values.package.jsonusesrebuild. Installed packages live in an image layer, so copying the manifest into the container would not install anything. Compose rebuilds the image and recreates the container.
Use sync+exec when a copied file needs a command to run inside the container, for example a code generation step. It takes an extra exec field with the command to run.
Why order matters: cost and risk
The actions differ in cost. sync takes about as long as a file copy. sync+restart adds a process restart, so in-memory state is lost. rebuild runs a Docker build and replaces the container, which is the slowest option. Because the Dockerfile copies package*.json before the source, a rebuild reuses cached layers up to the npm install step and only repeats the work that changed.
Choosing too low an action causes confusing symptoms. The file in the container changes, but the app behaves as before. Choosing too high an action works but wastes time, for example by using rebuild for plain source files.
Common mistakes
- Using
syncfor startup-only configuration. The file updates but the behavior does not. - Using
syncforpackage.json. The manifest is copied but no new package is installed. - Syncing
node_modulesfrom the host. Host binaries may not match the container's operating system, so addnode_modules/toignore. - Overlapping paths. If two rules cover the same file, it is unclear which action should win, so keep paths distinct.
Exercise
Exercise
Your app reads config/settings.json once at startup, and the source in src/ is served by a dev server that reloads on file changes. Which action should each path use, and why?
Show answer
Use sync for ./src, because the dev server reloads the copied files without a restart. Use sync+restart for ./config, because the process reads the file only at startup and must be restarted to see the new values.
Exercise
You add a new package to package.json while a sync rule covers the whole project directory. The container still cannot find the package. What is the fix?
Show answer
Add a rebuild rule for package.json. Dependencies are installed during the image build, so the image must be rebuilt for the new package to exist in the container.