Sub topic 8
Rebuilding on dependency changes
This chapter covers the rebuild action in Docker Compose Watch. It assumes you can read a Dockerfile and a Compose file, and that you know how develop.watch and the sync action work.
Why some changes need a rebuild
The sync action copies a file into a running container. That works for source code, because the files are already in the right place in the image. A dependency change is different. If you add a package to package.json, the new package is not installed in the container. Copying the file does nothing useful, because npm install has to run again.
The rebuild action handles this case. When a watched file changes, Compose rebuilds the image and recreates the service container from the new image.
Adding a rebuild rule
Here is a Compose service that syncs source files and rebuilds when package.json changes.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.jsonA rebuild rule needs only action and path. It has no target, because nothing is copied into the container. The path is relative to the project directory, the folder that holds the Compose file.
What happens during a rebuild
When package.json is saved, Compose runs the equivalent of a build for that service. It then replaces the running container with a new one. Other services are not touched.
Keep the rebuild fast with layers
A rebuild is only as fast as your Dockerfile allows. Docker caches each step as a layer. If a layer and every layer before it are unchanged, Docker reuses the cached result.
This Dockerfile copies the dependency manifest first and runs the install in its own layer.
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]Consider two cases:
- You edit
src/index.js. TheCOPY package*.jsonlayer is unchanged, so the cachednpm installlayer is reused. OnlyCOPY . .runs again. - You edit
package.json. The manifest layer changes, sonpm installruns again. This is the rebuild you want.
If you wrote COPY . . before RUN npm install, every source edit would invalidate the install layer. Each rebuild would then reinstall all packages.
Which files to rebuild on
Use rebuild for files that the image build reads and that cannot be fixed by copying a file. Common examples:
| File | Why it needs a rebuild |
|---|---|
| `package.json` | New or changed packages must be installed |
| `package-lock.json` | Pinned versions changed |
| `requirements.txt` | Python packages must be installed |
| `Dockerfile` | The build instructions themselves changed |
You can list several rebuild rules, or one rule per file. Each rule has its own path.
Common errors
- The rebuild never runs. The service must have a
buildsection. Compose cannot rebuild a service that only has animageentry. - The path does not match.
path: package.jsonis relative to the project directory. If your manifest lives in a subfolder, such as./api/package.json, write that path. - Rebuilds run too often. If a rule watches a whole directory that includes generated files, every generated file triggers a build. Narrow the
pathor addignorepatterns. - Changes seem lost after a rebuild. Files you copied in with
syncexist only in the old container. The rebuilt image comes from files on the host, so keep your edits in the host project and not inside the container.
Check your understanding
Exercise
Your Dockerfile has COPY . . on the line before RUN npm install. Why does every source edit make a rebuild slow, and how do you fix the Dockerfile?
Show answer
Any source edit changes the output of COPY . ., which invalidates the cache for every later layer, including RUN npm install. Fix it by copying only package*.json first, running RUN npm install, and then running COPY . .. Then the install layer is rebuilt only when the manifest changes.
Exercise
Why does a rebuild rule have a path but no target?
Show answer
A rebuild does not copy the file into the running container. It rebuilds the image from the project files and recreates the container, so there is no destination path to give.