Sub topic 5
Bind mounts for development workflows
A bind mount maps a directory or file on the host into a container. The container sees the host's files directly, with no copy. Edits on either side show up on the other side immediately. This makes bind mounts the usual tool for development, where you edit code on your machine and the container runs it.
This chapter assumes you can run containers with docker run, know the difference between an image and a container, and have met named volumes in the previous chapters.
How a bind mount differs from a volume
A named volume is created and managed by Docker, and its location on the host is an implementation detail. A bind mount points at a path you choose. Docker does not create, track, or clean it up like a volume, and docker volume ls does not list it.
| Property | Named volume | Bind mount |
|---|---|---|
| Host location | Managed by Docker | Any path you give |
| Listed by `docker volume ls` | Yes | No |
| Typical use | Database files, persistent state | Source code, config files |
| Host files visible to you | Not convenient | Yes, in your editor |
A worked example
Create a directory with a page, then serve it with nginx.
mkdir -p app && echo '<h1>hello</h1>' > app/index.html
docker run --rm \
--mount type=bind,source="$(pwd)/app",target=/usr/share/nginx/html,readonly \
-p 8080:80 nginx:alpineOpen http://localhost:8080 and you see "hello". In another terminal, change the file:
echo '<h1>hello again</h1>' > app/index.htmlReload the page. The new text appears without restarting the container, because nginx reads the same file you just edited.
The readonly option means the container cannot change the files. Writing from inside the container fails, while your edits on the host still work. Use it whenever the container only needs to read the data.
Rules and common errors
- The source must be an absolute path.
-v ./app:/datais not treated as a path. Docker reads./appas an invalid volume name. Use"$(pwd)/app"or${PWD}/app. --mountfails if the source is missing. You get an error such asbind source path does not exist. The-vsyntax silently creates the missing path as a directory, usually owned by root. That is a common source of confusing results, such as a directory appearing where you expected a file.- A bind mount hides what the image has at the target. If the image contains files in
/usr/share/nginx/html, they are not visible while the mount is active. They are not deleted. Unlike a new empty named volume, a bind mount is never filled from the image. - Ownership comes from the host. Files keep the numeric user and group IDs of the host. If the container process runs as a different user, it may not be able to write. A later chapter covers fixing this.
Typical development uses
- Live code reload. Mount your project into a container that runs a dev server with a file watcher, such as
nodemonor a framework's watch mode. - Config files. Mount a single file, for example
-v "$(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro", to try a configuration without rebuilding an image. - Build output. Mount an output directory so a build container writes artifacts to your machine.
On Docker Desktop for macOS and Windows, bind mounts cross a virtual machine boundary. Large directories, such as node_modules, can be slow. Keep heavy dependency directories inside the image or in a named volume.
Exercise
You run docker run -v app:/usr/share/nginx/html nginx:alpine expecting to serve the host's ./app directory, but nginx shows its default page. Why, and how do you fix it?
Show answer
Without a leading / or ./ path that is absolute, app is treated as the name of a named volume, not a host path. Docker created an empty volume named app, which was filled with nginx's default files. Use an absolute path, such as -v "$(pwd)/app:/usr/share/nginx/html", or --mount type=bind,source="$(pwd)/app",target=/usr/share/nginx/html.