Creating and Using Docker Volumes

Tutorial 1 of 5

Creating and Using Docker Volumes

Introduction

Goal

The goal of this tutorial is to understand how to create and use Docker Volumes. Docker Volumes offer a way to store data that originates from and is used by Docker containers, making it easier to manage data persistence and sharing data among multiple containers.

Learning Outcome

By the end of this tutorial, you will be able to:

  • Create a Docker volume
  • Attach it to a container
  • Manipulate the data within

Prerequisites

  • Basic understanding of Docker
  • Docker installed on your local machine

Step-by-Step Guide

Docker volumes are stored in a part of the host filesystem which is managed by Docker (/var/lib/docker/volumes/ on Linux). Unlike bind mounts, they are not dependent on the directory structure of the host machine.

To create a Docker volume, we use the docker volume create command. To attach it to a container, we use the -v option followed by the volume name when running docker run.

Creating a Docker Volume

docker volume create my_volume

This command creates a Docker volume named my_volume. You can verify this by running the docker volume ls command, which lists all the Docker volumes.

Attaching Volume to a Container

docker run -d --name devtest -v my_volume:/app nginx:latest

This command runs a Docker container named devtest using the nginx:latest image, and attaches the my_volume to the /app directory in the container. The -d option runs the container in the background.

Code Examples

Example 1: Creating and Listing Docker Volumes

# Create a Docker volume
docker volume create my_volume

# List Docker volumes
docker volume ls

This code snippet first creates a Docker volume named my_volume, then lists all Docker volumes. You should see my_volume in the output.

Example 2: Attaching Docker Volume to a Container

# Run a container with a volume attached
docker run -d --name devtest -v my_volume:/app nginx:latest

This command runs a Docker container named devtest using the nginx:latest image, and attaches the my_volume to the /app directory in the container. You should see the container running in the output of docker ps.

Summary

This tutorial covered how to create and use Docker volumes. We learned how to create a Docker volume using docker volume create, and how to attach it to a container using the -v option in docker run.

For further learning, you can explore how to share Docker volumes between containers, and how to backup, restore, or migrate data volumes.

Practice Exercises

  1. Exercise 1: Create a Docker volume named "vol1" and list all Docker volumes to verify its creation.

  2. Exercise 2: Run a container named "my_container" using the alpine image, and attach "vol1" to the /data directory in the container. Verify by running docker ps.

Solutions

  1. Solution 1:
# Create a Docker volume
docker volume create vol1

# List Docker volumes
docker volume ls
  1. Solution 2:
# Run a container with a volume attached
docker run -d --name my_container -v vol1:/data alpine

Tips for Further Practice

  • Try to share a volume between two containers.
  • Attempt to backup and restore a Docker volume.
  • Experiment with migrating a Docker volume to another container.