Sub topic 6
How path and target map host files into the container
This chapter covers the two attributes that decide where a watched change goes: path on the host and target in the container. It assumes you can write a Compose file with build, ports and command, and that you know what WORKDIR and COPY do in a Dockerfile. It also assumes you have seen a develop.watch rule and know that sync copies files into a running container. The sections below cover mapping, edge cases and common mistakes.
The two sides of a watch rule
Every watch rule has a host side and a container side.
pathis a location on the host. Compose resolves a relativepathagainst the project directory, which is the directory that holds the Compose file. It does not resolve against the directory where you ran the command.targetis a location inside the container. It applies to the actions that copy files:sync,sync+restartandsync+exec. Therebuildaction has notarget, because it builds a new image from the build context and never copies single files.
Here is a rule that uses both:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/srcCompose watches everything under ./src on the host. When a file changes, it copies that file to the matching place under /app/src in the running container.
How a changed file gets its destination
Compose does not copy the whole directory each time. It works out a destination for each changed file in three steps:
- Take the host path of the changed file, for example
./src/routes/users.js. - Remove the
pathprefix to get the relative part, hereroutes/users.js. - Append that relative part to
target, giving/app/src/routes/users.js.
The diagram shows the same steps.
Because the rule is a prefix substitution, path and target should usually name the same directory on each side. If the host has ./src and the container has /app/src, the rule is easy to read. It also matches where the Dockerfile put the files.
| Host path of rule | Target | Changed host file | Resulting container file |
|---|---|---|---|
| `./src` | `/app/src` | `./src/index.js` | `/app/src/index.js` |
| `./src` | `/app/src` | `./src/routes/users.js` | `/app/src/routes/users.js` |
| `./src` | `/app` | `./src/index.js` | `/app/index.js` |
| `./config` | `/app/config` | `./config/app.json` | `/app/config/app.json` |
The third row is a common mistake. The rule is valid, so Compose raises no error, but the file lands one level above where the application expects it. The container keeps running the old /app/src/index.js and ignores the new /app/index.js.
Making the mapping match the image
The target only makes sense if it matches the layout the Dockerfile creates. Look at the Dockerfile from the chapter on preparing the build:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]WORKDIR /app followed by COPY . . puts the project's src directory at /app/src and config at /app/config. The watch targets should copy those layout choices:
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.jsonThe package.json rule has a path but no target. For rebuild, Compose only needs to know which host file triggers the build.
Write target as an absolute path. A relative target depends on the container's working directory. That is easy to misjudge, and an absolute path leaves nothing to guess.
Paths that are directories and paths that are files
A path can name a directory or a single file.
- A directory is watched recursively. A change at any depth under it triggers the rule, and the nested position is kept in the container, as in the
routes/users.jsexample above. - A single file triggers the rule only when that file changes. For
syncrules, the usual form is to give the file's full container destination astarget. This is mainly useful for one configuration file that sits outside any watched directory.
services:
web:
build: .
develop:
watch:
- action: sync+restart
path: ./settings.json
target: /app/settings.jsonIf the file case behaves differently on your Compose version, check the result with the commands in the next section. Watch rules are still evolving between releases.
Deletions and new files
The mapping works for more than edits.
- A new file under
pathis copied to the matching place undertarget. New subdirectories are created as needed. - A file you delete on the host is deleted at the matching place in the container.
- A rename is a delete plus a create, so the old name disappears and the new name appears.
The reverse is not true. Nothing in the container is copied back to the host. If a process in the container writes a file under target, such as a generated bundle, the next sync of that path can overwrite it. Keep generated output out of the watched directories, or exclude it with ignore, which a later chapter covers.
Checking that the mapping works
You can test a rule without reading any logs. Start watch mode and change a file:
docker compose up --watchIn another terminal, edit a file on the host, then look at the container's copy:
echo "// touched" >> src/index.js
docker compose exec web tail -n 1 /app/src/index.jsIf the last line is // touched, the rule maps correctly. If it is not, list the directory you think the file went to:
docker compose exec web ls -la /app/src
docker compose exec web ls -la /appA file that appears in /app and not in /app/src means target is one level too shallow, as in the third row of the table.
Common mistakes
- Trailing differences. Treat
./srcand./src/as the same directory, and write them the same way on both sides so the rule is easy to read. - Wrong base directory. A relative
pathis relative to the project directory, not your shell's current directory. Runningdocker compose -f deploy/compose.yaml watchfrom elsewhere changes the project directory to the folder holding the Compose file. A path like./srcthen points atdeploy/src. - Target outside the image's layout. A target such as
/srccreates a new directory at the container root. Nothing in the image uses it. - Overlapping rules. Two rules whose
pathvalues overlap, such as./srcand./src/config, can both match the same change. Keep paths separate and give each file type one action. - A bind mount on the same directory. If you also mount
./srcinto/app/srcas a bind mount, the host and container already share the files, so the sync is redundant. Choose one mechanism for each directory.
Summary of the semantics
pathis a host location, relative to the project directory.targetis a container location, and only the copying actions use it.- The part of a changed file's path below
pathis appended totarget. - Directories are watched recursively, and created and deleted files follow the same mapping.
- The mapping should match the layout that
WORKDIRandCOPYbuild in the image.
Exercise
A service has WORKDIR /app and COPY . . in its Dockerfile. Its watch rule is action: sync, path: ./src, target: /app. You edit ./src/routes/users.js on the host. Where does the file appear in the container, and why does the application not pick up the change?
Show answer
It appears at /app/routes/users.js. Compose removes the ./src prefix, leaving routes/users.js, and appends that to /app. The application loads /app/src/routes/users.js, which was never updated. The fix is target: /app/src.
Exercise
Your Compose file is in deploy/compose.yaml, your source is in deploy/src, and you run docker compose -f deploy/compose.yaml watch from the repository root. What does path: ./src refer to, and why?
Show answer
It refers to deploy/src. Compose resolves a relative path against the project directory, which is the directory containing the Compose file, not the shell's current directory.
Exercise
Write a sync rule that copies changes under the host directory ./templates into /app/views in the container. Then say which container path receives a change to ./templates/mail/welcome.html.
Show answer
The rule is - action: sync, path: ./templates, target: /app/views. The change lands at /app/views/mail/welcome.html.