π Day 14: Mastering Docker Volumes Named, Anonymous, Bind Mount and tmpfs | Complete Guide


Welcome to this complete guide on Docker Volumes! In this video, we'll go from basics to advanced topics, ensuring you're fully equipped to manage persistent data in Docker. Let's dive in! π³π‘
π³ What is Docker Volume?
Docker volumes are Docker-managed storage mechanisms designed for persistent data. They are stored in a part of the host filesystem managed by Docker (typically /var/lib/docker/volumes/
on Linux) and are the preferred way to persist data in Docker.
π‘ Why Use a Docker Volume?
By default, any data written inside a container is lost when the container is removed. Volumes solve this problem by providing a managed, persistent storage location.
π Key Features of Docker Volumes
π¦ Persistent β Data is retained even if the container is deleted.
π Shared/Reusable β Volumes can be used by multiple containers.
π οΈ Managed by Docker β Docker takes care of creation, mounting, and lifecycle.
π Decouples Data and Container β Keeps data independent of container life.
β‘ Performance β Optimized for container use.
π§° Commands and Flags
-v
or--volume
: Specify a volume.--mount
: More explicit, flexible alternative (preferred for clarity).--tmfs
: Mount a temporary in-memory filesystem (tmpfs). Useful for ephemeral data that doesn't persist across reboots.
π¬ Video Demonstration
ποΈ Types of Docker Volumes
1οΈβ£ Anonymous Volumes
Anonymous volumes are created automatically by Docker when a container is started, and a volume is specified without a specific name. They are often used for temporary or disposable data, as they are not explicitly named and can be challenging to manage outside the context of the container.
Created without a specific name.
Docker generates a random name internally.
Example:
docker volume create
or
docker run -v /app/data nginx:alpine
docker volume ls
2οΈβ£ Named Volumes
Named volumes are explicitly created and named using the docker volume create command. They provide a way to manage and share data across containers with a user-defined name.
You define the name.
Managed by Docker in
/var/lib/docker/volumes/
.
Example:
docker volume create nginx_data
docker run -itd -v nginx_data:/usr/share/nginx/html/ nginx:alpine
or
docker run -itd --volume nginx_data:/usr/share/nginx/html/ nginx:alpine
docker volume ls
3οΈβ£ Mapped/Host Volumes (a form of Bind Mount)
Host bind mounts involve mounting a specific directory or file from the host machine into the container. Changes made in the container affect the host file system, and vice versa.
Maps a directory on the host to the container.
Great for sharing data between host and container.
Can be relative or absolute path
-v ./path:/container/path is a shorthand for bind mount, easier and quicker.
Example:
docker run -itd -p 80:80 -v ./data:/usr/share/nginx/html/ nginx:alpine
sudo nano ./data/index.html
curl http://localhost
sudo nano ./data/index.html
4οΈβ£ Bind Mounts (explicit)
Host bind mounts involve mounting a specific directory or file from the host machine into the container. Changes made in the container affect the host file system, and vice versa.
Uses absolute paths.
Data is managed directly by the host OS.
Must be absolute path
--mount type=bind,source=/path,target=/container is more powerful, cleaner, and preferred for scripts or production.
Example:
docker run -itd -p 80:80 --mount type=bind,source=/home/user/data/,target=/usr/share/nginx/html/ nginx:alpine
curl http://localhost
π Key Differences:
Feature | Mapped Host Volumes (-v ) | Bind Mounts (--mount ) |
Syntax | Short: -v ./host:/container | Long: --mount type=bind,... |
Path type (host) | Can be relative or absolute | Must be absolute |
Feature flexibility | Limited | More flexible (supports more options) |
Readability | Less explicit | More clear and structured |
Use case | Quick, local, simple dev scenarios | Advanced configurations, clarity, control |
5οΈβ£ tmpfs Mounts
tmpfs mounts allow you to create an in-memory volume that is stored in the host's memory rather than on disk. This type of volume is useful for temporary data that does not need to persist across container restarts.
Stored in host memory (RAM), not on disk.
Volatile: Data does not persist after the container stops.
High-performance: Ideal for fast, temporary data storage. very fast and lightweight storage (faster than other mounts because of in-memory )
tmpfs mounts in Docker map directly to tmpfs in the Linux kernel. As such, the temporary data may be written to a swap file, and thereby persisted to the filesystem.
Example:
docker run -itd --tmpfs /app nginx:latest
docker run -itd --mount type=tmpfs,destination=/usr/share/nginx/html/ nginx:alpine
or
docker run -itd --mount type=tmpfs,destination=/app/data,tmpfs-size=100m,tmpfs-mode=1770 nginx:alpine
# This mounts a tmpfs volume to /app/data inside the container. All data written to this path will be stored in the hostβs RAM.
docker run -d \
-it \
--name tmptest \
--mount type=tmpfs,destination=/app \
nginx:alpine
docker inspect tmptest --format '{{ json .Mounts }}'
tmpfs-size=100m: Limit RAM usage to 100MB
tmpfs-mode=1770: Set file permissions (e.g., 1770 for rwxrwx--T)
Works only on Linux hosts (Windows/macOS Docker Desktop uses a VM).
π§ Managing Docker Volumes
π Creating Volumes
docker volume create data_volume
# If no name specified:
docker volume create
π Listing Volumes
docker volume ls
# π Filter by name:
docker volume ls -f name=data
π Inspecting Volumes
docker volume inspect data_volume
# Example Output:
{
"Name": "data_volume",
"Mountpoint": "/var/lib/docker/volumes/data_volume/_data",
...
}
β Removing Volumes
docker volume rm data_volume
π§Ή Pruning Unused Volumes
docker volume prune
# Prompts: Are you sure you want to continue? [y/N]
π€ Sharing Volumes with --volumes-from
π What It Does
Allows one container to reuse volumes from another container.
β Example 1: Share Anonymous Volume
docker run -d --name data-container -v /data busybox sleep 3600
docker run -it --rm --name app-container --volumes-from data-container busybox sh
β Example 2: Share Named Volume
docker volume create shared-vol
docker run -d --name backend -v shared-vol:/var/lib/app busybox sleep 3600
docker run -it --rm --name frontend --volumes-from backend busybox sh
β Example 3: Read-Only Volume
docker run -it --rm --name reader --volumes-from backend:ro busybox sh
π― Example Scenarios
### ποΈ 1. Persist Database Data
docker run -v my-db-volume:/var/lib/mysql mysql
β
Data persists across container restarts.
### π¨βπ» 2. Live Code Development
docker run -v /home/user/code:/app my-image
β
Host file changes reflect immediately in the container.
### π 3. Read-Only Configuration
docker run --mount type=bind,source=/config,target=/app/config,readonly my-image
β
Ensures configuration files can't be modified by the container.
β Best Practices & Recommendations
βοΈ Use Volumes (
--mount type=volume
) for production.π§ͺ Use Bind Mounts for development scenarios.
π§Ύ Prefer
--mount
over-v
for clarity in scripts.β οΈ Avoid Bind Mounts in production due to permission/portability issues.
π Additional Resources
π Docker Volumes
π Docker Bind Mounts
π Docker tmpfs Mounts
Subscribe to my newsletter
Read articles from Ibrar Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ibrar Ansari
Ibrar Ansari
Hello! I'm a DevOps Engineer dedicated to continuous learning and contributing to the tech community. If you find value in what I share, feel free to spread the word to others who might benefit as well. Your support is greatly appreciated!