Docker
Containerization
Docker Volumes
DevOps
Host Directory Mounting

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.

Practice system design

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:

 
1docker run -d \
2  --name devtest \
3  --mount type=bind,source="$(pwd)"/mydata,target=/app \
4  nginx:latest

In this example:

  • --name devtest specifies the name of the container.
  • --mount type=bind,source="$(pwd)"/mydata,target=/app adds the mount from mydata directory, located in the current working directory on the host, to /app inside the container.
  • nginx:latest is 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 readonly in the mount options, you can make sure the contents are not modified by the container.
    Example:
 
  --mount type=bind,source="$(pwd)"/mydata,target=/app,readonly
  • Consistency: Control the consistency of the bind mount. The options are consistent, cached, and delegated.

Examples

Here’s how you would run a container with a bind mount in read-only and different consistency modes:

bash
1# Read-only bind mount
2docker run -d \
3  --name devreadonly \
4  --mount type=bind,source="$(pwd)"/mydata,target=/readonlyapp,readonly \
5  nginx:latest
6
7# Cached consistency
8docker run -d \
9  --name devcached \
10  --mount type=bind,source="$(pwd)"/mydata,target=/app,consistency=cached \
11  nginx:latest

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:

FeatureBind MountVolume
Host DependencyYes, depends on file systemNo, managed by Docker
PortabilityLower, ties to host pathHigher, no path required
LifecycleManaged by hostManaged by Docker
Use CaseDevelopment, specific needsProduction, 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.