Volume mounting in Docker allows you to share data between your host machine and Docker containers, making files and directories accessible from both environments. This is essential for persistent data storage, configuration sharing, and development workflows where code changes on the host need to be reflected in the running container. Without volume mounts, any data created inside a container is lost when the container stops or is removed. With volume mounting, you can preserve data, streamline development, and share configuration files across containers.
What You'll Need
- Docker installed and running on your system
- Basic knowledge of Docker commands like docker run and docker exec
- Access to a terminal or command line interface
- A Docker image to run (e.g., nginx, alpine, or a custom image)
- Permission to read/write files on your host machine
Step-by-Step
- Choose the type of volume mount: Docker supports three types — bind mounts, named volumes, and tmpfs mounts. For most use cases involving file sharing between host and container, use a bind mount or named volume. Bind mounts link a file or directory on the host to a path in the container. Named volumes are managed by Docker and are ideal for databases.
- Create a directory on your host machine: If using a bind mount, select or create a directory you want to share. For example, create a folder called
~/shared-datausingmkdir ~/shared-data. This will be the source path. - Run a container with a bind mount: Use the
-vor--mountflag withdocker run. The-vsyntax is shorter:docker run -v /host/path:/container/path image-name. For example:docker run -v ~/shared-data:/data alpine touch /data/test.txt. This creates a file inside the container that appears on the host. - Verify the mount works: Check the host directory to confirm the file was created:
ls ~/shared-data. You should seetest.txt. You can also enter the container withdocker exec -it container-id shand inspect/data. - Use named volumes for managed storage: Instead of a host path, create a named volume:
docker volume create myvol. Then mount it:docker run -v myvol:/data alpine touch /data/named-volume-test.txt. This keeps data isolated and managed by Docker. - Set read-only access if needed: To prevent the container from modifying host files, add
:roto the volume flag:-v ~/config:/etc/app:ro. This is useful for configuration files. - Use the --mount flag for clarity: While
-vis concise,--mountis more explicit. Example:docker run --mount type=bind,source=~/shared-data,target=/data alpine ls /data. This improves readability in scripts. - Clean up when done: Stop and remove containers with
docker rm -f container-id. Named volumes persist, so remove them withdocker volume rm myvolif no longer needed. Bind mounts leave host files intact.
Common Mistakes
- Using relative paths incorrectly: Docker may not resolve relative paths like
./dataas expected. Always use absolute paths (e.g.,/home/user/data). - Forgetting to create the host directory: If the source directory doesn’t exist, Docker may create a file instead of a directory, causing errors. Pre-create directories.
- Mounting over existing container directories: Mounting a volume over a non-empty directory in the container hides its original contents. Ensure the target path is correct.
- Ignoring file permissions: Files created in the container may have different ownership. Use
chownor run the container with the correct user ID using--user. - Confusing -v and --mount syntax:
-vuseshost:container:options, while--mountuses key-value pairs. Mixing them causes errors.
Tips
- Use named volumes for databases: They are easier to back up and manage. For example,
docker run -v dbdata:/var/lib/mysql mysqlensures data survives container restarts. - Mount configuration files individually: Instead of entire directories, mount specific config files like
-v ~/app.conf:/etc/app.conf:roto reduce clutter. - Use environment variables in compose files: In
docker-compose.yml, use${HOME}or./paths carefully. Prefer named volumes in production. - Test mounts with lightweight images: Use
alpineorbusyboxto test volume behavior before using larger images. - Monitor disk usage: Named volumes consume space. List them with
docker volume lsand inspect withdocker volume inspect vol-name.
Troubleshooting
If volume mounting fails, start by checking the container logs: docker logs container-id. If files don’t appear on the host, verify the host path exists and has correct permissions. On Linux, SELinux or AppArmor may block access; temporarily disable to test. On macOS or Windows, ensure Docker Desktop has file sharing enabled for the directory. If using --mount, double-check syntax — missing equals signs or commas cause failures. If a container exits immediately, run it interactively with -it and sh to inspect the mounted path. For permission issues, run the container with --user $(id -u):$(id -g) to match host user IDs. If a named volume seems empty, confirm you’re inspecting the right volume with docker volume inspect. Finally, restart the Docker service if mounts stop working unexpectedly.
Frequently Asked Questions
- What is the difference between -v and --mount?
The-vflag is shorter and older;--mountis more verbose and explicit, using key-value syntax. Both work, but--mountis recommended for scripts. - Can I mount a single file?
Yes. Use-v /host/file.txt:/container/file.txt. Ensure the file exists on the host to avoid Docker creating a directory. - Do volume mounts work on Docker Desktop?
Yes, but you must enable file sharing for the host path in Docker Desktop settings on macOS and Windows. - What happens to data when I remove a container?
Data in bind mounts persists on the host. Named volumes persist unless explicitly removed withdocker volume rm. - Can multiple containers share the same volume?
Yes. Multiple containers can mount the same named volume or bind mount, enabling shared storage. - Why is my mounted directory empty in the container?
This usually means the host path is wrong or empty. Verify the source path and permissions. Also, ensure you’re not mounting over a non-empty container path that gets hidden.