Sub topic 1
Why the Development Loop Feels Slow
Prerequisites
This chapter assumes you know basic Docker concepts, can write a Compose service with build and ports, and can run terminal commands. You should also understand that an image is a packaged filesystem and a container is a running instance of that image.
Rebuilding and Restarting Takes Time
A common development loop is:
- Edit source code on the host.
- Rebuild the image.
- Stop and recreate the container.
- Test the change.
This repeats dependency installation and container startup even when only one source file changed. Docker Compose Watch shortens this loop by copying selected host changes into an already-running container.
Bind Mounts Versus Compose Watch
A bind mount maps a host directory directly into a container. It is simple and useful for source code, but it can hide files created in the image at the mount location. File behavior can also differ across operating systems.
Compose Watch copies changes selectively. You choose the host path, container target, and response to each change. This keeps dependency directories such as node_modules inside the container and avoids syncing unsuitable files.
Adding `develop.watch` to a Service
Add watch rules to the service that has a build section:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/The path is relative to the Compose project directory. The target is the matching path inside the container. A source-only change uses sync, which copies files without rebuilding the image.
Preparing the Dockerfile and Build Section
The image must have the target directory and application setup expected by the watch rule. Copy dependency manifests before application source so dependency installation remains in a reusable build layer.
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]Starting Watch Mode
Start services and watch together:
docker compose up --watchOr start services first and attach watch mode:
docker compose up -d
docker compose watchExercise
A change to src/app.js does not appear in the container. What should you check first?
Show answer
Check that the service has develop.watch, that path: ./src points to the edited file, that target matches the application's directory, and that watch mode is running.