Sub topic 2
Bind mounts versus Compose Watch
This chapter assumes you know what images, containers and Dockerfiles are, and that you can write a basic Compose file with build and ports. It compares the two ways to get host file changes into a running container, so you can pick one on purpose.
The problem both tools solve
Without help, each source edit needs a rebuild of the image and a restart of the container. Bind mounts and Compose Watch both skip that step. They differ in how the file reaches the container.
Bind mounts share the directory
A bind mount maps a host path to a container path. The container reads the host's files directly, so no copy exists.
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- ./src:/app/srcAn edit to ./src/index.js is visible inside the container immediately. If the app runs a file watcher such as nodemon, it restarts itself.
Bind mounts have costs:
- Shadowing. A mount hides whatever the image had at that path. If you mount the whole project over
/app, the host'snode_modulesreplaces the one built into the image. Host binaries compiled for macOS or Windows then fail on Linux. - Performance. On Docker Desktop, file access across the host-to-VM boundary is slower than native access. Projects with many files feel it.
- Ownership. Files written by the container can end up owned by root on the host.
- No dependency handling. Changing
package.jsondoes nothing until you rebuild by hand.
Compose Watch copies files
Compose Watch uses rules under develop.watch. Compose monitors the host paths and applies an action when a file changes. For a sync action, it copies the changed file into the container. The container keeps its own filesystem.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.jsonHere a source edit is copied into /app/src. An edit to package.json rebuilds the image and recreates the container. The image's own node_modules is never shadowed.
Side-by-side comparison
| Aspect | Bind mount | Compose Watch |
|---|---|---|
| Mechanism | Shared directory | Copy, or rebuild, triggered by rules |
| Host `node_modules` shadows the image's | Yes, if you mount the project root | No |
| Dependency changes | Manual rebuild | `rebuild` action |
| Filtering | Mount only the paths you want | `ignore` and `include` patterns |
| Container writes appear on host | Yes | No, the flow is one way |
| Requires `build` section | No | Yes |
Choosing between them
Prefer Compose Watch when:
- Your dependencies are installed in the image and platform-specific.
- You want dependency changes to trigger a rebuild automatically.
- Bind-mount file performance is a problem on your machine.
Prefer a bind mount when:
- The container must write files back to the host, such as generated code or build output you want to inspect.
- The service uses an
image:only, with nobuildsection. - The tool inside the container already handles reloading, and you only need the files to be present.
The two can coexist in one project. You might bind-mount a directory of generated output while watching source files. Avoid pointing both at the same path, because two mechanisms writing the same files makes it unclear which version the container sees.
The next chapters cover the develop.watch syntax in detail.
Exercise
A team bind-mounts the whole project directory over /app in a Node.js service. The app starts on Linux containers but crashes with a native module error. The developers use macOS. What is the likely cause, and how would Compose Watch avoid it?
Show answer
The mount hides the image's node_modules, which was installed for Linux, and replaces it with the host's node_modules, which holds macOS binaries. Compose Watch copies only the paths named in its rules, and ignore: - node_modules/ keeps the host's copy out of the container. The image's Linux-built dependencies stay in place. A rebuild rule on package.json handles dependency changes.
Exercise
Your service writes generated report files to /app/reports, and you want to open them on the host. Would you use Compose Watch or a bind mount for that path, and why?
Show answer
A bind mount. Compose Watch only moves changes from the host into the container, so files created inside the container never appear on the host. A bind mount shares the directory in both directions.