Introduction
By default, data inside a container is ephemeral; it disappears when the container is removed. This behavior is unsuitable for databases or applications that need to store state. This lesson introduces Docker Volumes, the preferred mechanism for persisting data generated by and used by Docker containers. You'll learn how volumes provide a robust way to manage data independent of the container's lifecycle.
Key Concepts
The Problem with Ephemeral Container Data
When a container stops and is removed, all changes made within its writable layer are lost. For critical application data, this is unacceptable. Applications like databases, content management systems, or any service requiring state persistence need a mechanism to store data outside the container's lifecycle.
Docker Volumes
Docker Volumes are file systems mounted onto containers. They are managed by Docker and stored in a part of the host filesystem (/var/lib/docker/volumes/ on Linux) that is entirely separate from the Docker daemon's managed areas. This separation ensures that data persists even if the container is stopped, removed, or replaced.
-
Managed by Docker: Docker handles the creation, management, and removal of volumes.
-
Persistent: Data stored in volumes persists independently of container lifecycle.
-
Portable: Volumes can be backed up, migrated, and shared between containers.
-
Performance: Offer better performance than bind mounts for many use cases, especially for databases.
Volume Types
-
Named Volumes: The most common type. You create and manage them by name (e.g.,
my_data). Docker ensures that the volume exists and mounts it to the specified path in the container.- Best for general-purpose persistent storage.
-
Anonymous Volumes: Similar to named volumes but are not given an explicit name. Docker assigns a random unique name. They are less common as they are harder to reference and manage.
Example/Code
Creating and Using a Named Volume
- Create a named volume:
bash
undefined
docker volume create my-db-data ```
- Inspect the volume: See where it's stored on the host.
bash
undefined
docker volume inspect my-db-data ```
- Run a PostgreSQL container with the volume: We'll mount
my-db-datato/var/lib/postgresql/data, which is where PostgreSQL stores its data.bashundefined
docker run -d
--name postgres-db
-e POSTGRES_PASSWORD=mysecretpassword
-v my-db-data:/var/lib/postgresql/data
postgres:15
```
- Verify data persistence: Stop and remove the container, then run a new one with the same volume.
bash
undefined
docker stop postgres-db
docker rm postgres-db
docker run -d
--name postgres-db-new
-e POSTGRES_PASSWORD=mysecretpassword
-v my-db-data:/var/lib/postgresql/data
postgres:15
```
The data from the previous container will still be available in postgres-db-new.
- Clean up volumes: Be careful, this will delete your data.
bash
undefined
docker stop postgres-db-new docker rm postgres-db-new docker volume rm my-db-data ```
Summary/Key Takeaways
-
Container data is ephemeral by default; volumes solve this by persisting data outside the container.
-
Docker Volumes are Docker-managed filesystems on the host.
-
Named volumes (
docker volume create,-v my_volume:/path/in/container) are the standard for persistent storage. -
Volumes offer superior performance and portability compared to other persistence methods for many use cases.