How to deal with persistent storage (e.g. databases) in Docker?

Docker volumes provide a robust solution for persistent storage in Docker containers. Unlike bind mounts, which depend heavily on the host's directory structure, volumes are completely managed by Docker and offer better portability and control.

When using volumes, Docker creates a new directory within its storage directory on the host machine. This approach is superior to persisting data on the container's writable layer because volumes don't increase container size and their contents exist independently of the container's lifecycle.

Advantages of Docker Volumes

  • Portability − Easier to migrate or back up across different environments

  • Management − Simple to manage using Docker APIs or CLI commands

  • Cross-platform − Work consistently on both Windows and Linux systems

  • Security − Safe and easy to share between containers

  • Extensibility − Volume drivers enable cloud storage, encryption, and remote storage

  • Pre-population − Can be pre-populated with content from other containers

Volume Management Commands

Creating and Managing Volumes

To create a new volume −

$ docker volume create my-vol

To list all existing volumes −

$ docker volume ls

To inspect volume details −

$ docker volume inspect my-vol

To remove a volume −

$ docker volume rm my-vol

Mounting Volumes to Containers

Using --mount Flag

The --mount option uses key-value pairs separated by commas. It provides verbose syntax and is required when specifying driver options −

$ docker run -it -p 8080:80 --mount source=my-vol,target=/myapp nginx:latest

Using -v Flag

The -v flag uses three colon-separated fields in a specific order −

$ docker run -it -v my-vol:/app/data debian

Database Persistence Example

For database containers like MySQL, you can ensure data persistence by mounting a volume to the database directory −

$ docker run -d --name mysql-db \
  -e MYSQL_ROOT_PASSWORD=mypassword \
  --mount source=mysql-data,target=/var/lib/mysql \
  mysql:8.0

Sharing Volumes Between Containers

Multiple containers can share the same volume using the --volumes-from option, allowing data sharing and collaboration −

$ docker run -d --name data-container --mount source=shared-vol,target=/data alpine
$ docker run -it --volumes-from data-container ubuntu bash

Comparison of Storage Options

Storage Type Management Portability Performance Use Case
Volumes Docker managed High Good Production databases
Bind Mounts Host managed Low Excellent Development, config files
tmpfs Memory based N/A Excellent Temporary data, secrets

Conclusion

Docker volumes provide the most reliable solution for persistent storage in containerized applications. They offer better portability, security, and management compared to bind mounts, making them ideal for production database deployments where data persistence is critical.

Updated on: 2026-03-17T09:01:38+05:30

284 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements