Sub topic 12
Choosing the right action for each file type
This chapter assumes you can already write a develop.watch section, start it with docker compose watch or docker compose up --watch, and read a Dockerfile. If any of that is unfamiliar, review the earlier chapters on the develop.watch section and on starting watch mode first.
Each rule in develop.watch pairs a host path with one of four actions: sync, rebuild, sync+restart, and sync+exec. Every action works, but each costs a different amount of time. The goal is to pick the cheapest action that still gets the change into the running application.
The question to ask for each file
For every kind of file in your project, ask what the running process needs before it notices the change. There are three possible answers:
- The process already reads the file on each use, or a dev server reloads itself. Copying the file is enough.
- The process reads the file once at startup. Copying is not enough, and the process must restart.
- The file is baked into the image, for example installed packages or compiled output. Copying is not enough, and the image must be rebuilt.
The sections below map those answers to actions. A fourth case, where a different command must run after the copy, is covered under sync+exec.
Cost of each action
| Action | What happens | Typical cost | Use for |
|---|---|---|---|
| `sync` | Copies the changed file into the container | Milliseconds | Source files handled by a hot-reloading tool |
| `sync+restart` | Copies the file, then restarts the service container | Seconds | Config files read only at startup |
| `sync+exec` | Copies the file, then runs a command inside the container | Depends on the command | Services that support an in-place reload |
| `rebuild` | Rebuilds the image and recreates the container | Seconds to minutes | Dependency manifests, Dockerfile inputs, compiled code |
The rule of thumb is to start at the top of the table and move down only when the cheaper action does not work.
Source files: sync
Source files in an interpreted language usually need nothing more than a copy. A development server such as nodemon, Vite, Flask in debug mode, or Rails in development mode watches the filesystem inside the container. When sync places the new file there, the tool reloads it.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/sync only moves the file. Whether the application then picks up the change depends on the process. If the process is a plain node server.js with no reload tool, the new file is copied but the old code keeps running. The symptom is that the file inside the container is correct while the behavior is old. Fix that either by running a reloading tool as the container command or by switching the rule to sync+restart.
Dependency manifests: rebuild
A change to package.json, requirements.txt, go.mod, or Gemfile means the installed packages are out of date. Those packages live in an image layer created by RUN npm install or an equivalent command. Copying the manifest into the container would not rerun that command. Use rebuild:
- action: rebuild
path: package.jsonThe Dockerfile from earlier chapters copies package*.json and installs before it copies the rest of the source:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]This layering makes rebuild cheaper. When only a source file changes, the npm install layer is cached. When package.json changes, that layer is rebuilt, which is the intended behavior.
Use rebuild for these inputs:
- Dependency manifests and lock files.
- The Dockerfile itself, if you edit it often.
- Source files of compiled languages (Go, Rust, Java, C#) when the container runs a compiled artifact. The compile step happens during the image build, so the only way to get new code running is to build again.
- Files that a
COPYorRUNstep consumes during the build, such as a code generator's input.
For compiled languages, a rebuild on every source change is slow but correct. Some teams instead run a language-specific watcher in the container, such as air for Go, and use sync to feed it. That is a trade-off between a dev-only image and parity with the production image.
Startup-only configuration: sync+restart
Some files are read once when the process starts. Examples are a config.json, an .env-style file loaded at boot, or a settings module imported at startup. A plain sync copies them but the process never rereads them. sync+restart copies the file and then restarts the container, which makes the process read it again.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
- action: sync+restart
path: ./config
target: /app/config
- action: rebuild
path: package.jsonA restart reuses the existing container and image, so it is much faster than rebuild. Because it restarts, in-memory state such as sessions held in the process is lost. Keep the restart rules narrow so ordinary source edits do not trigger them.
In-place reloads: sync+exec
sync+exec copies the file and then runs a command inside the running container. It fits services that can reload their configuration without restarting. A web server such as nginx is the classic case:
services:
proxy:
build: ./proxy
ports:
- "8080:80"
develop:
watch:
- action: sync+exec
path: ./proxy/conf
target: /etc/nginx/conf.d
exec:
command: nginx -s reloadWhen a file under ./proxy/conf changes, Compose copies it to /etc/nginx/conf.d and then runs nginx -s reload. Active connections survive, which a restart would not allow. The exec block can also take options such as user and working_dir to control how the command runs.
Choose sync+exec over sync+restart when all three conditions hold:
- The program has a documented reload command or signal.
- Dropping connections or in-memory state during development is a nuisance.
- The command is available inside the image.
If the command fails, for example because the config has a syntax error, the rule fails but the old process keeps running. Check the watch output in your terminal for the error.
Assigning actions across a whole project
A realistic service combines several rules. Each path maps to one action, and the rules should not overlap.
| File or folder | Action | Reason |
|---|---|---|
| `./src` | `sync` | The dev server reloads changed modules |
| `./public` | `sync` | Static assets are read on each request |
| `./config` | `sync+restart` | Read only when the process boots |
| `./proxy/conf` | `sync+exec` | The server supports a reload command |
| `package.json` | `rebuild` | Packages are installed during the image build |
| `Dockerfile` | `rebuild` | The image definition changed |
Two points keep this table working in practice:
- Ignore build output and dependencies. Add
ignoreentries such asnode_modules/tosyncrules. The host'snode_modulesmay contain binaries built for a different operating system than the container, and copying them over the container's own install can break it. - Do not watch the same path with two rules. If
./srcis covered by bothsyncandrebuild, every edit triggers a rebuild and the cheap rule gains you nothing. Useignoreto carve out the exceptions.
When the choice is wrong
Mismatched actions have recognizable symptoms.
- The file is updated in the container but the app behaves the same. The process does not reread it. Move from
synctosync+restartorsync+exec, or run a reloading dev server. - New dependency is "not found" after editing the manifest. The rule for the manifest is
syncor missing. Change it torebuild. - Every small edit takes a long time. A broad
rebuildrule covers source files, or the Dockerfile copies the whole project before installing dependencies and so invalidates the cache. - The app restarts on every save. A
sync+restartrule covers a path that also contains source files. Narrow the path.
Exercise
A Python service reads settings.toml once at startup. Application code under ./app is served by a dev server that reloads automatically. Dependencies are listed in requirements.txt and installed in the Dockerfile. Which action would you assign to ./app, to settings.toml, and to requirements.txt?
Show answer
Use sync for ./app, because the dev server reloads the copied files itself. Use sync+restart for settings.toml, because the process reads it only at startup and must be restarted. Use rebuild for requirements.txt, because the packages are installed during the image build and only a new build will update them.
Exercise
You add a sync rule for ./src and a rebuild rule for the whole project directory .. Editing a source file now triggers a slow image build every time. Why, and how do you fix it?
Show answer
The rebuild rule on . also matches files under ./src, so every source edit triggers a rebuild. Narrow the rebuild rule to the specific inputs, such as package.json, or add ignore entries to it so it skips ./src. Then the sync rule alone handles source changes.
Exercise
An nginx container should pick up changes to files in ./proxy/conf without dropping active connections. Which action fits, and what extra field does it need?
Show answer
sync+exec, with an exec block whose command reloads the server, for example nginx -s reload. The files are copied first and the command then runs inside the container.