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
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.
Docker Storage Learning Path
Persistent Container Data
Named Volumes
Anonymous Volumes
Bind Mounts
Volume Management
Docker Compose Volumes
Introduction
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
Understand the main concepts behind persistent storage and filesystem mounts in Docker.
Docker volumes provide persistent storage that exists independently from a container's writable layer.
Key Topics
Named volumes have a specific name and can be reused by containers on the same Docker host.
Key Topics
Anonymous volumes are created without a user-defined name and receive an automatically generated identifier.
Key Topics
Bind mounts connect a directory or file from the host filesystem directly into a container.
Key Topics
Volumes can continue to exist after containers are removed, allowing persistent data to be managed separately.
Key Topics
Docker Compose can define named volumes and attach them to one or more application services.
Key Topics
Storage Types
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.
A Docker-managed volume with a specific name that can be reused and managed independently from containers.
my-data:/app/dataA Docker-managed volume without a user-defined name, commonly created automatically for container storage.
/app/dataA directory or file from the host filesystem mounted directly into a container.
./app:/usr/src/appPractical Example
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
Docker provides commands for creating, listing, inspecting, removing, and cleaning up volumes.
docker volume create my-dataCreate a named Docker volume.
docker volume lsList Docker volumes available on the system.
docker volume inspect my-dataDisplay detailed information about a Docker volume.
docker volume rm my-dataRemove a Docker volume that is no longer required.
docker volume pruneRemove unused Docker volumes.
docker run -v my-data:/data nginxRun a container with a named volume mounted inside the container.
Bind Mounts
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
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
Store important application and database data outside the container writable layer.
Use descriptive names to make volumes easier to identify and manage.
Check whether a volume contains important data before deleting it.
Host filesystem access can allow container processes to modify files outside the container.
Use read-only mounts when containers only need to read files.
Create an appropriate backup and recovery strategy for persistent application data.
Learning Roadmap
Follow this roadmap to understand Docker storage and persistent data management.
Learn why data written only to a container can be lost when the container lifecycle ends.
Create and inspect a Docker-managed volume using Docker CLI commands.
Attach a volume to a container and store application data inside the mounted location.
Understand how host directories can be mounted into containers for development and file sharing.
Learn how to list, inspect, remove, and clean up Docker volumes.
Define persistent storage for multi-container applications using Docker Compose.
Continue Learning
Next Step
Practice creating Docker volumes, mounting them into containers, using bind mounts during development, and managing persistent storage with Docker Compose.