Sub topic 1
Why the development loop feels slow
Prerequisites
This chapter assumes you know the following:
- Basic Docker concepts: images, containers, and Dockerfiles.
- How to write a simple Compose file with
services,build, andports. - How to run commands in a terminal.
- A web application project with source files and a dependency manifest such as
package.json.
The manual loop
A development loop is the cycle of editing code, running it, and checking the result. Without help, a containerized app makes each pass through that cycle longer than it needs to be. After every edit you repeat the same steps:
- Stop the running container.
- Rebuild the image.
- Start a new container from the new image.
- Reload the app and look at the result.
Each step is short, but together they add up to seconds or minutes per edit. Over a day of small changes, that is a lot of waiting.
The solid path is the manual loop. The dashed path is what Compose Watch gives you, and later chapters build it step by step.
Why a rebuild costs time
An image is built in layers. Docker caches each layer and reuses it only if the instruction and the files it depends on have not changed. Once one layer changes, every layer after it is rebuilt.
Look at this Dockerfile:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]The dependency manifest is copied and npm install runs before the source code is copied. This ordering matters:
- If you change only a file under
src/, Docker reuses the cachednpm installlayer. OnlyCOPY . .and later layers run again. - If you change
package.json, the cache is invalidated atCOPY package*.json ./. The install runs again, which is the slowest step.
Good layer ordering reduces the cost of a rebuild. It does not remove the rebuild, the container stop, or the new container start.
What else you lose on restart
Replacing the container also costs more than build time:
- The application process starts from scratch, including any slow startup work such as connecting to a database or warming a cache.
- Any state held in memory, such as a login session, is lost.
- Logs from the previous container are no longer attached to your terminal in the same way.
For a small edit, such as changing the text of a heading, this is a heavy price.
What a better loop needs
The goal is to get your edit into the running container with the least work possible. Different files need different treatment:
| Kind of change | What is really needed |
|---|---|
| Source file read on each request | Copy the file into the container |
| Source file for a framework that does not reload itself | Copy the file, then restart the process |
| Dependency manifest | Reinstall dependencies, which means a rebuild |
A single fixed behavior cannot serve all three cases. Copying is fast but is not enough when dependencies change. A full rebuild is always correct but is slow when only one source file changed. A good tool lets you choose per path.
The next chapters compare the common approach, bind mounts, with Compose Watch, and then show how to declare these per-path rules in your Compose file.
Exercise
You change one line in a file under src/. Your Dockerfile copies package*.json and runs npm install before COPY . .. Which layers are rebuilt, and why is the install step not among them?
Show answer
Only the COPY . . layer and the layers after it are rebuilt. Docker reuses the cached npm install layer because neither the package*.json files nor the earlier instructions changed. The container still has to be stopped and replaced, so the edit is not yet instant.
Exercise
You add a new package to package.json. Why can no copy-only approach pick up this change?
Show answer
The new package must be installed with npm install, and the installed files live in the image. Copying package.json into the container does not install anything, so the image has to be rebuilt.