Skip to main content

Volumes in Compose

Imagine a library where each department (service) has its own shelves. If the shelves were temporary, all books would vanish when the department closed. Instead, the library uses permanent storage rooms (volumes) so knowledge is preserved. In Docker Compose, volumes are those storage rooms - they ensure data survives container restarts and can be shared across services.


Volume Foundations

1. What are Volumes in Compose?

  • Volumes are persistent storage managed by Docker.
  • Defined in the docker-compose.yml file under the volumes section.
  • Services can mount volumes to specific paths inside containers.

2. Why Use Volumes?

  • Persistence: Data survives container restarts and removals.
  • Sharing: Multiple services can access the same data.
  • Isolation: Volumes are managed by Docker, independent of the host filesystem.
  • Flexibility: Can be named volumes or bind mounts.

3. Types of Volumes in Compose

  • Named Volumes:
    • Managed by Docker.
    • Defined in the Compose file.
    • Example: dbdata:/var/lib/mysql.
  • Bind Mounts:
    • Map a host directory into a container.
    • Useful for development (syncing source code).
  • Anonymous Volumes:
    • Created automatically if no name is given.
    • Harder to manage, not recommended for production.

4. Defining Volumes in Compose

version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=secret
    volumes:
      - dbdata:/var/lib/mysql

volumes:
  dbdata:

5. Sharing Volumes Between Services

version: '3'
services:
  web:
    image: nginx
    volumes:
      - shareddata:/usr/share/nginx/html
  app:
    image: node:14
    volumes:
      - shareddata:/app/data

volumes:
  shareddata:
  • Both web and app share the same volume shareddata.

Things to Remember

  • Volumes are the preferred way to persist data in Compose.
  • Named volumes are portable and easy to manage.
  • Bind mounts are flexible but tightly coupled to the host.
  • Shared volumes enable collaboration between services.

Hands‑On Lab

Step 1: Define a Volume in Compose

version: '3'
services:
  db:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=secret
    volumes:
      - dbdata:/var/lib/mysql

volumes:
  dbdata:

Step 2: Run the Stack

docker-compose up -d

Step 3: Inspect Volumes

docker volume ls
docker volume inspect dbdata

Step 4: Remove Services but Keep Volume

docker-compose down
docker volume ls
  • Notice the volume still exists.

Step 5: Re‑run Services with the Same Volume

docker-compose up -d
  • Data persists across runs.

Practice Exercise

  1. Create a Compose file with two services:
    • web (nginx) serving static files.
    • editor (alpine) writing files into the same volume.
  2. Define a shared volume sitecontent.
  3. Write files from editor and confirm they appear in web.
  4. Stop and restart the stack — confirm the files persist.

Visual Learning Model

docker-compose.yml
   ├── Service: db → mounts dbdata:/var/lib/mysql
   ├── Service: web → mounts shareddata:/usr/share/nginx/html
   └── Volumes: dbdata, shareddata

The Hackers Notebook

Volumes in Compose provide persistent, shareable storage for services. Named volumes are managed by Docker and ideal for production, while bind mounts are useful for development. By defining volumes in docker-compose.yml, developers ensure data survives container restarts and can be shared across services, making applications reliable and scalable.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 26, 2025