← Back to blog

Docker Volumes: Why Container Data Needs Persistent Storage

One of the first things you need to understand about Docker is that a container is not the same thing as permanent storage.

This becomes especially important when you’re running something like PostgreSQL, MySQL, MongoDB, or another application that needs to save data.

Imagine you have a PostgreSQL database running inside a Docker container. You create users, insert records, and store information in the database.

Then the container is removed.

What happens to the data?

This is where Docker volumes come in.

The Problem With Keeping Data Inside a Container is that a container is designed to be replaceable.

You might stop it, remove it, rebuild it, or create a new container from an updated image.

If important application data exists only inside the container’s writable filesystem, you shouldn’t treat that data as something that will automatically survive the lifecycle of the container.

For a database, this would obviously be a problem.

You don’t want to lose your entire PostgreSQL database simply because you replaced its container.

Docker provides persistent storage mechanisms called volumes to solve this problem.

What Is a Docker Volume?

A Docker volume is a place where data can be stored independently of the container that uses it.

The basic idea is while we have a container, we would want a permanent/persistent storage for that container

The container can come and go while the important data remains available.

Docker’s documentation describes volumes as persistent data stores implemented by the container engine.

This makes volumes particularly useful for stateful applications such as databases.

Why would a database even need a volume ?

Consider a PostgreSQL database container.

The database stores its actual database files somewhere inside the container’s filesystem.

If those files are not stored persistently, replacing the container can mean losing the database.

The solution to this would be attaching a Docker volume to PostgreSQL’s data directory.

For example, this code used in docker-compose.yml for building docker containers


services:
  database:
    image: postgres:15
    volumes:
      - pg-data:/var/lib/postgresql/data

volumes:
  pg-data:

In the code, we create a volume named pg-data and set the location of the volume pg-data to “/var/lib/postgresql/data” which would be where the PostgreSQL database files would be stored

The “pg-data” volume exists separately from the PostgreSQL container.

What Happens When the Container Stops then?

Well stopping a container is not the same as deleting its persistent volume.

For example:

when we do “docker compose down”, we can stop and remove the application’s containers and network while the named volume remains available.

When you start the application again, PostgreSQL can use the existing volume and access the data that was previously stored there.

This is why persistent storage is so important for databases.

Also, Volumes Aren’t Only for Databases

Although databases are one of the most obvious examples, persistent storage can be useful for other applications too.

For example:

The important question is:

Does this data need to survive the lifecycle of the container?

If the answer is yes, you need to think about persistent storage.

.