Sub topic 6
How path and target map host files into the container
This chapter assumes you know Dockerfiles, Compose services with build and ports, and basic terminal use. It covers how a watch rule's path and target decide where a changed host file ends up inside the running container.
What path and target mean
Each rule under develop.watch has a path. This is the location on the host that Compose monitors. Rules that copy files, such as sync, also need a target. This is the matching location inside the container.
pathis relative to the directory that holds the Compose file.targetis an absolute path inside the container.rebuildrules only needpath, because nothing is copied.
The Dockerfile's WORKDIR and COPY lines decide where your files live in the image. Your target must match that layout.
A worked example
Here is the Dockerfile from this tutorial's Node.js project:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]WORKDIR /app followed by COPY . . puts the project root at /app in the container. So ./src on the host becomes /app/src. The Compose rule states that mapping:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.jsonWhen you save ./src/routes/users.js, Compose copies it to /app/src/routes/users.js. The part of the path below the watched directory is kept.
How the mapping works
Compose takes the changed file's path, removes the path prefix, and adds the remainder to target.
| Host file | Rule path | Rule target | Container file |
|---|---|---|---|
| `./src/app.js` | `./src` | `/app/src` | `/app/src/app.js` |
| `./src/lib/db.js` | `./src` | `/app/src` | `/app/src/lib/db.js` |
| `./config/app.json` | `./config` | `/app/config` | `/app/config/app.json` |
A path can name a directory or a single file. A directory is watched recursively, so files in nested folders are included.
Choosing a target
Work out the target in two steps:
- Find the container directory that the Dockerfile's
COPYplaces your files in. In the example above, that is/app. - Append the same relative folder you used for
path.
If path is ./src and the image keeps the project at /app, the target is /app/src. The two sides should usually end in the same folder name. That keeps the mapping easy to read and check.
Common mistakes
- Target does not match the image layout. If the Dockerfile sets
WORKDIR /usr/src/appbut the rule says/app/src, Compose copies files to a folder your application never reads. The sync succeeds, but the app shows no change. - Relative target. Write
/app/src, notapp/srcor./src. A target is an absolute container path. - Wrong host path. A path is resolved from the Compose file's directory, not from your current shell directory. Check the path if you run Compose from a subfolder.
- Watching a folder you have not synced the right way. Copying a file only helps if the running process reads it again. Source files need a reload mechanism, and files read only at startup need a restart. Later chapters cover those actions.
Exercises
Exercise
A Dockerfile has WORKDIR /srv/site followed by COPY . .. You want changes in the host folder ./templates to appear in the container. Write the path and target for a sync rule, and say where ./templates/home/index.html ends up.
Show answer
Use path: ./templates and target: /srv/site/templates. The file ./templates/home/index.html is copied to /srv/site/templates/home/index.html, because the part of the path below ./templates is kept.
Exercise
Your sync rule uses path: ./src and target: /app/src, and you save ./src/app.js. The command finishes without errors, but the running app does not change. The Dockerfile uses WORKDIR /usr/src/app. What is wrong, and how do you fix it?
Show answer
The target does not match the image layout. The app reads files from /usr/src/app/src, but Compose copies the file to /app/src. Change the target to /usr/src/app/src.