Docker Volumes

Understand how Docker volumes work, why containers should not store important data internally, and how persistent storage works in containerized environments.

One of the most important beginner Docker lessons is this:

Containers are temporary.
Data is not.

This single idea explains why Docker volumes exist.

Containers are designed to be:

  • stopped
  • deleted
  • replaced
  • recreated

very easily.

But applications often need persistent data.

For example:

  • databases
  • uploaded files
  • application state
  • configuration
  • logs

Without persistent storage, this data would disappear whenever containers are removed.

Docker volumes solve this problem.


The Core Problem

Imagine a PostgreSQL container.

Example:

docker run postgres

The database writes data inside the container filesystem.

Simplified model:

Container
Internal Filesystem
Database Files

Now imagine:

docker rm postgres

The container disappears.

And:

the database files disappear too

This is one of the biggest beginner mistakes in Docker.


Why Containers Lose Data

Containers use writable layers.

Simplified structure:

+----------------------+
| Writable Layer       |
| Container Changes    |
+----------------------+
| Docker Image         |
+----------------------+

When the container is deleted:

the writable layer disappears

This includes:

  • created files
  • logs
  • database data
  • uploaded content

Unless the data exists outside the container, it is lost.


What is a Docker Volume?

A Docker volume is persistent storage managed separately from the container lifecycle.

Simplified model:

Container
Mounted Volume
Persistent Data

Now:

Deleting the container
does NOT delete the volume

This is the key idea.


Why Volumes Matter

Volumes are critical for real-world applications.

For example:

  • PostgreSQL databases
  • MySQL databases
  • Nextcloud uploads
  • WordPress content
  • Grafana dashboards
  • application configuration

Without persistent storage:

containers become disposable
but data becomes disposable too

which is usually unacceptable.


Creating a Volume

To create a volume:

docker volume create postgres-data

Docker now manages persistent storage separately.

To list volumes:

docker volume ls

Example:

postgres-data
grafana-storage
nextcloud-data

Mounting a Volume

Volumes become useful when attached to containers.

Example:

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

Simplified flow:

Docker Volume
Mounted Inside Container
Database Stores Data Persistently

Now database files survive container replacement.


Understanding Volume Mount Syntax

Basic syntax:

-v VOLUME_NAME:CONTAINER_PATH

Example:

-v postgres-data:/var/lib/postgresql/data

Meaning:

Docker Volume:
postgres-data

mounted into:

Container Path:
/var/lib/postgresql/data

The application inside the container writes data normally.

Docker handles persistence automatically.


Container Replacement Example

Without volumes:

Delete Container
Lose Data

With volumes:

Delete Container
Volume Still Exists
New Container Uses Same Data

This is one of the foundational container infrastructure concepts.


Real-World Example

Imagine upgrading PostgreSQL.

Workflow:

Old PostgreSQL Container
Stop Container
Delete Container
Start New PostgreSQL Container
Reuse Existing Volume

The database survives because the data exists outside the container itself.

This pattern is extremely common.


Volumes vs Bind Mounts

Docker supports multiple storage approaches.

The two most common are:

Volumes

and:

Bind Mounts

Docker Volumes

Docker-managed storage.

Example:

-v postgres-data:/data

Advantages:

  • managed by Docker
  • portable
  • easier backups
  • cleaner isolation

Best for:

  • databases
  • persistent application storage
  • production systems

Bind Mounts

Direct mapping to host filesystem paths.

Example:

-v /home/user/app:/app

Simplified model:

Host Folder
Mounted Into Container

Best for:

  • development
  • live code editing
  • configuration files

Bind Mount Example

Frontend development example:

docker run -v $(pwd):/app node

Now:

Host File Changes
Immediately Visible Inside Container

This is extremely useful during development workflows.


Anonymous Volumes

Docker can also create unnamed volumes automatically.

Example:

docker run -v /data nginx

Docker creates a hidden anonymous volume.

These can accumulate over time if not cleaned up.


Inspecting Volumes

To inspect volume details:

docker volume inspect postgres-data

This shows:

  • storage location
  • metadata
  • mount information

Useful for troubleshooting.


Where Docker Stores Volumes

On Linux systems, Docker often stores volumes under:

/var/lib/docker/volumes/

However:

manually editing volume files directly
is usually discouraged

Docker should manage volume lifecycle whenever possible.


Volume Persistence

Important distinction:

docker rm container

does NOT remove volumes automatically.

Volumes remain until explicitly deleted.

Example:

docker volume rm postgres-data

This prevents accidental data loss.


Common Beginner Mistake

One of the biggest beginner mistakes is storing databases directly inside containers without volumes.

Example:

Run PostgreSQL
Delete Container
Database Gone

This mistake is extremely common.

Volumes are mandatory for persistent applications.


Volumes and Backups

Persistent volumes also make backups possible.

Example:

Backup Volume
Restore Volume
Recreate Container

Modern infrastructure often treats containers as temporary while preserving storage independently.


Infrastructure Thinking

Containers introduced a major infrastructure shift:

Application Runtime

became separated from:

Persistent Data

This separation improves:

  • scalability
  • automation
  • portability
  • deployment consistency

Modern orchestration systems heavily rely on this idea.


Volumes in Docker Compose

Docker Compose commonly defines persistent volumes.

Example:

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

volumes:
  postgres-data:

This makes multi-container applications much easier to manage.


Why This Matters

Understanding volumes is critical before learning:

  • Docker Compose
  • databases in containers
  • backups
  • Kubernetes persistent storage
  • production deployments

Persistent storage is one of the hardest problems in distributed infrastructure.

Docker volumes provide one important piece of that solution.


Key Takeaways

  • Containers are temporary but data usually should persist
  • Volumes store data outside container lifecycle
  • Deleting containers does not automatically remove volumes
  • Volumes are critical for databases and persistent applications
  • Bind mounts connect host folders directly into containers
  • Volumes improve portability and infrastructure consistency
  • Persistent storage is foundational in modern infrastructure
  • Understanding volumes is essential for production Docker usage