Sub topic 10
Restarting after configuration changes with sync+restart
Some files are read once, when a process starts. If you sync such a file into a running container, the process keeps using the old values. The sync+restart action fixes this. It copies the changed file into the container and then restarts the container, so the process starts again and reads the new contents.
This chapter assumes you know the following:
- The
develop.watchsection and thesyncandrebuildactions from the earlier chapters. - How
pathandtargetmap host files into the container. - How to start watch mode with
docker compose watchordocker compose up --watch.
The problem: files that are read only at startup
The sync action works well for source code, because many development servers watch their own files and reload when they change. Configuration is different. A typical application reads config/settings.json once during boot and keeps the parsed values in memory. After a sync, the file inside the container is correct, but the running process still holds the old values.
You could stop and restart the service by hand. That is the manual loop Compose Watch exists to remove. With sync+restart, Compose does both steps for you:
- It detects a change under the watched
path. - It copies the file to the
targetinside the running container. - It restarts that service's container.
The container is not rebuilt, and no new image is created. The existing container stops and starts again, and the synced file is still there when it comes back.
A small project that needs it
This example is a Node.js service that reads a JSON file at startup. The project layout is:
.
├── compose.yaml
├── Dockerfile
├── package.json
├── config/
│ └── settings.json
└── src/
└── server.jsThe server loads the configuration once, at the top of the file:
const http = require("http");
const fs = require("fs");
// Read once at startup. Later edits are not seen until the process restarts.
const settings = JSON.parse(fs.readFileSync("/app/config/settings.json", "utf8"));
const server = http.createServer((req, res) => {
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ greeting: settings.greeting }));
});
server.listen(3000, () => {
console.log(`Listening with greeting: ${settings.greeting}`);
});The configuration file is:
{ "greeting": "Hello" }The package.json needs a start script, because the Dockerfile runs npm start:
{
"name": "watch-demo",
"version": "1.0.0",
"scripts": {
"start": "node src/server.js"
}
}The Dockerfile is the one from earlier in the tutorial. Dependencies are installed in their own layer, and the rest of the project is copied afterward:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]Because of COPY . ., the image already contains /app/config/settings.json from the moment it was built. Watch only needs to deliver later changes.
Adding the sync+restart rule
Here is the Compose file. It has three rules, one for each kind of change:
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.jsonEach rule matches a different kind of file:
| Rule | Files it matches | What happens |
|---|---|---|
| `sync` on `./src` | Application source | Files are copied. The process keeps running. |
| `sync+restart` on `./config` | Configuration read at startup | Files are copied, then the container restarts. |
| `rebuild` on `package.json` | The dependency manifest | The image is rebuilt and the service is recreated. |
The sync+restart rule has the same fields as sync: action, path and target. Only the action name differs.
Start watch mode as before:
docker compose up --watchNow edit config/settings.json and change Hello to Bonjour. Compose copies the file and restarts the container. The service log shows the process starting again with the new value:
Listening with greeting: BonjourRequest the service with curl http://localhost:3000 and the response contains Bonjour.
What the restart does and does not do
The restart is a container restart, not a rebuild. This has consequences:
- The container filesystem survives. Files you synced earlier, including source files under
/app/src, are still present after the restart. - The main process starts again. Anything held in memory is lost, such as caches, open connections and in-memory sessions.
- The image is unchanged. If you change something that comes from the Dockerfile,
sync+restartcannot help. Userebuildfor that. - Only the service that owns the rule restarts. Other services in the project keep running. A database that
webdepends on is not restarted when you editweb's configuration.
The restart also costs time. For a small Node.js service it takes a second or two. For an application with a slow startup, such as a large JVM service, the pause is noticeable. If you edit a file often, think about whether it really needs a restart.
Comparing the actions on a change
The diagram shows what happens for each kind of host change.
When sync+restart is the right choice
Use sync+restart when all of the following are true:
- The file must be inside the container at runtime.
- The application reads the file only at startup.
- The change does not affect the image build, so a rebuild is unnecessary.
Typical examples are:
- Application config files such as JSON, YAML or TOML.
- Feature flag files.
- Web server configuration, such as an
nginx.conffile mounted into a proxy service. - Template or translation bundles that are loaded into memory at boot.
Do not use it in these cases:
- Source code with its own reloader. Frameworks and tools such as nodemon, Vite or Flask in debug mode reload on file change. A plain
syncis enough and avoids restarting the whole container. - Dependency manifests. A change to
package.jsonorrequirements.txtneeds the packages reinstalled, which only a rebuild does. - Configuration the service can reload without restarting. Some servers reload when they receive a signal. That is where
sync+execfits, as covered in the next chapter.
Common errors
Nothing happens when you edit the file. Check that the file is inside the rule's path. A path of ./config matches files under that directory. A file in the project root does not match. Also check that your ignore patterns are not excluding it.
The file is copied, but the old value is still served. You probably used sync instead of sync+restart. Compare the action name against the rule you expect to match.
The container restarts, but the old value still appears. The application may read the value from somewhere else. A common cause is an environment variable set in the Compose file or an env_file. Watch does not re-read the Compose file. Edits to compose.yaml are not applied while watch is running, so stop watch and run docker compose up --watch again.
The service fails after the restart. A syntax error in the config file will crash the process on startup. The container then exits or restarts repeatedly. Read the logs with docker compose logs web and fix the file. The next save triggers another sync and restart.
The rule does nothing and Compose reports an unknown action. sync+restart arrived in a later Compose release than the first version of watch. Run docker compose version and upgrade if you are on an old release.
Narrowing the rule with ignore
The ignore field works on sync+restart the same way as on sync. Editors often create swap and backup files, and you do not want those to restart your service. This version of the rule skips them:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
- action: sync+restart
path: ./config
target: /app/config
ignore:
- "*.swp"
- "*~"
- local/
- action: rebuild
path: package.jsonWith this rule, edits to config/local/ and to editor temporary files do not cause a restart. Other files under ./config still do.
Checking your understanding
Exercise
A service reads /app/config/features.yaml once at startup. You have a sync rule with path: ./config and target: /app/config. After you edit the file on the host, the file inside the container has the new content, but the application still uses the old feature flags. What do you change, and why does that fix it?
Show answer
Change the rule's action from sync to sync+restart. The sync action only copies the file, so the running process keeps the values it loaded at startup. sync+restart copies the file and then restarts the container, so the process starts again and reads the new features.yaml.
Exercise
You add a sync+restart rule for ./config. Your editor leaves settings.json.swp files in that directory, and each one restarts the service. How do you stop that without changing which configuration files trigger a restart?
Show answer
Add an ignore list to the rule, for example ignore: ["*.swp"]. Files matching the pattern no longer trigger a sync or a restart, and the other files under ./config still do.
Exercise
You change the ports mapping in compose.yaml while docker compose up --watch is running. The change has no effect. Is this a sync+restart problem? What should you do?
Show answer
No. Watch rules react to changes under the paths they list, and they do not re-read the Compose file. Stop watch and run docker compose up --watch again, so Compose applies the new service definition.