Multiple stacks of newspapers
Developer Tools  / Development

Docker Volumes: A Comprehensive Guide

Discover the power of Docker volumes in this comprehensive guide! Learn how to create, mount, and share volumes to simplify your workflow.

Christina Harker, PhD

Christina Harker, PhD


Docker is a popular containerization platform that allows developers to create, deploy, and manage applications in a more efficient and streamlined manner. One of the key features that Docker provides is the ability to use volumes to store data that can be accessed by containers. In this article, we will explore the different types of Docker volumes and their use cases.

What are Docker Volumes?

Docker volumes are a way of storing data that can be shared across multiple containers. They are similar to files or directories on a file system, but they can be managed by Docker and can be easily shared among different containers. Docker volumes are used to store data that needs to persist between container restarts or that needs to be shared among different containers.

Docker volumes come in different types and can be managed using different methods. Some of the most commonly used Docker volume types are:

  • Named volumes: Named volumes are created and managed by Docker. They are stored in a directory on the host machine and can be easily accessed by multiple containers. Named volumes are created using the docker volume create command.

  • Host volumes: Host volumes are directories or files on the host machine that are mounted into a container. Host volumes are created using the docker run command with the -v flag.

  • Anonymous volumes: Anonymous volumes are created by Docker and are attached to a container when it is created. They are not named and cannot be reused. Anonymous volumes are created using the -v flag in the docker run command.

Docker volumes can be managed using different methods, including Docker CLI commands and Docker Compose. In the following sections, we will explore some of the most common Docker volume management methods.

Docker CLI Commands for Managing Volumes

Docker provides several CLI commands for managing volumes. These commands include:

  • docker volume create: Creates a new named volume.

  • docker volume ls: Lists all the volumes that are currently available on the host machine.

  • docker volume rm: Removes a named volume.

  • docker volume inspect: Provides detailed information about a named volume.

  • docker run -v: Mounts a host directory or file into a container.

  • docker run --mount: Mounts a named volume or a host directory or file into a container.

Using these commands, developers can easily manage Docker volumes and access them from different containers.

Docker Compose for Managing Volumes

Docker Compose is a tool that allows developers to define and manage multi-container Docker applications. Docker Compose uses YAML files to define the services, networks, and volumes that make up a multi-container application. Docker Compose provides several options for managing volumes, including:

  • Creating named volumes using the volumes keyword.

  • Mounting host directories or files using the volumes keyword and specifying the host path.

  • Mounting named volumes using the volumes keyword and specifying the name of the volume.

Here is an example of how to use Docker Compose to create and manage volumes:

version: '3'
    image: nginx
      - type: volume
        source: my-named-volume
        target: /usr/share/nginx/html
      - type: bind
        source: ./html
        target: /usr/share/nginx/html
      - type: volume
        source: my-other-named-volume
        target: /var/log/nginx

In this example, we define a web service that uses the nginx image. We create three volumes: two named volumes (my-named-volume and my-other-named-volume) and one host volume (./html). The named volumes are used to store the HTML content and the nginx logs, while the host volume is used to store the HTML.

Stay informed! Join our LinkedIn and X/Twitter community to access exclusive insights and be the first to know about our latest blog posts.

Experience Divio's Open Cloud with our 30-day Free Trial!
Easily deploy your web applications and explore customized solutions.
Sign up now!