Skip to main content
🎤 Luca Berton is speaking at Red Hat Summit & KubeCon EU 2026!Learn more →
Back to Blog

Docker Volumes and Persistent Data

Manage persistent data in Docker with named volumes and bind mounts. Backup strategies, restore procedures, and container data sharing.

Luca BertonApril 14, 20262 min read

Why Volumes?

Containers are ephemeral — when they stop, data is gone. Volumes persist data beyond the container lifecycle.

Named Volumes

Docker manages the storage location:

bash
# Create
docker volume create mydata

# Use in a container
docker run -d --name db \
  -v mydata:/var/lib/postgresql/data \
  postgres:16

# List volumes
docker volume ls

# Inspect
docker volume inspect mydata

Bind Mounts

Map a host directory into the container:

bash
docker run -d --name web \
  -v /home/user/website:/usr/share/nginx/html:ro \
  nginx

The :ro flag makes it read-only inside the container.

Related Course

Master this topic with hands-on labs

Go beyond reading — build real projects in sandboxed environments with expert video guidance.

Browse Courses →

Docker Compose Volumes

yaml
services:
  db:
    image: postgres:16
    volumes:
      - pgdata:/var/lib/postgresql/data      # Named volume
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql  # Bind mount

volumes:
  pgdata:

When to Use Which

FeatureNamed VolumeBind Mount
Managed by DockerYesNo
PerformanceBest (especially on Mac)Native on Linux
PortableYesDepends on host paths
Pre-populatedYes (from image)No
Direct host accessVia docker volume inspectYes

Rule of thumb: named volumes for databases and app state, bind mounts for config files and source code (dev).

Backup a Volume

bash
# Backup to tar
docker run --rm \
  -v mydata:/source:ro \
  -v $(pwd):/backup \
  alpine tar czf /backup/mydata-backup.tar.gz -C /source .

# Restore
docker run --rm \
  -v mydata:/target \
  -v $(pwd):/backup \
  alpine tar xzf /backup/mydata-backup.tar.gz -C /target

Copy Data Between Volumes

bash
docker run --rm \
  -v source_vol:/from:ro \
  -v dest_vol:/to \
  alpine sh -c "cp -a /from/. /to/"
Stay Updated

Get weekly IT automation tips

Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.

Subscribe Free →

Share Volumes Between Containers

Multiple containers can mount the same volume:

yaml
services:
  writer:
    image: alpine
    command: sh -c "while true; do date >> /data/log.txt; sleep 5; done"
    volumes:
      - shared:/data

  reader:
    image: alpine
    command: tail -f /data/log.txt
    volumes:
      - shared:/data:ro

volumes:
  shared:

tmpfs Mounts

In-memory storage — fast but data is lost on restart:

bash
docker run -d --name cache \
  --tmpfs /tmp:size=100m \
  redis:7-alpine

Use for: temporary files, caches, sensitive data that should not persist.

Cleanup

bash
# Remove unused volumes
docker volume prune

# Remove a specific volume
docker volume rm mydata

# Nuclear — remove all stopped containers and their volumes
docker system prune --volumes

Warning: docker volume prune deletes ALL volumes not attached to a running container. Double-check before running in production.

---

Ready to go deeper? Check out our hands-on course: Docker Fundamentals — practical exercises you can follow along on your own machine.

Ready to learn by doing?

Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.

Share this article
LB
Luca Berton

Docker Captain, IT automation expert, Red Hat Summit & KubeCon speaker. Building hands-on education for DevOps engineers at CopyPasteLearn.

Related Articles

Explore topics

Browse more articles on the topics covered here.