Sub topic 8
Rebuilding on dependency changes
Some changes cannot be copied into a running container. A new package in package.json needs npm install to run, and that happens at image build time. The rebuild action in Compose Watch handles this case. When a watched file changes, Compose rebuilds the service image and recreates the container from it.
This chapter assumes you can read a Dockerfile and a Compose file with build and ports. It also assumes you know the develop.watch section and the sync action from earlier chapters. You should know that a layer is a cached build step.
What the rebuild action does
A rebuild rule names a path to watch. When a file at that path changes, Compose does the following:
- It runs the service's build again, using the
buildsection from your Compose file. - It creates a new container from the new image.
- It replaces the old container with the new one.
Only the affected service is rebuilt. Other services keep running.
The effect is the same as running docker compose up --build web, but you don't have to type it. The rule is also narrow. You pick which files trigger it, so a change to a CSS file does not cause a rebuild.
A rebuild rule needs only action and path. It has no target, because nothing is copied to a destination. The new image is built from your project directory.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: rebuild
path: package.jsonThe path is relative to the project directory, which is the folder that holds the Compose file. It can name a single file, as above, or a directory.
Why the Dockerfile layout matters
A rebuild is only fast if Docker's build cache is used well. Docker reuses a cached layer until a step or the files it copies change. After the first changed layer, every later layer is rebuilt.
Here is the Dockerfile from the plan:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]The order of the lines is deliberate.
COPY package*.json ./copies only the dependency manifest and lockfile.RUN npm installdepends only on those files. If they have not changed, Docker reuses the cached layer and skips the install.COPY . .comes after the install. A source change invalidates only this layer and the ones after it.
The result is that a rebuild triggered by package.json reruns npm install, while a rebuild for any other reason reuses the cached install. If you wrote COPY . . before RUN npm install, every source change would invalidate the install layer. A rebuild would then reinstall all dependencies each time.
Choosing what triggers a rebuild
List every file whose change should produce a new image. For a Node.js project, that is usually the manifest and the lockfile:
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.json
- action: rebuild
path: package-lock.jsonBoth files are listed because npm install reads both. A change to the lockfile alone, such as an upgrade by npm update, should still produce a new image. If you watch only package.json, the lockfile can change on the host while the container keeps the old versions.
Other common rebuild triggers:
| Stack | Files that usually need a rebuild |
|---|---|
| Node.js | `package.json`, `package-lock.json` |
| Python | `requirements.txt`, `pyproject.toml`, `poetry.lock` |
| Any | `Dockerfile` |
| Compiled languages | Sources, when the build step produces the program |
The Dockerfile itself is a good candidate. If you change a RUN line or the base image, the running container is out of date until the image is rebuilt:
- action: rebuild
path: DockerfileCompiled languages are a special case. If your container runs a binary produced by RUN go build or a similar step, a source change needs a rebuild to take effect. Syncing source files into the container would have no effect. Expect slower feedback with this setup.
Starting watch and seeing a rebuild
Watch mode has two start commands:
# Start the services and watch for changes in one command
docker compose up --watch
# Or start the services first, then run watch separately
docker compose up -d
docker compose watchOnce watch is running, edit package.json on the host, for example by adding a dependency. Compose notices the change and prints a rebuild message. It then builds the image again and replaces the container.
You can install the package from the host with npm install express, which changes both manifest files. Do not run npm install inside the container for this purpose. That would change only the container's filesystem, and the next rebuild would discard it, because package.json on the host would not list the package.
What a rebuild replaces and what it keeps
A rebuild recreates the container, so you should know what survives.
- The container's writable filesystem layer is discarded. Files created inside the container at runtime are lost.
- Named volumes are kept. Data in a database volume survives the replacement.
- The image has the dependencies and source as of the build.
- Processes are restarted. In-memory state, such as sessions held in a Node.js process, is gone.
For a stateless web service, this is rarely a problem. Be careful if the service writes files into its own filesystem. Put that data on a volume.
Combining rebuild with sync
A common setup uses sync for frequently edited source and rebuild for rarely edited manifests. This keeps most edits fast and rebuilds only when needed.
Keep the paths of the two rules separate. In the earlier example, sync watches ./src and rebuild watches two files at the project root. A file never matches both. If you gave a sync rule and a rebuild rule overlapping paths, you would have to reason about which one applies. Avoiding overlap removes that question.
There is a subtle interaction with COPY . .. A rebuild copies the current host source into the image. So after a rebuild, the container has your latest source even for files that were never synced. That is helpful, but only if the build context contains the right files. A .dockerignore file controls the context:
node_modules
.git
npm-debug.logIf node_modules from the host were copied into the image by COPY . ., it could overwrite the dependencies that npm install just produced. This is a common source of "works in the container, breaks after rebuild" problems, especially when host and container operating systems differ and native modules are involved.
Ignoring files in a rebuild rule
Rebuild rules accept ignore like other actions. This matters when path is a directory. Suppose a Python project keeps its dependency files in ./deps, next to editor backups:
services:
api:
build: .
ports:
- "8000:8000"
develop:
watch:
- action: rebuild
path: ./deps
ignore:
- "*.bak"
- "*.swp"With this rule, saving a .swp file does not rebuild the image. Without it, a swap file written by your editor could start a build you did not intend.
Common problems
Nothing rebuilds when the manifest changes. Check that the service has a build section. Watch applies only to services built from a Dockerfile. A service that has only image: is skipped, because there is nothing to build. Also check that the path is spelled as it appears relative to the Compose file.
The rebuild runs but the new package is missing. Look at the Dockerfile. If the install step runs before the manifest is copied, or if the install step is cached incorrectly, the new image will not contain the package. Confirm that COPY package*.json ./ comes before RUN npm install.
Every rebuild takes minutes. Check for a COPY . . that appears before the install step. Also check that .dockerignore excludes node_modules and .git, which keeps the build context small.
Rebuilds fire repeatedly. Something is writing to a watched file. Build tools and package managers sometimes rewrite lockfiles. Narrow the path or add ignore entries.
Choosing rebuild or another action
Use rebuild when the change must be applied at image build time:
- A dependency manifest or lockfile changed.
- The Dockerfile changed.
- A build step compiles code the container runs.
Use sync when the running program can pick up the change by itself, such as source files with a reloading dev server. Later chapters cover sync+restart and sync+exec for the cases between these two.
A rebuild is the most expensive action, so give it the smallest set of paths that need it.
Exercise
A Python service uses COPY requirements.txt ., then RUN pip install -r requirements.txt, then COPY . .. You add a package to requirements.txt while docker compose watch runs. Write the rule that makes Compose rebuild the image, and say which Dockerfile step is rerun and which earlier steps are reused from the cache.
Show answer
The rule is - action: rebuild with path: requirements.txt under develop.watch. No target is needed. The changed file invalidates the COPY requirements.txt . layer, so that step and every step after it run again: RUN pip install -r requirements.txt and COPY . .. Steps before the copy, such as FROM and WORKDIR, are reused from the cache. The old container is then replaced by one built from the new image.
Exercise
A teammate edits package.json by hand, and the container does not get the new dependency. Their Compose file has a sync rule for ./src only. What is missing, and why does sync not help?
Show answer
There is no rebuild rule for package.json. The sync rule covers only ./src, so a change at the project root matches nothing. Even if sync covered the file, copying package.json into the container would not run npm install, so the dependency would still not be installed. A rebuild rule rebuilds the image, which runs the install step.