Skip to content

Volumes

PotatoScript edited this page Feb 16, 2025 · 1 revision

Docker Volumes Cheat Sheet

Overview

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.


Key Concepts

  • 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.

Commands for Managing Volumes

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.

Using Volumes in Containers

Attach a Volume to a Container

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.

Using Bind Mounts

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.

Managing Volumes in docker-compose.yml

Define Named Volumes

version: '3.8'

services:
  app:
    image: my_app
    volumes:
      - my_named_volume:/app/data

volumes:
  my_named_volume:

Example with Multiple Services

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:

Examples of Volume Usage

Persist Database 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.

Share Data Between Containers

  1. Start the first container:
    docker run -d --name container1 -v shared_volume:/data alpine
  2. Start the second container using the same volume:
    docker run -d --name container2 -v shared_volume:/data alpine

Inspecting and Cleaning Volumes

Inspect a Volume

docker volume inspect my_volume

Prune Unused Volumes

Remove all volumes not referenced by any container:

docker volume prune

Best Practices

  1. Use Named Volumes: For data that needs to persist and be managed by Docker.
  2. Avoid Bind Mounts for Sensitive Data: Bind mounts directly link host directories and can expose the host file system.
  3. Keep Volumes Organized: Use meaningful names to identify volumes easily.
  4. Clean Up Unused Volumes: Regularly prune unused volumes to free up disk space.

Advanced Example: Using Volumes with Backups

Create a Volume Backup

  1. 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 a Volume Backup

  1. 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

Volume-Related Issues and Debugging

  1. Check Mounted Volumes:
    docker inspect <container_id>
  2. Verify Data is Persisted: Stop the container and restart it. Check if the data remains in the volume.
  3. Remove Stale Volumes: Use docker volume prune to clean up unused volumes.