Bind Mounts
Bind mounts are another way to make files from the host available to a container. Unlike volumes, bind mounts reference a specific directory on the host filesystem, and they are not managed by Docker.
What Are Bind Mounts?
A bind mount maps a host directory or file into a container. Any changes in the container are reflected on the host and vice versa. Bind mounts are useful for development (live code reload) and for giving containers access to host configuration files.
Using Bind Mounts
Specify an absolute path on the host and a path in the container with
-v or --mount. For example, to mount the current directory into /app in a container:docker run -v $(pwd):/app alpine ls /appUsing --mount (more explicit):docker run --mount type=bind,source=$(pwd),target=/app alpine ls /appBind Mounts vs. Volumes
- Volumes: Managed by Docker, stored in Docker’s storage directory. Can be used across containers and have better performance on some filesystems. Recommended for production.
- Bind mounts: Directly link a host path. Easy for development, but less portable and can have permission issues.
Read‑Only Bind Mounts
You can make a bind mount read‑only by adding
:ro to the mount spec:docker run -v $(pwd):/app:ro alpine touch /app/test.txt # will failUse Case: Development with Live Reload
Suppose you have a Node.js app in the current directory. You can mount it and run nodemon inside the container to automatically restart on changes:
docker run -v $(pwd):/app -w /app node nodemon app.jsNow editing files on your host triggers a restart inside the container.Permissions Considerations
Files created inside a container using a bind mount will have the UID of the user inside the container, which may differ from your host user. This can cause permission issues. You can solve this by matching UIDs or using a volume.
Two Minute Drill
- Bind mounts map a host directory into a container.
- Use
-v /host/path:/container/pathor--mount type=bind,source=...,target=.... - Bind mounts are great for development, but volumes are preferred for production.
- Add
:rofor read‑only mounts. - Be aware of permission differences between host and container.
Need more clarification?
Drop us an email at career@quipoinfotech.com
