Sub topic 2
Bind mounts versus Compose Watch
This chapter assumes you can read a Compose file and know what images, containers and volumes are. You should also know how a Dockerfile produces layers. The previous chapter covered why rebuilding and restarting by hand slows you down. This chapter compares the two main ways to get host edits into a running container: bind mounts and Compose Watch.
Two ways to get your edits into a container
Both approaches aim at the same result. You save a file on your host, and the running container sees the new version without a manual docker compose build and docker compose up. They get there by different mechanisms:
- A bind mount makes a host directory appear inside the container at a path you choose. The container reads the host's files directly. Nothing is copied.
- Compose Watch observes paths on the host. When a file changes, Compose copies it into the container, rebuilds the image, or restarts the service, depending on the rule you wrote.
This difference explains most of the trade-offs below.
How bind mounts work
A bind mount is declared in the service's volumes list. The left side is a host path and the right side is the container path.
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- ./src:/app/srcThe container's /app/src is now the same directory as ./src on your host. The image's own /app/src directory, whatever the COPY instruction put there, is hidden behind the mount while the container runs. An edit on the host is visible in the container immediately, because it is one set of files.
This is simple and has no extra tooling. It also has well-known problems.
Problems with bind mounts
The mount hides what the image contains. If you mount your whole project at /app, you cover the node_modules directory that npm install created in the image. The container then sees the host's node_modules, which may be empty or missing. A common workaround is to add a second, anonymous volume so the image's directory wins:
services:
web:
build: .
volumes:
- .:/app
- /app/node_modulesThis works, but it is a trick you must remember, and the anonymous volume keeps stale dependencies after you change package.json. A new dependency does not appear until you remove the volume and rebuild.
Host and container may not match. If the host's dependencies were installed on macOS or Windows, native modules built there will not run in a Linux container. Mounting them in produces errors that look unrelated to Docker.
File ownership and permissions can differ. On Linux, files created by the container in a bind-mounted directory are often owned by root on the host. Files you create on the host may be unreadable to a non-root user in the container.
Performance depends on the platform. On Linux, a bind mount is a direct view of the host filesystem and is fast. On Docker Desktop for macOS and Windows, the Docker engine runs in a virtual machine, so file access crosses a sharing layer. Projects with many small files, such as a large node_modules tree, can become noticeably slower.
Change notifications may not arrive. Many dev servers use file system events to detect edits. Events do not always cross the sharing layer reliably, so some tools need a polling mode switched on.
How Compose Watch works
Compose Watch moves the host-to-container step into Compose itself. You describe rules in a develop.watch section of the service, and Compose acts when a matching file changes.
services:
web:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.jsonYou start it with docker compose up --watch, or with docker compose watch after the services are running. The details of the syntax and each action are covered in later chapters. For this comparison, notice three things:
- The container keeps its own filesystem.
/app/node_modulescomes from the image and is never replaced by host files. - Only the paths you list are watched. Everything else stays as the image built it.
- A change to
package.jsontriggers a rebuild, so the image gets new dependencies. A bind mount cannot do that.
Because Compose Watch can rebuild images, it requires the service to have a build section. A service that only uses image: has nothing to rebuild, so Watch cannot manage it. The next chapters cover this requirement.
Compose Watch needs a recent Docker Compose release (2.22 or later). Check yours with docker compose version. Some actions arrived in later releases, so an older version may reject a rule that a newer one accepts.
What happens on a file change
The flowchart below shows the two paths for the same edit.
With a bind mount, the dev server must notice the change itself. With Compose Watch, the copy happens inside the container's filesystem. The container's own file system events fire normally, which avoids the notification problems that cross a VM boundary.
Side-by-side comparison
| Aspect | Bind mount | Compose Watch |
|---|---|---|
| How files arrive | Host directory is shared into the container | Compose copies files, rebuilds, or restarts |
| Container filesystem | Mount hides the image's directory at that path | Image filesystem is kept, with files copied over it |
| Dependency changes | Not handled. Needs a manual rebuild | `rebuild` action rebuilds the image |
| Config read only at startup | Needs a manual restart | `sync+restart` restarts the service |
| Platform-specific files | Host files can break the container | Container keeps its own dependencies |
| Selection of files | Whole directory is shared | Only watched paths, with `ignore` and `include` |
| Requires `build` section | No | Yes |
| Works with `image:`-only services | Yes | No |
| Extra setup | One `volumes` line | A `develop.watch` section and a watch command |
| Direct write-back to host | Yes. Container writes appear on the host | No. Copying goes from host to container only |
The last row matters. With a bind mount, a container that writes into the mounted directory changes your host files. With Watch, the flow is one way. A tool inside the container that generates files, such as a code generator or a migration tool, will not write them back to your host.
Choosing between them
Neither option is always better. Use these criteria.
Prefer a bind mount when:
- The service uses a prebuilt
image:with nobuildsection. - The container must write files back to the host, for example a code generator or a database data directory you want to inspect.
- You run Linux, where bind mount performance is good, and your tooling already works.
- The files are static assets or documents with no dependency or build step.
Prefer Compose Watch when:
- Your project has a dependency manifest and you want the image rebuilt when it changes.
- You develop on macOS or Windows and bind mount speed or file events are a problem.
- Host-built dependencies differ from container dependencies.
- You want different responses for different files: copy source, restart on config, rebuild on manifest.
Using both together
The two are not exclusive. A service can have bind mounts for some paths and watch rules for others, as long as the same path is not handled by both. If a directory is bind-mounted, the container already sees every change in it. Adding a sync rule for the same directory does nothing useful and can create confusing behavior. Pick one mechanism per path.
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- ./logs:/app/logs
develop:
watch:
- action: sync
path: ./src
target: /app/src
- action: rebuild
path: package.jsonHere ./logs is bind-mounted so you can read the application's log files on the host. ./src is handled by Watch. The two paths do not overlap.
Common mistakes when moving from bind mounts
- Keeping the old mount on the same path. If
./srcis still bind-mounted after you add a watch rule for it, the rule is redundant. Remove the mount. - Expecting writes to return to the host. Watch copies one way. If you need generated files on your host, keep a bind mount for that directory.
- Forgetting the build section. A rule on a service with no
buildcannot rebuild it. Compose will report the problem when you start watch mode. - Starting the stack without watch. Rules in
develop.watchdo nothing under a plaindocker compose up. You must pass--watchor rundocker compose watch.
Summary
A bind mount shares a host directory with the container. It is simple, works with any image, and allows writes back to the host, but it hides image contents, can bring in incompatible host files, and may be slow or miss file events on macOS and Windows. Compose Watch keeps the container's filesystem and acts on each change with a rule: copy it, rebuild the image, or restart the service. It needs a build section and a running watch command. Choose per path, and do not apply both mechanisms to the same directory.
Exercise
A Node.js service mounts the whole project with - .:/app. After you add a new package to package.json and restart the container, the app fails with "Cannot find module". The image was built before the change. Give two reasons the bind mount contributes to the problem, and name the Compose Watch rule that avoids it.
Show answer
First, the bind mount covers the image's /app directory, including the node_modules that npm install created, so the container sees the host's node_modules instead (which may be empty or built for another platform). Second, a bind mount never rebuilds the image, so even if you add an anonymous volume to preserve the image's node_modules, the image still lacks the new package until you rebuild by hand. A develop.watch rule with action: rebuild and path: package.json rebuilds the image and recreates the service whenever the manifest changes.
Exercise
A service has build: . and you want to read the application's generated report files from ./reports on the host, while source files in ./src reload live. Which mechanism do you use for each directory, and why?
Show answer
Use a bind mount for ./reports, because Compose Watch only copies from host to container and generated files would never reach the host. Use a sync watch rule for ./src (with target: /app/src), so the container keeps its own dependencies and the paths do not overlap.