TechyPilots Info
TechyPilots.INFO
Home/Tutorials/Docker/Docker Volumes
Docker Storage

Docker Volumes Tutorial

Learn how Docker volumes provide persistent storage for containers. Understand named volumes, anonymous volumes, bind mounts, volume lifecycle, Docker volume commands, and Docker Compose storage.

Level: Beginner
Topics: 6 Core Areas

Docker Storage Learning Path

Essential Volume Skills

Persistent Container Data

Named Volumes

Anonymous Volumes

Bind Mounts

Volume Management

Docker Compose Volumes

Introduction

What are Docker volumes?

Docker volumes are persistent data stores managed by Docker. They allow application data to exist independently from a container's writable layer.

When a volume is mounted into a container, applications can read and write data to the mounted location while the volume is managed separately from the container lifecycle.

Volumes are useful for databases, uploaded files, application data, backups, and other information that should persist beyond a specific container instance.

Core Concepts

Docker volume fundamentals

Understand the main concepts behind persistent storage and filesystem mounts in Docker.

01

What are Docker Volumes?

Docker volumes provide persistent storage that exists independently from a container's writable layer.

Key Topics

  • Persistent data
  • Container storage
  • Docker-managed storage
  • Data lifecycle
02

Named Volumes

Named volumes have a specific name and can be reused by containers on the same Docker host.

Key Topics

  • Volume names
  • Reuse
  • Shared storage
  • Volume management
03

Anonymous Volumes

Anonymous volumes are created without a user-defined name and receive an automatically generated identifier.

Key Topics

  • Automatic creation
  • Generated names
  • Container storage
  • Volume lifecycle
04

Bind Mounts

Bind mounts connect a directory or file from the host filesystem directly into a container.

Key Topics

  • Host directories
  • Development
  • Source code sharing
  • Read-only mounts
05

Volume Lifecycle

Volumes can continue to exist after containers are removed, allowing persistent data to be managed separately.

Key Topics

  • Container removal
  • Persistent data
  • Volume cleanup
  • Data management
06

Docker Compose Volumes

Docker Compose can define named volumes and attach them to one or more application services.

Key Topics

  • Compose configuration
  • Shared volumes
  • Service storage
  • Multi-container applications

Storage Types

Types of Docker storage mounts

Docker applications can use Docker-managed volumes or bind mounts depending on whether the storage should be managed by Docker or directly connected to the host filesystem.

Named Volume

A Docker-managed volume with a specific name that can be reused and managed independently from containers.

my-data:/app/data

Anonymous Volume

A Docker-managed volume without a user-defined name, commonly created automatically for container storage.

/app/data

Bind Mount

A directory or file from the host filesystem mounted directly into a container.

./app:/usr/src/app

Practical Example

Create and use a named volume

You can explicitly create a Docker volume and then mount it into a container.

The volume can persist independently from the container, allowing a new container to access the same data when the same named volume is mounted.

Example Commands

# Create a named volume
docker volume create app-data

# Run a container with the volume
docker run -d \
  --name my-app \
  -v app-data:/app/data \
  nginx

# Inspect the volume
docker volume inspect app-data

Volume Management

Essential Docker volume commands

Docker provides commands for creating, listing, inspecting, removing, and cleaning up volumes.

docker volume create my-data

Create a named Docker volume.

docker volume ls

List Docker volumes available on the system.

docker volume inspect my-data

Display detailed information about a Docker volume.

docker volume rm my-data

Remove a Docker volume that is no longer required.

docker volume prune

Remove unused Docker volumes.

docker run -v my-data:/data nginx

Run a container with a named volume mounted inside the container.

Bind Mounts

Share files between the host and container

A bind mount connects a file or directory from the host filesystem directly into a container.

Bind mounts are commonly useful during development when application source code should be available both on the host system and inside a running container.

Because bind mounts depend on host paths, applications using them can be more tightly connected to the filesystem structure of the Docker host.

Bind Mount Example

docker run -it \
  --mount type=bind,src=$(pwd),dst=/app \
  node:22-alpine

This example mounts the current directory from the Docker host into the container at /app.

Docker Compose

Use volumes with Docker Compose

Docker Compose allows named volumes to be defined at the top level and mounted into one or more services.

This approach is useful for multi-container applications where services such as databases require persistent storage.

compose.yaml Example

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

volumes:
  postgres-data:

Best Practices

Working with Docker volumes

Use Volumes for Persistent Data

Store important application and database data outside the container writable layer.

Use Clear Volume Names

Use descriptive names to make volumes easier to identify and manage.

Inspect Before Removing

Check whether a volume contains important data before deleting it.

Use Bind Mounts Carefully

Host filesystem access can allow container processes to modify files outside the container.

Consider Read-Only Mounts

Use read-only mounts when containers only need to read files.

Plan Backup and Recovery

Create an appropriate backup and recovery strategy for persistent application data.

Learning Roadmap

How to learn Docker volumes

Follow this roadmap to understand Docker storage and persistent data management.

Step 1

Understand Container Storage

Learn why data written only to a container can be lost when the container lifecycle ends.

Step 2

Create a Named Volume

Create and inspect a Docker-managed volume using Docker CLI commands.

Step 3

Mount a Volume

Attach a volume to a container and store application data inside the mounted location.

Step 4

Learn Bind Mounts

Understand how host directories can be mounted into containers for development and file sharing.

Step 5

Manage Volume Lifecycle

Learn how to list, inspect, remove, and clean up Docker volumes.

Step 6

Use Docker Compose

Define persistent storage for multi-container applications using Docker Compose.

Next Step

Start managing persistent container data

Practice creating Docker volumes, mounting them into containers, using bind mounts during development, and managing persistent storage with Docker Compose.