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

Ibrar AnsariIbrar Ansari
6 min read

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

Watch on Youtube

πŸ—‚οΈ 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/.

image_layers

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.

image_layers

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:

FeatureMapped Host Volumes (-v)Bind Mounts (--mount)
SyntaxShort: -v ./host:/containerLong: --mount type=bind,...
Path type (host)Can be relative or absoluteMust be absolute
Feature flexibilityLimitedMore flexible (supports more options)
ReadabilityLess explicitMore clear and structured
Use caseQuick, local, simple dev scenariosAdvanced 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.

image_layers


βœ… 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

0
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!