Sub topic 7
Syncing Source Files with the sync Action
This chapter covers the sync action, the rule you will use most often in develop.watch. It assumes you can write a Compose file with build, ports, and command, and that you know what an image layer is. It also assumes you have a small web project with a dependency manifest such as package.json. The earlier chapters introduced the develop.watch section and showed how to start watch mode. Here the focus is on how sync copies files, what it expects from the container, and where it fails.
What sync does
A sync rule tells Compose to copy changed files from a path on your host into a target path inside the running container. The container is not rebuilt or restarted. The files in it are replaced while it runs.
Each rule has three parts:
action: syncselects the behavior.pathis the host location to watch, relative to the directory that holds the Compose file.targetis the matching location inside the container.
Compose watches path recursively. When you create, change, or delete a file under it, Compose applies the same change under target. Deletes are synced too, so a file you remove on the host disappears from the container.
Syncing only moves files. The process in the container must notice the new files and use them. Frameworks with their own reloaders do this, such as nodemon, webpack dev server, Vite, Flask in debug mode, or Rails in development mode. If your process loads code once at startup and never rereads it, sync alone will not change what the application does. That case is handled by sync+restart, covered in a later chapter.
A working example
The example is a minimal Express app that reloads with nodemon. Create these four files in an empty directory.
{
"name": "watch-demo",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "nodemon src/server.js"
},
"dependencies": {
"express": "^4.19.2"
},
"devDependencies": {
"nodemon": "^3.1.0"
}
}Save this as package.json. Save the next file as src/server.js.
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello from version 1\n");
});
app.listen(3000, () => {
console.log("Listening on port 3000");
});The Dockerfile installs dependencies in their own layer, so a source-only change 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"]Finally, the Compose file adds a sync rule for ./src.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/Start the stack with watch mode enabled:
docker compose up --watchIn another terminal, check the response, then edit the message in src/server.js and check again:
curl http://localhost:3000Change the string to "Hello from version 2\n" and save the file. Within a second or two, nodemon in the container logs a restart. The next curl returns the new text. No image was built and the container kept running. Compose copied the file and nodemon restarted the Node process inside it.
Following one edit
The diagram shows what happens between saving a file and seeing the new response.
Compose handles the first half and your application's tooling handles the second. When a sync "doesn't work", first decide which half failed.
How path and target map files
Compose takes the part of a changed file's path that comes after path and appends it to target. With path: ./src and target: /app/src, the mapping looks like this:
| Host file | Container file |
|---|---|
| `./src/server.js` | `/app/src/server.js` |
| `./src/routes/users.js` | `/app/src/routes/users.js` |
| `./src/new-dir/` (created) | `/app/src/new-dir/` |
| `./src/old.js` (deleted) | `/app/src/old.js` (removed) |
| `./package.json` | Not synced, outside `path` |
Some points follow from this mapping:
- Match the target to where the Dockerfile placed the files. The Dockerfile above sets
WORKDIR /appand runsCOPY . ., so./srclands at/app/src. If you mapped it to/src, Compose would copy files to a directory your application never reads. - Use an absolute path for
target. Relative paths would depend on the container's working directory and are easy to misread. - A
pathcan also be a single file, such as./config.json. A directory is better when several related files change together. - Only files under
pathare considered. A change topackage.jsondoes nothing unless another rule covers it, which is why the laterrebuildrule exists.
Ignoring files you should not sync
Without filtering, a sync rule copies everything under path. That can be harmful. The most common case is a dependency directory such as node_modules. You installed those packages inside the image, for Linux and the container's CPU architecture. If your host node_modules is copied over them, native modules built for macOS or Windows can overwrite the Linux ones and crash the app.
The ignore list takes patterns that are relative to path:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- "**/*.test.js"
- "*.log"Quote any pattern that begins with * so YAML does not read it as an alias. In this example ./src is the watched path, so node_modules/ only matches a node_modules directory inside src. The project's top-level node_modules is outside path and is never watched. It still helps to list node_modules/ when you watch the project root with path: ., which is common in small projects. If you do that, mapping . to /app also syncs files like Dockerfile and compose.yaml, so ignore the files you do not need in the container.
Sync needs a way to reload
Most mistakes with sync come from the second half of the sequence diagram. Check these before blaming Compose.
- The process has no reloader. A plain
node src/server.jsreads files once. The container has the new file but the old code is still in memory. Use a watcher likenodemon, or usesync+restart. - The reloader does not see the change. File watchers inside containers rely on filesystem events. Synced writes generate them in the normal way, but some tools default to polling or have ignore lists. Check the tool's own configuration if the file is correct in the container but nothing restarts.
- The build tool needs a compile step. TypeScript or bundled front ends often read compiled output rather than source. If you sync
.tsfiles into a container that runs compiled.js, either run the compiler in watch mode in the container or choose a different action.
You can check the first half of the sequence directly. Compare the file inside the container with the one on your host:
docker compose exec web cat /app/src/server.jsIf this shows your latest edit, sync worked and the problem is the reloader.
Edge cases and limitations
- Services need a
buildsection. Watch rules only apply to services Compose builds. A service that uses onlyimage:is skipped, because there is no build context to relate the watched files to. - The container needs basic tools. Syncing relies on standard utilities such as
stat,mkdir, andrmdirbeing present in the image. Very minimal images may lack them. Thenode:20-alpinebase used here has them. - The container user needs write access. Compose writes as part of the container's environment, so the user running your process must be able to create and replace files under
target. If a Dockerfile switches to a non-root user, make sure that user owns the application directory. - Changes made while Compose is not watching are lost. Syncing reacts to events. If you edit files before running
docker compose watch, the container still holds the version copied at build time. Rebuild withdocker compose up --build, then start watching. - Synced files are not in the image. The files reach the running container only. If you remove and recreate the container, it starts again from the image, which holds the files from the last build. Rebuild before you depend on the image.
- Don't edit inside the container. A container-side change is not copied back to the host, and a later host change overwrites it.
Choosing sync
sync is the right choice when a file is read at runtime and your application reloads without help from Compose. Source code under a hot-reloading framework is the main example, along with static assets and templates. Do not use it for dependency manifests, because installed packages come from a build step. Do not use it for files that are only read at startup unless you add a restart. The next chapters add the rebuild and sync+restart actions for those cases.
Exercise
Your Compose file has path: ./src and target: /app/src. You create ./src/utils/format.js on the host. Where does the file appear in the container, and what two things must be true for the running app to use it?
Show answer
It appears at /app/src/utils/format.js, because Compose appends the part of the path after ./src to the target. The app uses it only if the container's process notices and reloads the code (for example through nodemon), and if something actually requires or imports the new file.
Exercise
After saving a change, docker compose exec web cat /app/src/server.js shows your new code, but the browser still shows the old behavior. Which half of the process failed, and what are two possible fixes?
Show answer
The Compose half succeeded, since the file reached the container. The reload half failed, because the running process still has the old code in memory. Fixes: run the app under a reloader such as nodemon, or change the rule to sync+restart so Compose restarts the service after copying.
Exercise
Why is docker compose watch not enough to update a service that is defined with only image: nginx and no build section?
Show answer
Watch rules apply only to services built by Compose. A service that uses only image: has no build context to relate the watched files to, so it is skipped.