HOWTO · Docker

How to Mount Host Directory Into a Docker Container

This tutorial demonstrates how to mount host directory into a Docker container.

On this page

A bind mount connects a directory on your host to a path inside a Docker container. Use it when a container needs to read or update files in a specific host directory, such as source code during development. The example below prepares a host directory and file; the Docker commands show how to expose that directory inside an Alpine shell. The Docker commands are illustrative and were not executed during this writing stage.

Use Bind Mounts to Mount Host Directory Into a Docker Container

A bind mount exposes the host path you choose at a container path. Unlike a Docker-managed volume, its source is a particular location on the host. Changes made to files in that directory are visible through the container mount point. An application sees those changes, but hot reload happens only if that application watches or reloads its files.

Mount a host directory with -v or --volume

This Python example creates a host directory named site, writes a file into it, and prints the file name. Run it from the directory where you will start Docker:

from pathlib import Path

site = Path("site")
site.mkdir(exist_ok=True)
(site / "hello.txt").write_text("Hello from the host.\n")
print("\n".join(sorted(path.name for path in site.iterdir())))
hello.txt

The host-side file is ready to inspect at /site in a container started with docker run --rm -it --volume "$PWD/site:/site" alpine:3.20 sh. The volume value has the form host-path:container-path[:options]: $PWD/site is the host directory, /site is where it appears in the container, and the optional ro suffix makes the mount read-only. The short -v flag and long --volume flag use the same syntax, so -v "$PWD/site:/site:ro" is the read-only equivalent.

While the container shell is running, create a file on the host with touch "$PWD/site/hello.txt". At the container prompt, use ls /site to inspect the files exposed by the mount. Files created under /site are also written to the host directory when the mount is writable. With a read-only mount, the container can read the files but cannot change them through that mount.

The -v and --volume forms create a missing host directory as a directory. Check the resulting host path before using the container, since a typo can create a new, empty directory and make it look as though the expected files are missing.

Mount a host directory with --mount

The --mount form names each setting as a comma-separated key-value field. For the same example, run docker run --rm -it --mount "type=bind,source=$PWD/site,target=/site" alpine:3.20 sh. type=bind selects a bind mount, source is the host directory, and target is its path in the container. Add readonly to the mount fields to prevent writes through it. Field order does not determine which path is the source or target; the keys do.

Unlike -v and --volume, --mount reports an error if its bind-mount source does not exist. Create and verify the host directory before starting the container. For paths containing commas, the comma-separated mount syntax needs additional care; choose a suitable host path or consult Docker’s mount syntax for your platform.

A bind mount gives the container access to the selected host path. Mount only directories the workload needs, avoid exposing sensitive files, and use read-only access when the container does not need to write. Host path sharing and permissions can also depend on how Docker is configured on your system.

Conclusion

Use -v or --volume for the compact colon-separated form, or --mount for explicit key-value fields. Both make a host directory available at a container path; -v can create a missing source directory, while --mount requires the source to exist. Choose a bind mount when the container needs a particular host path. A Docker-managed volume is often a better fit for application data that Docker should manage independently of a specific host directory.