Sub topic 11
Running a command after sync with sync+exec
This chapter assumes you can write a Compose file with a build section, you know what develop.watch and the sync action do, and you can run docker compose watch. It also assumes Docker Compose 2.32.0 or later, which is the release that added sync+exec.
What sync+exec does
sync+exec copies changed files into the container, like sync. It then runs a command inside the same running container. The container is not restarted and no new image is built.
Some programs read their files only at startup, but they can reload without a restart. Nginx is an example. It re-reads its configuration when it gets a reload signal. A sync+exec rule can send that signal each time the file changes.
Choosing between similar actions
| Action | Copies files | Then | Container process |
|---|---|---|---|
| `sync` | Yes | Nothing | Keeps running |
| `sync+restart` | Yes | Restarts the container | Starts again |
| `sync+exec` | Yes | Runs your command in the container | Keeps running |
Use sync+exec when the application has its own reload command. This is faster than a restart, and the process keeps its state and open connections.
A worked example
This example reloads Nginx when its configuration changes. First, create conf/default.conf:
server {
listen 80;
location / {
return 200 "hello v1\n";
}
}Next, create the Dockerfile. A watched service must be built from a build section, so the configuration is copied into the image:
FROM nginx:1.27-alpine
COPY conf/ /etc/nginx/conf.d/Then create compose.yaml:
services:
proxy:
build: .
ports:
- "8080:80"
develop:
watch:
- action: sync+exec
path: ./conf
target: /etc/nginx/conf.d
exec:
command: nginx -s reloadThe path and target fields work as they do for sync. Compose copies files from ./conf on the host to /etc/nginx/conf.d in the container. The exec.command field is required for this action.
Start watching, then check the response:
docker compose up --watch
curl http://localhost:8080You should see hello v1. Change the string in conf/default.conf to hello v2 and save. Compose syncs the file and runs nginx -s reload. Run curl again and you should see hello v2, with the same container still running.
Optional exec settings
The exec block accepts more than command. These fields are optional:
user: the user the command runs as.working_dir: the directory the command runs in.environment: extra environment variables for the command.privileged: runs the command with extended privileges.
exec:
command: nginx -s reload
user: root
working_dir: /etc/nginxCommon errors
- The file changes but behavior does not. The command may have failed. Read the Compose output for the exit status, and run the same command by hand with
docker compose exec proxy nginx -s reload. - "command not found". The command runs inside the container, so it must exist in the image. A tool installed only on your host does not count.
- The command runs before the application is ready. The command runs as soon as the sync finishes. If the process cannot reload at that moment, use
sync+restartinstead. - Rule validation fails. Older Compose versions do not know this action. Run
docker compose versionand upgrade if it is below 2.32.0.
Exercise
A Python service loads settings.toml only at startup, but it reloads its settings when you run kill -HUP 1 inside the container. Which action fits, and what would the rule look like?
Show answer
Use sync+exec, because the app can reload without a restart. For example: action: sync+exec, path: ./settings.toml, target: /app/settings.toml, and exec: {command: kill -HUP 1}. If the app had no reload mechanism, sync+restart would be the right choice.