Course contentsShow
← Cloud and Infrastructure
Lesson 1053 of 2,98920. Docker in DepthFree lesson

Volume Management in Compose

Define named volumes, bind mounts, and volume configurations for persistent data across Compose-managed containers.

Volume Management in Compose

You'll learn: How to declare and use volumes in Docker Compose for persistent data that survives container restarts.

The Core Idea

In Docker Compose, volumes let containers store data outside their writable layers, ensuring it persists even when containers are destroyed and recreated. Compose supports named volumes (managed by Docker), bind mounts (mapping host directories), and detailed volume configurations right in your docker-compose.yml file.

Named Volumes

Named volumes are Docker-managed storage that exists independently of containers. They're defined in a top-level volumes: section and referenced by services:

volumes:
  db_data:

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

The volume db_data persists even if you docker compose down the stack.

Bind Mounts

Bind mounts directly map host filesystem paths into containers, useful for development when you want live code reloading:

services:
  web:
    image: nginx
    volumes:
      - ./html:/usr/share/nginx/html

Changes on your host's ./html directory instantly appear inside the container.

Volume Configurations

You can customize volumes with driver options, labels, and external declarations:

volumes:
  app_data:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /mnt/storage

Think of it this way: Named volumes are like rented storage units (Docker manages them), while bind mounts are like giving someone the key to a room in your house (direct host access).

Key Takeaway: Docker Compose simplifies persistent storage with named volumes for production durability and bind mounts for development flexibility, all declared cleanly in YAML.