Docker Compose is a powerful orchestration tool designed to simplify managing and deploying multi-container applications using Docker. The docker-compose.yml file streamlines deployment by defining complex applications with multiple services, networks, and volumes within one file. One of the essential aspects of working with Docker Compose is managing persistent data using volumes.

This article explores the importance of using volumes in Docker Compose for handling persistent data and provides a hands-on guide for using volumes effectively.

What Are Docker Volumes?

Docker volumes are a crucial ecosystem component that stores and manages persistent data generated by ephemeral containers. They enable data to persist even after removing or updating a container so that essential application data isn’t lost during routine operations.

Volumes are decoupled from the container’s file system, so you can easily back them up, share them among multiple containers, and migrate them between hosts.

A key advantage of using volumes over bind mounts, which are directory mounts from the host system to a container, is portability. You can quickly move volumes between different hosts or containers, but you must tie bind mounts to a specific directory on the host system.

This portability enables more flexible and efficient data management in container-based applications. Volumes are also compatible with various storage drivers, allowing you to choose the best storage solution for your specific use case.

Types of Docker Volumes

Docker volumes are essential for managing data in container-based applications. They come in two distinct types: named volumes and anonymous volumes. This section delves into the key differences between the two types and demonstrates how to implement them to manage data in your applications.

Named and anonymous volumes serve different purposes and offer varying control and management capabilities. While named volumes are generally preferred for most use cases due to their human-readable identifiers and ease of management, it’s essential to understand how both types function to maximize their benefits.

Named Volumes

Named volumes have a user-defined name, making them easy to identify, manage, and share among multiple containers. Docker creates and manages named volumes and stores their data in a specific location on the host system. This location is typically within the Docker installation directory under a unique ID corresponding to the volume’s name.

Named volumes offer greater control and flexibility, as you can easily reference and manipulate them using their human-readable identifier.

To create a named volume in Docker, run:

docker volume create my_named_volume

Anonymous Volumes

Unlike named volumes, anonymous volumes don’t have a user-defined name. Instead, Docker automatically creates them when you create a container and assign a unique ID to the volume.

It’s generally harder to manage and store volumes due to lacking a human-readable identifier. Since Docker creates them automatically, it’s common to use anonymous volumes for temporary storage. They can also appear if you don’t specify a named volume when creating a container.

To create a container with an anonymous volume, run:

docker run -v /data nginx

This command mounts an anonymous volume to the /data directory inside the container nginx. You can replace nginx with the name of the container you’re mounting the volume into.

How To Create and Manage Volumes With Docker Compose

Docker Compose simplifies creating and managing volumes by allowing you to define them within the docker-compose.yml file. This file contains the configuration of your application’s services, networks, and volumes, enabling easy management of your application’s resources in one place.

1. Define Volumes in Docker Compose

To create a named volume in the docker-compose.yml file, define it under the volumes key. You can also specify the volume driver and options if necessary.

2. Mount Volumes To Containers

To attach a volume to a container, use the volumes key within the service definition in the docker-compose.yml file. Specify the volume name followed by a colon and the container path where you want to mount the volume.

You can also share volumes between multiple containers by using the same volume name.

Here’s an example of creating named volumes called web_data and db_data in your docker-compose.yml file:

version: '3.8'
services:
  web:
    image: nginx
    volumes:
      - web_data:/var/www/html
  web-test:
    image: nginx
    volumes:
      - web_data:/var/www/html # Web and web test share the web_data volume
  db:
    image: mysql
    volumes:
      - db_data:/var/lib/mysql
volumes:
  web_data:
  db_data:
    driver: local # Define the driver and options under the volume name
    driver_opts:
      type: none
      device: /data/db_data
      o: bind

This example defines two named volumes. It then mounts the volumes to their respective containers under specific paths. Next, it mounts the web_data volume to the /var/www/html directory in the web container and the db_data volume to the /var/lib/mysql directory in the db container.

The containers web and web-test share the web_data volume, allowing them to access and modify the same volume of data.

By defining and managing volumes within the docker-compose.yml file, you can easily create, update, and delete volumes as needed without manually managing them using Docker commands. This streamlined process allows you to focus on developing and deploying your application while Docker Compose handles the underlying resource management.

How To Work With Docker Compose and Volume Commands

Docker Compose provides several commands that help you effectively manage your application and its resources. Let’s review these commands and how they relate to volumes in more detail:

  • docker compose up creates and starts your application, including its services, networks, and volumes. If you define a named volume in the docker-compose.yml file before it exists, this command will create it automatically.
  • docker compose down stops and removes your application’s services and networks. By default, it doesn’t remove named volumes. To remove named volumes, use the --volumes or -v flag.
  • docker compose ps lists the containers and their current status, including volume-related information.
  • docker compose config validates and displays the effective configuration generated from the docker-compose.yml file, including volume definitions.

List Volumes

To list all volumes, use ls:

docker volume ls

The output displays all named volumes, including those created by Docker Compose.

Inspect Volumes

To view the details of a specific volume, use inspect . It outputs information about the volume, such as its name, driver, mount point, and options:

docker volume inspect db_data

The details of the volume are given in JSON format. For example, considering the docker-compose.yml file provided above, this is the returned output:

[
    {
        "CreatedAt": "some-date-here",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/path/on/host/where/volume/is/mounted",
        "Name": "db_data",
        "Options": {
            "device": "/data/db_data",
            "o": "bind",
            "type": "none"
        },
        "Scope": "local",
        "Status": {
            "Mounts": [
                ...
            ]
        }
    }
]

Remove a Docker Volume By Name

To remove a Docker volume, you can use docker volume rm followed by the volume name:

docker volume rm volume-name

Clean Up Unused Volumes

To remove unused volumes, use prune:

docker volume prune

This command helps clean up your development environment and reclaim storage space. It removes all unused volumes not associated with containers, including those that Docker Compose creates.

By leveraging these commands and their volume-related features, you can effectively manage your application’s resources, ensuring optimal performance and efficient use of storage space.

Docker Compose Versions

As of July 2023, Docker Compose V1 stopped receiving updates. It’s also no longer available in new releases of Docker Desktop. However, Docker Desktop continues to support a docker-compose alias to redirect commands to docker compose for convenience and improved compatibility with third-party tools and scripts.

To switch any existing code from Docker Compose version 1 to 2, simply replace the dash with a space. For example, docker-compose up becomes docker compose up.

With version 2, you can use the &compose command directly in the Docker command-line interface (CLI), switch the Docker context to build a container on a cloud service, and use Amazon ECS and Microsoft ACI.

Summary

This article highlighted the importance of using volumes with Docker Compose for managing persistent data. Volumes are a crucial component in the Docker ecosystem, enabling you to store and manage data that Docker containers generate. With Docker volumes, important application data persists even after you delete or update a container, helping maintain your application’s integrity and consistency.

Docker Compose offers a streamlined approach to creating and managing volumes within the docker-compose.yml file. This method simplifies your development process and ensures efficient use of resources.

Using volumes also makes development flexible and efficient, with Docker Compose providing various volume-related commands to help you oversee application resources effectively. Leveraging these commands allows you to easily create, inspect, and clean up volumes.

When you host your application with Kinsta, you have a fast, secure, and reliable infrastructure, with your projects deployed on Google Cloud Platform’s Premium Tier Network and C2 machines. Choose between 25 data centers and an HTTP/3-enabled CDN.

Stay secure with isolated container technology, two strong firewalls, and advanced Cloudflare-powered DDoS protection. You can also integrate apps or automate workflows with the Kinsta API and deploy them with Docker.

Kinsta provides high-quality resources and content for web developers, including in-depth tutorials and guides for various languages and tools like Docker Compose. Read our Docker-specific content on the Kinsta blog to learn more about Docker.

Marcia Ramos Kinsta

I'm the Editorial Team Lead at Kinsta. I'm a open source enthusiast and I love coding. With more than 7 years of technical writing and editing for the tech industry, I love collaborating with people to create clear and concise pieces of content and improve workflows.