Docker Volume Management

DockerDockerBeginner
Practice Now

Introduction

The section will cover the usage of persistent data stores in a Docker environment called volumes. You will learn the characteristics of persistent data stores, such as the difference between internal and external volumes and how and when to use them.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/exec("Execute Command in Container") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/VolumeOperationsGroup -.-> docker/cp("Copy Data Between Host and Container") docker/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") subgraph Lab Skills docker/run -.-> lab-7769{{"Docker Volume Management"}} docker/ls -.-> lab-7769{{"Docker Volume Management"}} docker/exec -.-> lab-7769{{"Docker Volume Management"}} docker/inspect -.-> lab-7769{{"Docker Volume Management"}} docker/create -.-> lab-7769{{"Docker Volume Management"}} docker/cp -.-> lab-7769{{"Docker Volume Management"}} docker/volume -.-> lab-7769{{"Docker Volume Management"}} end

Data Volume Management

Data volumes are a standard operation for data persistence in Docker, and in this section, I will take you through the standard operations on data volumes in practice.

Target

Your goal is to create a data volume called my-vol using the docker command, and add a file called test.txt to its volume.

Result Example

Here's an example of what you should be able to accomplish by the end of this challenge:

  1. Create a data volume named my-vol.
labex:~/ $ docker volume ls
DRIVER    VOLUME NAME
local     jenkins-data
local     minikube
local     my-vol
  1. Get Mountpoint by data volume details.
[
  {
    "CreatedAt": "2024-01-20T17:37:33+08:00",
    "Driver": "local",
    "Labels": {},
    "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
    "Name": "my-vol",
    "Options": {},
    "Scope": "local"
  }
]
  1. Go to Mountpoint and create the test.txt file.
root@iZj6cj604ytlr2cacq6vi2Z:/var/lib/docker/volumes/my-vol/_data## pwd
/var/lib/docker/volumes/my-vol/_data
root@iZj6cj604ytlr2cacq6vi2Z:/var/lib/docker/volumes/my-vol/_data## ls
test.txt

Requirements

To complete this challenge, you will need:

  • Know how to create a data volume using the docker command.
  • Create a file using the root user.
  • Swithch to the root user using sudo su, Set the user root password using sudo passwd root.
โœจ Check Solution and Practice

Creating Containers with Data Volume

In this section, we will create containers using docker data volumes and then make copies of the files.

Target

You aim to create a docker volume named nginx-vol, then start a container named nginx and mount it, and finally copy the /home/labex/project/index.html file to the nginx container.

Result Example

Here's an example of what you should be able to accomplish by the end of this challenge:

  1. Create a data volume called nginx-vol.
labex:~/ $ docker volume ls
DRIVER    VOLUME NAME
local     jenkins-data
local     minikube
local     my-vol
local     nginx-vol
  1. Create a container named nginx based in nginx image that will mount its /usr/share/nginx/html directory to the nginx-vol data volume.
labex:~/ $ docker ps | grep nginx
c39296aaf39e   nginx     "/docker-entrypoint.โ€ฆ"   35 seconds ago   Up 31 seconds   80/tcp    nginx
  1. Copy the /home/labex/project/index.html file to the nginx container.
labex:/tmp/ $ pwd
/tmp
labex:/tmp/ $ cat index.html
hello world
  1. Copy the /home/labex/project/index.html file to the nginx container.
labex:/tmp/ $ docker exec nginx cat /usr/share/nginx/html/index.html
hello world

Requirements

To complete this challenge, you will need:

  • Know how to create a data volume using the docker command.
  • Know how to start a container.
  • Know how to copy local files to containers.
โœจ Check Solution and Practice

Create Containers with Host Path

In many cases, we are not used to data volumes but instead. Use the native directory directly for data persistence. In this section, we will use the native directory to create containers.

Target

Your goal is to create a container that uses Host Path, then copy the /home/labex/project/nginx.txt file into the container and verify that the files in that container are the same as those in Host Path.

Result Example

Here's an example of what you should be able to accomplish by the end of this challenge:

  1. Create an nginx directory in the /tmp directory.
labex:/tmp/ $ ls | grep nginx
nginx
  1. Start the nginx-host container based on the nginx image that mounts the /tmp/nginx directory to the /usr/share/nginx/html directory in the container.
labex:/tmp/ $ docker ps | grep nginx-host
6fc2d40e12df   nginx     "/docker-entrypoint.โ€ฆ"   44 seconds ago   Up 43 seconds   80/tcp    nginx-host
  1. Check the specific mount details with the docker inspect command.
"Mounts": [
   {
         "Type": "bind",
         "Source": "/tmp/nginx",
         "Destination": "/usr/share/nginx/html",
         "Mode": "",
         "RW": true,
         "Propagation": "rprivate"
   }
],
  1. Copy the /home/labex/project/nginx.txt file to the /usr/share/nginx/html directory of the nginx-host container.
labex:/tmp/ $ pwd
/tmp
labex:/tmp/ $ cat nginx.txt
hello nginx
  1. Copy the /home/labex/project/nginx.txt file to the /usr/share/nginx/html directory of the nginx-host container.
labex:nginx/ $ docker exec nginx-host cat /usr/share/nginx/html/nginx.txt
hello nginx
  1. Check the /tmp/nginx directory on the local machine to see if there is a nginx.txt file.
labex:nginx/ $ pwd
/tmp/nginx
labex:nginx/ $ cat nginx.txt
hello nginx

Requirements

To complete this challenge, you will need:

  • Know the Host Path path you want to use.
  • Know how to use some relevant commands inside the container to verify if the host directory or file can be accessed.
  • Know how to start the container using Host Path.
  • Be familiar with basic Linux commands, such as cd, mkdir, touch, etc.
โœจ Check Solution and Practice

Sharing Data between Containers

In a containerized environment, sharing data from one container to another is sometimes necessary. In this case, various techniques can be used to achieve data sharing, and this challenge will explore how to share data between containers.

Target

Your goal is to implement data sharing between two containers. Specifically, you will use Host Path to start container A, then start another container B and mount A's storage volume, eventually enabling data sharing between the two containers.

Result Example

Here's an example of what you should be able to accomplish by the end of this challenge:

  1. Create a /tmp/share directory on the local host.
labex:/tmp/ $ ls -ld /tmp/share
drwxr-xr-x 2 labex labex 4096 Jan 20 18:20 /tmp/share
  1. Create a container named nginx-share based on the nginx image and mount the local /tmp/share directory to the /usr/share/nginx/html directory in the container.
labex:/tmp/ $ docker ps | grep nginx-share
216d02d94f39   nginx     "/docker-entrypoint.โ€ฆ"   55 seconds ago   Up 55 seconds   80/tcp    nginx-share
  1. Create a container named busybox-share based on the busybox image and mount it from nginx-share container.
labex:/tmp/ $ docker ps | grep busy
30bf04aee681   busybox   "sh"                     43 seconds ago   Up 42 seconds             busybox-share
  1. Copy the /home/labex/project/share.txt file to the busybox-share container.
labex:/tmp/ $ pwd
/tmp
labex:/tmp/ $ cat share.txt
hello share
  1. Copy the /home/labex/project/share.txt file to the nginx-share container.
labex:/tmp/ $ docker exec nginx-share cat /usr/share/nginx/html/share.txt
hello share
  1. Go to the busybox-share container to see if the share.txt file exists.
labex:/tmp/ $ docker exec busybox-share cat /usr/share/nginx/html/share.txt
hello share

Requirements

To complete this challenge, you will need:

  • Be familiar with the basic operations and concepts of Docker containers.
  • Use Docker command line tools to create, run, and stop containers.
  • Understand the concept and usage of data volumes.
  • Understand how to mount a data volume in a container.
  • Be familiar with basic Linux commands, such as cd, mkdir, touch, etc.
โœจ Check Solution and Practice

Summary

Congratulations on completing this challenge. If you are now proficient in using storage volumes in containers, go for it.