-
Notifications
You must be signed in to change notification settings - Fork 0
Volumes
PotatoScript edited this page Feb 16, 2025
·
1 revision
Volumes are used to persist data in Docker containers. Unlike bind mounts, volumes are managed by Docker, and they are a preferred way to store data across container restarts or upgrades.
- Named Volumes: Managed by Docker and can be shared between multiple containers.
- Anonymous Volumes: Automatically created by Docker, useful for temporary data.
- Bind Mounts: Links a specific directory on the host machine to a directory inside the container.
| Command | Description |
|---|---|
docker volume create <name> |
Create a named volume. |
docker volume ls |
List all volumes. |
docker volume inspect <name> |
Display details about a volume. |
docker volume rm <name> |
Remove a specific volume. |
docker volume prune |
Remove all unused volumes. |
docker run -d --name my_container -v my_volume:/app/data my_image-
my_volume: The name of the volume. -
/app/data: The directory inside the container where the volume is mounted.
docker run -d --name my_container -v /path/to/host:/app/data my_image-
/path/to/host: Path on the host machine. -
/app/data: Directory inside the container.
version: '3.8'
services:
app:
image: my_app
volumes:
- my_named_volume:/app/data
volumes:
my_named_volume:version: '3.8'
services:
app:
image: node:16
volumes:
- app_data:/app
depends_on:
- db
db:
image: postgres:13
volumes:
- db_data:/var/lib/postgresql/data
volumes:
app_data:
db_data:Run a PostgreSQL container with a persistent data volume:
docker run -d \
--name postgres_container \
-e POSTGRES_USER=admin \
-e POSTGRES_PASSWORD=secret \
-v postgres_data:/var/lib/postgresql/data \
postgres:latest-
postgres_data: Named volume to persist database data.
- Start the first container:
docker run -d --name container1 -v shared_volume:/data alpine
- Start the second container using the same volume:
docker run -d --name container2 -v shared_volume:/data alpine
docker volume inspect my_volumeRemove all volumes not referenced by any container:
docker volume prune- Use Named Volumes: For data that needs to persist and be managed by Docker.
- Avoid Bind Mounts for Sensitive Data: Bind mounts directly link host directories and can expose the host file system.
- Keep Volumes Organized: Use meaningful names to identify volumes easily.
- Clean Up Unused Volumes: Regularly prune unused volumes to free up disk space.
- Run a container to create a backup:
docker run --rm \ -v my_volume:/volume_data \ -v /path/to/backup:/backup \ alpine tar czf /backup/my_volume_backup.tar.gz /volume_data
- Restore from the backup file:
docker run --rm \ -v my_volume:/volume_data \ -v /path/to/backup:/backup \ alpine tar xzf /backup/my_volume_backup.tar.gz -C /volume_data
-
Check Mounted Volumes:
docker inspect <container_id>
- Verify Data is Persisted: Stop the container and restart it. Check if the data remains in the volume.
-
Remove Stale Volumes:
Use
docker volume pruneto clean up unused volumes.