When you are working with Docker, you might need to share files and directories between your host system and running containers. This is where the Docker bind mount comes in. A bind mount is essential to bind a directory or a file from the host machine directly into the container, making it accessible in real time.
Bind mounts is essentially important for:
- Local development; to keep the code in sync between the host and the container.
- Configuration management; sharing config files across environments.
- Data access; exposing log files, datasets, or scripts inside a container.
They provide the required flexibility and simplicity but it also requires careful handling since changes on both sides.
How Docker Bind Mounts Work
At the core, bind mounts create a direct link between the path on the host machine and a path inside the container. This essentially means that:
- When you update a file on the host, the container will immediately sense the change.
- If the container modifies the mounted file, the change is written back to the host.
Here is how the mechanism works:
- You define a host path (e.g., /home/user/app).
- You map it to a container path (e.g., /usr/src/app).
- Docker mounts the host directory into the container at runtime.
For example:
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
docker run -it –name myapp \
-v /home/user/app:/usr/src/app \
python:3.10 bash
In this example:
- /home/user/app is the host directory.
- /usr/src/app is the mount point inside the container.
- Any changes made in one location are instantly visible in the other.
Because of this real-time sync, Docker bind mounts are super effective for development workloads. However, there can be issues with the portability and security challenges if managed carefully.
Related Articles: Docker Build Args: How to Use Build Arguments in Docker and Docker Compose
Setting Up a Bind Mount in Docker
Setting up Docker bind mount is super easy and there are two main methods to do it:
- Using the -v flag with docker run.
- Using the –mount flag for a more explicit and readable configuration.
Both methods allow you to map a host directory or file into a container directly, but they differ in terms of syntax and readability.
Using docker run with -v
The -v flag is one of the easiest and the quickest ways to create a Docker bind mount. The general syntax is:
docker run -d \

-v <host-path>:<container-path> \
–name myapp \
nginx
Example of Docker bind mount with -v:
docker run -d`w`aq \
-v /home/user/website:/usr/share/nginx/html \
–name webserver \
Nginx
- /home/user/website → host directory containing your website files.
- /usr/share/nginx/html → the Nginx default web root inside the container.
- Any file changes on the host are instantly reflected inside the container.
Using –mount Flag
The –mount flag is a more recent and descriptive manner to define a Docker bind mount. It allows you to avoid ambiguity by clearly stating the mount type and parameters.
Syntax:
docker run -d \
–mount type=bind,source=<host-path>,target=<container-path> \
–name myapp \
nginx
Example of Docker bind mount with –mount:
docker run -d \
–mount type=bind,source=/home/user/website,target=/usr/share/nginx/html \
–name webserver \
nginx
Here’s what each option means:
- type=bind → Specifies a bind mount.
- source → Host path to mount.
- target → Path inside the container where it will be mounted.
Since the syntax is quite exclusive, –mount is considered more reliable for production environments and easier to understand in team projects.
Bind Mounts in Docker Compose
So docker run works best for screen tests, most production projects use Docker Compose for multi-container environments. Docker bind mount can be defined in the docker-compose.yml file under the volumes section.
Example of a Docker Compose bind mount:
version: ‘3.9’
services:
web:
image: nginx:latest
ports:
– “8080:80”
volumes:
– ./website:/usr/share/nginx/html
Here:
- ./website → directory on the host (relative path).
- /usr/share/nginx/html → mount point inside the container.
This setup will automatically sync the local website folder using the container web root, making it the best choice for web development.
Advantages and Limitations of Bind Mounts
Docker bind mounts are super powerful, but it does come with certain limitations as well.
Advantages:
- Real-time syncing between the host and container ports.
- No extra storage layer, it uses the filesystem directly.
- It is a great option for local development and debugging.
- Simple to set up with -v, –mount, or Docker Compose.
Limitations:
- It does come with certain portability issues.
- Containers gain easy access to sensitive host directories if they are misconfigured.
- Moving containers between machines may break the bind mounts.
- Can be slower than Docker volumes on some platforms (e.g., macOS/Windows with Docker Desktop).
Inspecting and Managing Bind Mounts
To verify and manage Docker bind mounts, you can use these useful commands:
- List all mounts for a container:
docker inspect <container_id> –format='{{json .Mounts}}’ - Stop container and remove mount:
docker rm -f <container_id>
Security Considerations with Bind Mounts
A Docker bind mount can expose host paths to the containers, which introduce potential security risks if not properly handled.
Key issues:
- A compromised container could modify critical host files.
- Accidentally mounting sensitive directories (e.g., /etc, /var/lib/docker).
- By default, bind mounts are writable, which increases risk.
Best practices for security:
- You should only use read-only mounts whenever possible:
docker run -d \
–mount type=bind,source=/home/user/config,target=/app/config,readonly \
myapp
- Keep the Docker bind mounts limited to non-sensitive host paths.
- Avoid unnecessary mounts that are in production.
- Use Docker Compose or Kubernetes with strict configuration to avoid accidental overexposure.
Bind Mounts vs. Volumes: What’s the Difference?
Feature | Bind Mounts | Volumes |
Storage Location | Any path on the host filesystem | Managed by Docker in /var/lib/docker/volumes/ |
Setup | Requires specifying host path | Docker auto-manages paths |
Portability | Tied to host path (less portable) | Portable across environments |
Performance | May be slower on non-Linux hosts (e.g., Docker Desktop) | Optimized for Docker, usually faster |
Security | Exposes host directories to containers | Better isolation, safer for production |
Management | Fully manual (you manage files) | Managed by Docker (docker volume ls, prune, etc.) |
Best For | Development, testing, quick file access | Production, portability, and persistence |
Best Practices for Using Bind Mounts
To make most out of a Docker bind mount while avoiding the pitfalls, use these best practices:
- Use absolute paths to prevent the ambiguity and to ensure consistent behaviour.
- Leverage read-only mounts to secure sensitive files, such as config files.
docker run -d \
–mount type=bind,source=/home/user/config,target=/app/config,readonly \
myapp
- Only mount the necessary binds to reduce exposure.
- Never bind sensitive host paths like /etc, /var/lib/docker.
- Document properly in Docker Compose files so that it is easy to replicate setups.
- Only reserve the bind mounts for local dev.
Conclusion
A Docker bind mount is one of the simplest yet the most powerful ways to share files and directories between the host and the container. They would shine in development workflows where it is important to sync in real-time, like code edits, configuration testing, or accessing logs.
By understanding the bind mounts and using them carefully, you would be able to build a more efficient, flexible, and secure Docker workflow.
FAQs
How is a Docker bind mount different from a Docker volume?
Bind mounts map a specific host path into a container, while Docker volumes are managed by Docker and stored in a Docker-specific directory, offering more portability and flexibility.
How do I create a bind mount in Docker?
You can create a bind mount with the -v
or --mount
flag in the docker run
command. For example:
docker run -v /host/path:/container/path my_image
Are bind mounts secure in Docker?
Bind mounts can pose security risks since they grant containers access to host files. It’s important to mount only necessary directories, use read-only mounts where possible, and restrict container permissions.