0
0
Dockerdevops~5 mins

Volume vs bind mount decision in Docker - CLI Comparison

Choose your learning style9 modes available
Introduction
When running applications in Docker containers, you often need to save data outside the container. Volumes and bind mounts are two ways to do this. Choosing the right one helps keep your data safe and your app working smoothly.
When you want Docker to manage your data storage automatically and keep it isolated from your host system.
When you need to share specific files or folders from your computer directly with a container for development.
When you want to persist database files so they are safe even if the container is deleted.
When you want to edit source code on your computer and see changes immediately inside the container.
When you want to backup or move data easily between different Docker hosts.
Commands
This command creates a Docker volume named 'my-volume' to store data managed by Docker.
Terminal
docker volume create my-volume
Expected OutputExpected
my-volume
Runs a container named 'volume-container' using the 'busybox' image. It mounts the Docker volume 'my-volume' inside the container at /app/data to persist data.
Terminal
docker run -d --name volume-container -v my-volume:/app/data busybox sleep 3600
Expected OutputExpected
a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0
-d - Run container in background (detached mode)
-v - Mount volume or bind mount inside container
Runs a container named 'bind-container' mounting the host folder '/home/user/project' inside the container at /app. This is a bind mount to share files directly.
Terminal
docker run -d --name bind-container -v /home/user/project:/app busybox sleep 3600
Expected OutputExpected
b1c2d3e4f567890123456789abcdef0123456789abcdef0123456789abcdef0
-d - Run container in background (detached mode)
-v - Mount volume or bind mount inside container
Shows details about the Docker volume 'my-volume', including where Docker stores it on the host.
Terminal
docker volume inspect my-volume
Expected OutputExpected
[ { "CreatedAt": "2024-06-01T12:00:00Z", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/my-volume/_data", "Name": "my-volume", "Options": {}, "Scope": "local" } ]
Shows detailed information about the 'bind-container', including the bind mount path from the host.
Terminal
docker inspect bind-container
Expected OutputExpected
[ { "Id": "b1c2d3e4f567890123456789abcdef0123456789abcdef0123456789abcdef0", "Mounts": [ { "Type": "bind", "Source": "/home/user/project", "Destination": "/app", "Mode": "", "RW": true, "Propagation": "rprivate" } ] } ]
Key Concept

If you remember nothing else from this pattern, remember: use volumes for Docker-managed persistent data and bind mounts to share host files directly with containers.

Common Mistakes
Using a bind mount to store database files directly on the host.
Bind mounts can cause permission and consistency issues with database files because the host and container handle files differently.
Use a Docker volume to store database files for safe and consistent data management.
Expecting changes in a volume to reflect immediately on the host filesystem.
Docker volumes are managed by Docker and do not map directly to visible host folders, so changes are not directly visible on the host.
Use bind mounts if you need to see and edit files directly on the host.
Not creating a volume before using it in a container.
Docker can create volumes automatically, but explicitly creating them helps manage and inspect volumes easily.
Create volumes explicitly with 'docker volume create' before using them.
Summary
Create Docker volumes to store data managed by Docker and keep it isolated from the host.
Use bind mounts to share specific host folders or files directly with containers for development or configuration.
Inspect volumes and containers to verify where data is stored and how it is mounted.