Sub topic 2
How Path and Target Map Host Files into the Container
This chapter assumes you know Docker images, containers, Dockerfiles, and basic Compose services. It also assumes your web project has source files and a dependency manifest such as package.json.
Path and target mapping
A Compose Watch rule connects a host path to a path inside the container:
pathis the file or directory on your computer that Compose watches.targetis the destination path inside the container.- The paths are evaluated relative to the directory containing
compose.yaml.
For example, ./src refers to the host project's src directory. /app/src refers to the directory inside the container.
The target must match the location your application actually reads. If the application runs from /app but you sync files to /workspace/src, the container may continue using old files.
Syncing source files
Use the sync action for files that the application can use immediately after they are copied. Source code is the usual example.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/When a file under src changes, Compose copies the changed file into /app/src. The image is not rebuilt, and the container is not restarted. Your development server must detect the changed file, or you must configure a sync+exec or sync+restart rule instead.
ignore prevents unsuitable files from being copied. Excluding node_modules avoids syncing host-installed dependencies that may be incompatible with the container's operating system.
> callout tip > A sync rule does not create a missing application process or reload logic. Confirm that the container's command watches /app/src and that the target is the directory used by that process.
Rebuilding on dependency changes
Use rebuild when a change affects the image or installed dependencies. A dependency manifest is a common example:
develop:
watch:
- action: rebuild
path: package.jsonCompose rebuilds the service image, then replaces the running container. This works well when the Dockerfile copies package.json before application source and runs the package manager during the image build.
COPY package*.json ./
RUN npm install
COPY . .Source changes use sync; dependency changes use rebuild.
Exercise
A change to src/routes.js appears only after manually rebuilding. Which rule should you check first?
Show answer
Check that path: ./src targets the directory the application reads, such as /app/src, and that the service is running in watch mode.