How to mount a host directory in a Docker container
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When working with Docker, you might find it necessary to share or persist data between your host system and containers. A common method to achieve this is to mount a host directory inside a Docker container. This can be done via bind mounts or volumes, but for the purposes of this article, we’ll focus on using bind mounts.
Understanding Bind Mounts
Bind mounts have been around since the early versions of Docker. They allow you to map a directory or file from the host machine to a container. When you use a bind mount, a file or directory on the host machine is mounted into a container.
Benefits of Bind Mounts
- Performance: Bind mounts are performed directly through the host's filesystem, offering high I/O performance.
- Control: You get fine-grained control over the directories or files you want to share between your host and container.
- Development: Ideal for development environments where code on the host needs to be tested within a container in real time.
How to Use Bind Mounts
To use a bind mount, you include the --mount option when using docker run. The basic syntax looks like this:
In this example:
--name devtestspecifies the name of the container.--mount type=bind,source="$(pwd)"/mydata,target=/appadds the mount frommydatadirectory, located in the current working directory on the host, to/appinside the container.nginx:latestis the image used to create the container.
Note: It’s important that the source directory exists on your host, or Docker will return an error unless you specify the --mount option with the bind-propagation.
Mount Options
When configuring bind mounts, you have several options to control the behavior:
- Read-only: By setting
readonlyin the mount options, you can make sure the contents are not modified by the container.Example:
- Consistency: Control the consistency of the bind mount. The options are
consistent,cached, anddelegated.
Examples
Here’s how you would run a container with a bind mount in read-only and different consistency modes:
Comparing Bind Mounts and Volumes
Volumes are another way to persist and share data between the host and containers, managed by Docker. Here is a quick comparison:
| Feature | Bind Mount | Volume |
| Host Dependency | Yes, depends on file system | No, managed by Docker |
| Portability | Lower, ties to host path | Higher, no path required |
| Lifecycle | Managed by host | Managed by Docker |
| Use Case | Development, specific needs | Production, general data storage |
Best Practices
When using bind mounts, consider the following practices:
- Security: Avoid mounting sensitive host system directories into containers, especially in production.
- Lifecycle management: Remember, Docker does not manage the lifecycle of the host directory; you need to clean up manually.
Troubleshooting
Common issues with bind mounts include:
- Permission Errors: Ensure the container has the necessary permissions to access the bind mount directory.
- Path Errors: Verify the correct source path is specified. Remember, Docker does not auto-create the source path on the host when using bind mounts.
In conclusion, bind mounts are a powerful feature in Docker for developing applications, debugging issues, and sharing specific files or directories with a container. Correct usage can enhance your Docker experience but always handle with caution to avoid common pitfalls like security and path-related errors.
Related reading
- How to mount a host directory in a Docker container
- How to mount /dev/kvm in a non-privileged pod?
- How to mount docker socket as volume in docker container with correct group
- How to mount docker volume with jenkins docker container?
- How to move a pod from one node to another in kubernetes
- How to name Dockerfiles
- How to mount external volume for mongoDB using docker-compose and docker-machine
- How to mount host volumes into docker containers in Dockerfile during build

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.