Volumes for Docker Compose

By February 13, 2018Docker Volumes

Search Knowledge Base by Keyword

Volumes for Docker Compose

Docker Compose allows users to define an application which consists of multiple services. A definition file describes the inter-dependencies between the services, including details such network or storage requirements, in a manner that is easy to maintain.

Compose includes the ability to attach volumes to any service that has persistent storage requirements. With Compose version 3 and the Storidge volume plugin, you can easily create persistent storage for any stateful applications.

WordPress MySQL Example

This guide uses an example to show how the CIO volume plugin is called within a Docker Compose file. Additional examples of Compose files with the CIO volume plugin are available here.

The example below shows a file with two services, a MySQL database (key db:) and a WordPress (key wordpress:) installation. The MySQL service will use volume mysql-data to persist data which is accessed inside the container at path /var/lib/mysql.

A volume specification (volumes:) at the end of the wordpress-mysql.yml file completes the definition of the volume. Note the key mysql-data names the volume and calls the cio driver/volume plugin. Under driver options, the volume definition also specifies a MYSQL profile to be used for creating the volume.

# wordpress-mysql.yml
version: '3'
services:
  db:
    image: mysql:5.7
    volumes:
        # Pass volume named mysql-data to mysql container
      - "mysql-data:/var/lib/mysql"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    links:
      - db
    ports:
      - "8080:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_PASSWORD: wordpress

# Add volumes definition to create volume mysql-data referenced above
volumes:
  mysql-data:
    driver: cio
    driver_opts:
      profile: "MYSQL"

Explore Further

Docker Storage

Volumes
Volumes for Containers
Volumes for Services
Volumes and Dockerfiles

Persistent Storage Cookbook

CIO Volume with Minio
Cloud Scale Minio with CIO