Sub topic 1
Why the development loop feels slow
Every web developer has a rhythm: change a line, look at the result, change another. This chapter explains why that rhythm breaks down when the application runs in a container. It also identifies which costs Docker Compose Watch removes and which ones it leaves in place. Later chapters cover the watch configuration itself.
Prerequisites
This chapter assumes you already have the following:
- Basic Docker concepts: images, containers, and Dockerfiles.
- Experience writing a simple Compose file with
services,build,ports, andcommand. - Comfort running commands in a terminal.
- A web application project with source files and a dependency manifest. The examples use a Node.js project with a
package.json, but the ideas apply to any stack.
You do not need to know anything about develop.watch yet.
The loop you want
When you run an application directly on your machine, a development tool such as a file watcher or a hot-reload server notices that a file changed and refreshes the running program. The cycle looks like this:
- Edit a file and save it.
- The tool reloads the code.
- Check the result in the browser.
The delay between step 1 and step 3 is usually well under a second. That speed lets you test small ideas, so you make small changes and see their effects right away.
The loop you get in a container
A container runs from an image, and an image is a snapshot taken at build time. The code inside it does not follow your working directory. If you edit src/app.js on the host, the container still runs the old copy of that file, because the copy was made when the image was built.
Without extra tooling, a change reaches the running application through a manual sequence:
- Save the file on the host.
- Rebuild the image with
docker compose build. - Stop and remove the old container, then create a new one from the new image.
- Wait for the application process to start.
- Return to the browser and reload.
You can combine steps 2 and 3 into one command:
docker compose up --buildThat is shorter to type, but it does the same work. The loop is slow because each step costs time, and the costs add up on every edit.
Where the time goes
The delay has three separate sources. Knowing which one dominates in your project tells you which fix will help.
Image build time
Docker builds an image from a Dockerfile one instruction at a time. Each instruction that changes the filesystem produces a layer. Docker caches layers and reuses a cached layer when neither the instruction nor the files it depends on have changed. Once one layer is invalidated, every layer after it is rebuilt.
Instruction order therefore decides how much work a small edit causes. Consider this Dockerfile:
FROM node:20-alpine
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]COPY . . copies the whole project, including your source files, before the dependencies are installed. Any source edit changes the content of that layer, so the cache is invalid for RUN npm install and everything after it. Every one-line change reinstalls every dependency.
The version below separates the two concerns:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]Only the manifest files are copied before npm install. If package.json and the lock file have not changed, Docker reuses the cached install layer. A source-only edit invalidates only the final COPY . . layer, so the build finishes much faster.
This ordering is the single most useful Dockerfile habit for development speed, and you should adopt it whether or not you ever use Compose Watch. It also matters later in this tutorial, because the later chapters on rebuild assume that rebuilds are cheap when only dependencies change.
Container replacement
Even a fully cached build is not free. Docker must still create a new container, stop the old one, and swap them. Replacing a container means:
- The old process receives a stop signal and has to shut down. If it ignores the signal, Docker waits for the stop timeout, which defaults to 10 seconds, before killing it.
- Anything held in memory is lost, such as caches, open connections, and sessions.
- Anything written to the container's own filesystem is lost, because the new container starts from the image again.
- Networks and volumes are reattached.
The last two points explain why data you want to keep during development belongs in a named volume and not in the container's writable layer.
Application startup
After the container starts, the process inside it has to start too. A framework may compile code, load configuration, open database connections, and warm caches. For a large application this can take longer than the build. A hot-reload server on your host avoids this cost by patching a running process. A container replacement pays it every time.
The table below summarizes the three costs and what influences each.
| Cost | When it is paid | What reduces it |
|---|---|---|
| Image build | Every rebuild | Order Dockerfile instructions from least to most frequently changed |
| Container replacement | Every rebuild | Avoid replacing the container when a file copy would do |
| Application startup | Every new container | Use a dev server that reloads code in place |
What a hand-rolled fix costs
Developers often respond to the slow loop by working around it. Two workarounds are common.
The first is to run the application on the host and use Docker only for supporting services such as a database. This gives you fast reloads, but your host now needs the right runtime version and tools. Teammates may get different results, and the application no longer runs in the environment you ship.
The second is to mount the source directory into the container as a bind mount, so the container sees host files directly. A Compose file for that looks like this:
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- ./:/appThis removes the rebuild for source edits, and it works for many projects. It also has a cost. The mount replaces /app in the container with the host directory, which hides anything the image placed there. In the Node.js example, that includes the node_modules directory that npm install created during the build. If your host runs a different operating system, native dependencies compiled on the host may not run in the container. File system event delivery and read speed can also vary on some platforms. The next chapter compares bind mounts and Compose Watch in detail, so this chapter only notes that the workaround trades one problem for others.
What Compose Watch changes
Compose Watch is a feature of Docker Compose that you configure under a develop.watch section of a service. Compose observes files on the host. When a file in a watched path changes, Compose applies an action you chose:
- Copy the changed file into the running container.
- Rebuild the image and replace the service.
- Copy the file and restart the process in the same container.
- Copy the file and run a command inside the container.
The purpose is to apply the cheapest correct update for each kind of change. Editing a source file needs only a copy, with no build and no new container. Changing a dependency manifest needs a rebuild, because new packages must be installed. A configuration file read only at startup needs a copy and a restart. Running these decisions automatically removes the manual steps from the loop above.
Watch does not eliminate every cost. Rebuilds still take time. The application process still has to be able to pick up the copied file, which means it needs its own reload mechanism, such as a dev server, if you want a change to appear without any restart. Compose Watch moves files. It does not make the program inside the container reload itself.
A quick way to measure your own loop
Before you change anything, time the loop you have. Run a build after a source-only edit and again after a manifest edit:
# After editing only a source file
time docker compose build
# After editing package.json
time docker compose buildThe first number shows how well your Dockerfile uses its cache. If it is close to the second, your instructions are probably ordered so that source edits invalidate the install step. Then time how long the application takes to answer its first request after docker compose up. These two numbers tell you whether the build or the startup is the bigger cost for your project. They give you a baseline to compare against once watch rules are in place.
What comes next
The next chapter compares bind mounts with Compose Watch in more detail. After that, the tutorial shows how to add a develop.watch section and how to start it. The chapters that follow cover each action in turn, then filtering, choosing among actions, and troubleshooting.
Exercise
A Dockerfile contains COPY . . followed by RUN npm install. You change one line in a source file and rebuild. Why does npm install run again, and how would you reorder the instructions to avoid it?
Show answer
COPY . . creates a layer whose content includes your edited source file, so its cache entry is invalid. Docker rebuilds every layer after an invalid one, including RUN npm install. To avoid this, copy only package*.json first, run npm install, and then run COPY . .. A source-only edit then invalidates only the last copy layer, and the install layer comes from the cache.
Exercise
Name the three sources of delay in the manual rebuild-and-restart loop, and say which of them a cached build still pays.
Show answer
The three sources are image build time, container replacement, and application startup. A fully cached build removes most of the first, but the second and third are still paid, because Docker must still replace the container and the process must start again.
Exercise
Your team replaces the rebuild with a bind mount of ./ onto /app. After the change, the container fails because node_modules is missing, even though the image installed it. Explain why.
Show answer
The bind mount covers /app in the container with the host directory. The node_modules folder that npm install created in the image at /app/node_modules is hidden behind the mount. If the host directory has no node_modules, or has one built for a different operating system, the application cannot find working dependencies.