๐Ÿ“ Docker

Docker Volumes: Persisting Data

P
Author
PyLand Team
๐Ÿ“…
Published
08.05.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
330
๐ŸŒฟ
Level
Medium

By default, data inside a container is not persisted โ€” delete the container and the data is gone. Volumes solve this problem.

Three Ways to Mount

Docker manages the storage. Data survives container removal.

docker run -v postgres_data:/var/lib/postgresql/data postgres

In docker-compose.yml:

services:
  db:
    image: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:  # declare the volume

You mount a directory from the host into the container. Changes in either direction are immediately visible.

docker run -v /path/on/host:/path/in/container myapp
docker run -v $(pwd):/app myapp           # current directory
docker run -v ./src:/app/src myapp        # relative path

In docker-compose.yml:

services:
  web:
    volumes:
      - ./src:/app/src     # for live reload in development
      - ./static:/app/static

3. tmpfs (Linux only)

Data lives in memory, not on disk. For temporary data.

docker run --tmpfs /tmp myapp

Managing Volumes

docker volume ls                    # list volumes
docker volume inspect postgres_data # volume details
docker volume rm postgres_data      # remove a volume
docker volume prune                 # remove unused volumes

When to Use What

Type When to use
Named volume Database data, uploads, persistent data
Bind mount Development โ€” hot reload, config files
tmpfs Cache, sessions, sensitive data

Development Pattern

Mount your code as a bind mount, dependencies as a named volume:

services:
  web:
    build: .
    volumes:
      - .:/app                      # code โ€” bind mount for hot reload
      - /app/.venv                  # isolate .venv in a volume

Without the second line, the host .venv would shadow the .venv from the image.

Backing Up a Volume

# Create a backup of a volume as a tar archive
docker run --rm -v postgres_data:/data -v $(pwd):/backup \
  ubuntu tar czf /backup/backup.tar.gz /data

# Restore
docker run --rm -v postgres_data:/data -v $(pwd):/backup \
  ubuntu tar xzf /backup/backup.tar.gz -C /

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Multi-stage Builds: Shrinking Your Docker Image

A large image means slow downloads, more disk usage, and a bigger attack surface. Multi-stage...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 321
๐Ÿ“

Docker Compose: Advanced Features

A basic docker-compose.yml is just the starting point. Production workloads need healthchecks, profiles, override files,...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 306
๐Ÿ“

Docker Networking: How Containers Communicate

Containers are isolated, but they often need to talk to each other and to the...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 306
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Docker: From Chaos to Containers Open course curriculum