Sub topic 10
Restarting after configuration changes with sync+restart
This chapter assumes you can write a Compose file with build and ports, and that you have a develop.watch section with a sync rule working. It covers the sync+restart action: when to use it, how to write the rule, and the mistakes people commonly make.
Why sync alone is not enough
The sync action copies a changed file into the running container. That is enough when the process reads the file again on its own. A dev server with hot reload does this for source code.
Some files are read only once, when the process starts. Examples are a JSON or YAML config file, an .env-style settings file, or a routing table loaded at boot. After sync, the container holds the new file, but the running process still uses the old values in memory. You would have to restart the service by hand.
sync+restart removes that step. Compose copies the file, then restarts the service container.
The rule
Add a second rule next to your source sync rule:
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 has its own job:
./srcusessync. Source files are copied and the dev server reloads them without a restart../configusessync+restart. Files are copied, then the container restarts so the process rereads them.package.jsonusesrebuild. A dependency change needs a new image.
The path and target fields work the same way as in a plain sync rule. path is relative to the Compose file on the host, and target is the matching location inside the container.
What happens at runtime
The restart is of the container, not the image. No rebuild happens, so the restart takes seconds. Any state held only in the container's memory or its writable layer is lost on restart, so do not keep data you need there.
Running it
Start watch mode the usual way:
docker compose up --watchEdit ./config/app.json and save. In the log output you should see the sync, then the service restarting. Edit a file under ./src and notice that the container does not restart.
Choosing between sync+restart and rebuild
Both options end with a fresh process, so they are easy to mix up. The difference is what is stored in the image.
| Change | Action | Reason |
|---|---|---|
| Config file read at startup | `sync+restart` | File only needs to be replaced, then reread |
| Dependency manifest | `rebuild` | Installed packages live in the image |
| Source file with hot reload | `sync` | Process picks it up on its own |
If a file affects only what the process reads at boot, sync+restart is faster. If a change affects what was installed or compiled into the image, use rebuild.
Common errors
- Using
sync+restartfor source code. Every save restarts the container, which is slow and loses the benefit of hot reload. Usesyncthere. - Overlapping paths. If
./configalso sits inside a path covered by asyncrule, a change could match both rules. Keep rule paths separate so each file has one clear action. - Wrong
target. If the target does not match where the app reads the file, the restart succeeds but the old value still appears. Check the path withdocker compose exec web ls /app/config. - Forgetting the build section. Watch applies to services built from a
buildsection. A service that only usesimage:is not a valid target.
Exercise
Your app loads ./settings/flags.yaml once at startup. You use a sync rule for it, but after editing the file the app still shows the old flags. What is the likely cause, and how do you fix it?
Show answer
sync copies the file but does not restart the process, which keeps the old values in memory. Change the rule for ./settings to sync+restart (with the matching target), so the container restarts after the copy and the app rereads the file.
Exercise
You edit package.json to add a dependency. Why is sync+restart the wrong action for this file?
Show answer
The new package must be installed into the image. sync+restart only copies the file and restarts the container, so the dependency would not be installed. Use rebuild.