Sub topic 7
Syncing source files with the sync action
This chapter assumes you can write a Compose file with build and ports, and that you know what a Dockerfile layer is. It covers the sync action: what it does, how to configure it, and the errors you will meet most often.
What sync does
The sync action copies a changed file from the host into the running container. The container keeps running. The image is not rebuilt. If your application has a dev server that reloads when files change, such as a Node.js framework or Flask in debug mode, the copy is all you need.
Use sync for source code that the running process re-reads on its own. Typical cases are .js, .py and .html files, and stylesheets.
A working configuration
This Dockerfile installs dependencies in their own layer. A source-only change then does not repeat npm install when the image is rebuilt.
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]This Compose service syncs ./src into the container and rebuilds the image when package.json changes.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.jsonEach entry in watch is a rule with these fields:
| Field | Meaning |
|---|---|
| `action` | What to do when a matching file changes. Here it is `sync`. |
| `path` | The host location to watch, relative to the Compose file. |
| `target` | The container location that receives the copy. Required for `sync`. |
| `ignore` | Patterns that Compose skips inside `path`. |
How a change travels
Compose maps the file's position under path to the same position under target. Saving ./src/routes/user.js updates /app/src/routes/user.js. Adding a new file copies it, and deleting a file removes it from the container.
Start watching
Run watch together with up, or start the services first and run it separately.
# Start the services and watch for changes in one command
docker compose up --watch
# Or start the services first, then run watch separately
docker compose up -d
docker compose watchEdit a file under ./src and save it. Compose logs the sync, and the container sees the new content without a restart.
Common errors
- The change never appears. Check that the file sits under
path. A file in./libis not covered by a rule for./src. - The file arrives but nothing happens.
synconly copies. If the process reads the file once at startup, the old code stays in memory. Use a dev server that reloads, or switch the rule tosync+restart, which later chapters cover. - The
targetis wrong. The target must match where the Dockerfile placed your code. WithWORKDIR /appandCOPY . ., the source directory is/app/src, so a target of/srccopies to the wrong place. node_modulescauses noise or breaks the app. Host dependencies may be built for a different operating system than the container's. Ignore the directory, as the example does.- Watch refuses to start. Compose Watch works with services that have a
buildsection. A service that only usesimage:has nothing to rebuild or sync into.
Exercise
You save ./src/utils/format.js. The rule is action: sync, path: ./src, target: /app/src. Which container path is updated, and why might the running app still show old behavior?
Show answer
The file /app/src/utils/format.js is updated, because Compose keeps the relative position under path and applies it under target. The app can still show old behavior if the process loaded the module at startup and has no reload mechanism. sync copies the file but does not restart the process.
Exercise
A teammate's rule reads action: sync, path: ./src, with no target. What is missing, and what should it be for a Dockerfile that uses WORKDIR /app and COPY . .?
Show answer
The target field is missing. It should be /app/src, because COPY . . places the host's src folder at /app/src.