Docker Volumes & Persistent Storage

Docker Volumes & Persistent Storage

Sufi Aurangzeb Hossain
October 9, 2025
5 min read
DockerDevOps

By default, Docker containers are ephemeral, meaning all data inside a container is lost once it's removed. To persist data beyond container lifecycles, Docker provides **volumes**, **bind mounts**, and **tmpfs**. This article explains these concepts and demonstrates how to use them effectively.

Docker Volumes & Persistent Storage – Managing Data in Containers

By default, Docker containers are ephemeral, meaning all data inside a container is lost once it's removed. To persist data beyond container lifecycles, Docker provides volumes, bind mounts, and tmpfs. This article explains these concepts and demonstrates how to use them effectively.

Why Persistent Storage?

  • Containers are temporary – removing or recreating them deletes their filesystem.
  • Applications like databases (MySQL, PostgreSQL, MongoDB) need permanent storage.
  • Volumes allow you to store, share, and back up container data.

Storage Options in Docker

  1. Volumes – Managed by Docker, stored in */var/lib/docker/volumes/*.
  2. Bind Mounts – Maps a host machine directory into the container.
  3. tmpfs Mounts – Stores data in memory only (fast but not persistent).

Volumes are the best option for persistent storage because they are portable, easy to back up, and independent of the host file structure.

docker volume create my_volume
docker run -d --name my_container -v my_volume:/app/data nginx

This creates a named volume my_volume and mounts it at */app/data* inside the container.

docker volume ls
docker volume inspect my_volume

Use these commands to list and inspect volumes.

Bind Mounts

Bind mounts link a specific folder from the host to a container. Useful for development when you need live code changes reflected inside the container.

docker run -d --name dev_container -v $(pwd):/usr/src/app node:18

This mounts the current directory from your host into */usr/src/app* inside the container. Any local changes immediately sync into the container.

tmpfs Mounts

  • Stores data in memory only.
  • Very fast, but lost once the container stops.
  • Useful for caching and sensitive information.
docker run -d --name cache_container --mount type=tmpfs,destination=/app/cache nginx

This creates a tmpfs mount at */app/cache*, useful for temporary runtime data.

Example: Persisting a Database

Let's run a MySQL container with a persistent volume for database files.

docker volume create mysql_data
docker run -d --name mysql_db -e MYSQL_ROOT_PASSWORD=secret -v mysql_data:/var/lib/mysql mysql:8

Even if the container is removed and recreated, the data inside *mysql_data* volume persists.

Cleaning Up Volumes

Remove an unused volume:

docker volume rm my_volume

Prune all unused volumes:

docker volume prune

Real-World Use Cases

  • Volumes → Production databases, logs, user uploads.
  • Bind Mounts → Local development for syncing code changes.
  • tmpfs → High-performance caching, sensitive temporary files.

Example: A Node.js app can use bind mounts in dev mode to reflect code changes instantly, and volumes in production to store database data.

Summary

  • Volumes are the best way to persist data in Docker.
  • Bind mounts are great for development.
  • tmpfs mounts are useful for temporary data.

In the next article, we'll cover Docker Compose – Defining Multi-Container Applications Easily.