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.
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:
- Create a data volume named
my-vol.
labex:~/ $ docker volume ls
DRIVER VOLUME NAME
local jenkins-data
local minikube
local my-vol
- Get
Mountpointby 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"
}
]
- Go to
Mountpointand create thetest.txtfile.
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
dockercommand. - Create a file using the
rootuser. - Swithch to the
rootuser usingsudo su, Set the userrootpassword usingsudo passwd root.
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:
- 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
- Create a container named
nginxbased in nginx image that will mount its/usr/share/nginx/htmldirectory to thenginx-voldata volume.
labex:~/ $ docker ps | grep nginx
c39296aaf39e nginx "/docker-entrypoint.…" 35 seconds ago Up 31 seconds 80/tcp nginx
- Copy the
/home/labex/project/index.htmlfile to thenginxcontainer.
labex:/tmp/ $ pwd
/tmp
labex:/tmp/ $ cat index.html
hello world
- Copy the
/home/labex/project/index.htmlfile to thenginxcontainer.
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
dockercommand. - Know how to start a container.
- Know how to copy local files to containers.
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:
- Create an
nginxdirectory in the/tmpdirectory.
labex:/tmp/ $ ls | grep nginx
nginx
- Start the
nginx-hostcontainer based on thenginximage that mounts the/tmp/nginxdirectory to the/usr/share/nginx/htmldirectory in the container.
labex:/tmp/ $ docker ps | grep nginx-host
6fc2d40e12df nginx "/docker-entrypoint.…" 44 seconds ago Up 43 seconds 80/tcp nginx-host
- Check the specific mount details with the
docker inspectcommand.
"Mounts": [
{
"Type": "bind",
"Source": "/tmp/nginx",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
- Copy the
/home/labex/project/nginx.txtfile to the/usr/share/nginx/htmldirectory of thenginx-hostcontainer.
labex:/tmp/ $ pwd
/tmp
labex:/tmp/ $ cat nginx.txt
hello nginx
- Copy the
/home/labex/project/nginx.txtfile to the/usr/share/nginx/htmldirectory of thenginx-hostcontainer.
labex:nginx/ $ docker exec nginx-host cat /usr/share/nginx/html/nginx.txt
hello nginx
- Check the
/tmp/nginxdirectory on the local machine to see if there is anginx.txtfile.
labex:nginx/ $ pwd
/tmp/nginx
labex:nginx/ $ cat nginx.txt
hello nginx
Requirements
To complete this challenge, you will need:
- Know the
Host Pathpath 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.
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:
- Create a
/tmp/sharedirectory on the local host.
labex:/tmp/ $ ls -ld /tmp/share
drwxr-xr-x 2 labex labex 4096 Jan 20 18:20 /tmp/share
- Create a container named
nginx-sharebased on thenginximage and mount the local/tmp/sharedirectory to the/usr/share/nginx/htmldirectory in the container.
labex:/tmp/ $ docker ps | grep nginx-share
216d02d94f39 nginx "/docker-entrypoint.…" 55 seconds ago Up 55 seconds 80/tcp nginx-share
- Create a container named
busybox-sharebased on thebusyboximage and mount it fromnginx-sharecontainer.
labex:/tmp/ $ docker ps | grep busy
30bf04aee681 busybox "sh" 43 seconds ago Up 42 seconds busybox-share
- Copy the
/home/labex/project/share.txtfile to thebusybox-sharecontainer.
labex:/tmp/ $ pwd
/tmp
labex:/tmp/ $ cat share.txt
hello share
- Copy the
/home/labex/project/share.txtfile to thenginx-sharecontainer.
labex:/tmp/ $ docker exec nginx-share cat /usr/share/nginx/html/share.txt
hello share
- Go to the
busybox-sharecontainer 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.
Summary
Congratulations on completing this challenge. If you are now proficient in using storage volumes in containers, go for it.



