docker

Docker Volumes and Persistent Storage: Complete Guide

Docker Volumes and Persistent Storage: Complete Guide

By Shubhankar Tripathi • • 5 min read

Introduction

Containers are designed to be lightweight and disposable. This makes them excellent for running applications, but it creates an important challenge: what happens to data when a container is deleted?

By default, data written inside a container belongs to that container's writable layer. If the container is removed, that data is removed with it.

Docker provides several mechanisms for handling persistent data:

  • Volumes
  • Bind mounts
  • tmpfs mounts

Among these, Docker volumes are the preferred Docker-managed mechanism for persistent application data.

Understanding Docker storage is essential when running databases, application uploads, logs, configuration files, or any workload where data must survive container recreation.


1. Why Do Containers Need Persistent Storage?

Consider a MySQL container:

docker run -d \
  --name mysql-db \
  -e MYSQL_ROOT_PASSWORD=password \
  mysql:8

MySQL writes its database files inside the container filesystem.

Now suppose the container is removed:

docker rm -f mysql-db

The container and its writable filesystem are removed.

If another MySQL container is created, it does not automatically contain the previous database data.

This demonstrates an important principle:

Container lifecycle and application data lifecycle should usually be treated separately.

The container can be replaced while the data remains available.


2. Docker Storage Architecture

A simplified Docker storage model looks like this:

                 Docker Host
                     |
          +----------+----------+
          |                     |
      Container              Persistent
      Filesystem               Storage
          |                     |
    Writable Layer        +------+------+
                          |             |
                       Volume       Bind Mount

A container normally has a writable layer on top of its image layers.

hanges made inside the container are written to the writable layer.

When the container is removed, that writable layer disappears.

With persistent storage:

+-----------------------------+
| Container                   |
|                             |
| /app/data ------------------+------+
+-----------------------------+      |
                                      |
                                Docker Volume
                                      |
                                Persistent Data

The storage exists independently of the container.


3. Docker Storage Options

Docker supports three major types of mounts.

Storage TypeManaged ByTypical Use
VolumeDockerPersistent application data
Bind mountUser/HostDevelopment and host file access
tmpfsHost memoryTemporary sensitive/runtime data

4. What Is a Docker Volume?

A Docker volume is persistent storage managed by Docker.

Create a volume:

docker volume create app-data

List volumes:

docker volume ls

Inspect a volume:

docker volume inspect app-data

A volume can then be mounted into a container.

docker run -d \
  --name myapp \
  -v app-data:/app/data \
  nginx

Here:

app-data
   |
   +----> /app/data
             |
          Container

The data written to /app/data is stored in the Docker volume.


5. Named Volumes

The volume created explicitly by the user is called a named volume.

Example: docker volume create mysql-data

docker run -d \
  --name mysql \
  -e MYSQL_ROOT_PASSWORD=password \
  -v mysql-data:/var/lib/mysql \
  mysql:8

The MySQL database files are stored in the volume instead of depending only on the container's writable layer.


6. Volume Lifecycle

One of the most important concepts is that the volume has its own lifecycle. If Container A is removed: the volume can still exist. A new container can use the same volume: The new container can access the data stored in the volume.

> docker run -d \

  --name mysql-new \

  -e MYSQL_ROOT_PASSWORD=password \

  -v mysql-data:/var/lib/mysql \

  mysql:8


7. Volumes vs Container Writable Layer

This separation is extremely useful for stateful applications. 

Container
   |
   +-- /var/lib/mysql
          |
          ↓
     mysql-data
          |
     Persistent

Container removed ↓ Volume remains ↓ Data remains