Analysis of the Principle and Usage of Docker Container Data Volume
- 2021-08-31 09:44:52
- OfStack
What is a container data volume
If all the data is in the container, the data will be lost once the container is deleted!
eg: The mysql container is deleted, which is what we often call deleting libraries and running away. Requirements: The data can be persisted, and the container can be deleted immediately. Our data is still there
Container can have a data sharing technology directly! Docker container generated data, synchronized to the local!
This is roll technology! Mount the directory, mount the directory of our container to linux!
Summary: Volume technology is to achieve data persistence and synchronous operation, and data can be shared among containers
Working with data volumes
Mode 1: mount-v directly using commands
# Command
docker run -it -v Host directory: in-container directory -p Host port: Container port
# Test
# Host home The directory is empty
[root@bogon home]# ls
# Start contes Mirror image Set the host's home Container's home Bind
[root@bogon home]# docker run -v /home:/home/ -it centos
[root@8dc073caf39c /]# cd home/
# Container home The directory is empty
[root@8dc073caf39c home]# ls
# Create in the container directory test.java Documents
[root@8dc073caf39c home]# touch test.java
[root@8dc073caf39c home]# ls
test.java
# Switch to host home Discover after directory Appears test.java Documents
[root@8dc073caf39c home]# [root@bogon home]# ls
test.java
# On the mainframe home Directory test2.java Documents
[root@bogon home]# touch test2.java
[root@bogon home]# ls
test2.java test.java
[root@bogon home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8dc073caf39c centos "/bin/bash" 3 minutes ago Up 3 minutes focused_nobel
# Enter the running container
[root@bogon home]# docker exec -it 8dc073caf39c /bin/bash
# Enter the container home Directory
[root@8dc073caf39c /]# cd home/
# Discover existence test2.java Documents
[root@8dc073caf39c home]# ls
test.java test2.java
# Pass inspect Command to view container information
[root@bogon home]# docker inspect 8dc073caf39c
"Mounts": [
{
"Type": "bind",
"Source": "/home", # Host directory
"Destination": "/home", # Container directory
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
## Conclusion: If we use -v Data binding is done
# 1 The container stops and the host makes changes to the data After the container is started, The data will also be synchronized
# 2 Delete the container, and the data in this directory still exists in the host
## Benefit: After using the data volume, when we modify the configuration file in the future, we only need to modify it locally, and it will be synchronized automatically in the container
Installing mysql
Data Persistence in mysql
# -e MYSQL_ROOT_PASSWORD=my-secret-pw Set the initial password to my-secret-pw
# Official order: docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
# Test
# Interpretation
# -d Running in the background
# -p 3306:3306 Binding port
# -v /home/mysql/conf:/etc/mysql/conf.d Data volume mount technology binding mysql Configuration file
# -v /home/mysql/data:/var/lib/mysql Data volume mount technology binding mysql Data
# -e MYSQL_ROOT_PASSWORD=123456 Environment configuration --- "Settings mysql The initial password is 123456
# --name mysql0 Name the container mysql01
[root@bogon home]# docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql
6d75b6312f725de2c71709116af5755604ea60cd073f1daf3755c578c1e64f57
Named and anonymous mount
# Anonymous mount
-v Path inside the container!
docker run -d -P --name nginx01 -v /etc/nginx nginx
# Named mount
-v Volume name : Path in container
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
# Test anonymous mount
[root@localhost test]# docker run -d -P --name nginx01 -v /etc/nginx nginx
214dab398d9997a730b970b6e3bb08fa7e39bbb0ca91ad59f6b3f235d8f1b9bc
# View all volume The situation of
[root@localhost test]# docker volume ls
DRIVER VOLUME NAME
local 2c22e1c50ff7330b815b692f8f71a1fca878209223846c95626f7efd9dc2a83b # Anonymous mount
# Test named mount
# Pass -v Volume name : Path in container
[root@localhost test]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
a678d79886565601bf466ff41734cb5334bdaf9e881b9cbf11edb84e9d790251
# View all volume The situation of
[root@localhost test]# docker volume ls
DRIVER VOLUME NAME
local 2c22e1c50ff7330b815b692f8f71a1fca878209223846c95626f7efd9dc2a83b # Anonymous mount
local juming-nginx # Named mount
# View information about a data volume
# Command
docker volume inspect Volume name
# All docker Volume in container If no directory is specified, it is in the /var/lib/docker/volumes/XXX/_data
[root@localhost test]# docker volume inspect juming-nginx
[
{
"CreatedAt": "2020-08-13T09:18:34+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "juming-nginx",
"Options": null,
"Scope": "local"
}
]
# We can easily find our 1 Volumes, most of which are used ---- Named mount
# How do I determine whether to mount with name or anonymity Or the specified path mount!
# -v Path in container # Anonymous mount
# -v Volume name : Path in container # Named mount
# -v Host path : Path in container # Mount with specified path
Expand
# Pass -v Volume name : Path in container :ro rw Change read and write permissions
# ro--->read only Read-only
# rw--->read write Read and write
docker run -d -P --name nginx01 -v juming-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx01 -v juming-nginx:/etc/nginx:ro nginx
Get to know DockerFile for the first time
DockerFile is the build file used to build the docker image! Command script! Experience it first!
[root@localhost docker-test-volume]# cat dockerfile
FORM centos
VOLUME ["volume01", "volume02"]
CMD echo "-----end-----"
CMD /bin/bash
# Build
# Command docker build -f shell Script file -t Mirror name : Version number
[root@localhost docker-test-volume]# docker build -f /home/docker-test-volume/dockerfile1 -t centos:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 0d120b6ccaa8
Step 2/4 : VOLUME ["volume01", "volume02"]
---> Running in 4e6de7bc2f15
Removing intermediate container 4e6de7bc2f15
---> f9e48207902b
Step 3/4 : CMD echo "-----end-----"
---> Running in b22adea363e5
Removing intermediate container b22adea363e5
---> a7518e2e1c72
Step 4/4 : CMD /bin/bash
---> Running in ae1b746bef6b
Removing intermediate container ae1b746bef6b
---> d840628c30a9
Successfully built d840628c30a9
Successfully tagged centos:1.0
# View Mirror
[root@localhost overlay2]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 1.0 d840628c30a9 12 minutes ago 215MB # Our own generated mirror image
centos latest 0d120b6ccaa8 2 days ago 215MB
# Start the image we generated
[root@1af673cf9c88 /]# docker run -it d840628c30a9 /bin/bash
[root@1af673cf9c88 /]# ls -l
total 0
lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x. 5 root root 360 Aug 13 02:18 dev
drwxr-xr-x. 1 root root 66 Aug 13 02:18 etc
drwxr-xr-x. 2 root root 6 May 11 2019 home
lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64
drwx------. 2 root root 6 Aug 9 21:40 lost+found
drwxr-xr-x. 2 root root 6 May 11 2019 media
drwxr-xr-x. 2 root root 6 May 11 2019 mnt
drwxr-xr-x. 2 root root 6 May 11 2019 opt
dr-xr-xr-x. 117 root root 0 Aug 13 02:18 proc
dr-xr-x---. 2 root root 162 Aug 9 21:40 root
drwxr-xr-x. 11 root root 163 Aug 9 21:40 run
lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 May 11 2019 srv
dr-xr-xr-x. 13 root root 0 Aug 11 09:58 sys
drwxrwxrwt. 7 root root 145 Aug 9 21:40 tmp
drwxr-xr-x. 12 root root 144 Aug 9 21:40 usr
drwxr-xr-x. 20 root root 262 Aug 9 21:40 var
drwxr-xr-x. 2 root root 6 Aug 13 02:18 volume01 # This is the data volume directory that is automatically mounted when we generate the image
drwxr-xr-x. 2 root root 6 Aug 13 02:18 volume02
# This volume and the external 1 There must be 1 Synchronized directories! We're looking for 1 Under
# 1 , According to the container id Query the information of the container -- "Data volume information
# docker inspect 1af673cf9c88
# 2 , Find from the data volume information volume01 Corresponding data volume name
# docker volume inspect 8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3
# 3 , According to the data volume name Query the information of the data volume -- "Find linux Corresponding directory in
# docker volume inspect 8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3
# Create inside the container 1 Files In the data volume volume01 Created in the test.java Documents
[root@1af673cf9c88 volume01]# touch test.java
[root@1af673cf9c88 volume01]# ls
test.java
# Exit container
[root@1af673cf9c88 volume01]# exit
exit
# View information for this container
[root@localhost overlay2]# docker inspect 1af673cf9c88
# Mount Volume Found volume01 The corresponding name, Namely: 8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3
"Mounts": [
{
"Type": "volume",
"Name": "8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3",
"Source": "/var/lib/docker/volumes/8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "046d0baa3cc0bc3540c5e7248808358371641bfba4e0bbd139c99fe851751da2",
"Source": "/var/lib/docker/volumes/046d0baa3cc0bc3540c5e7248808358371641bfba4e0bbd139c99fe851751da2/_data",
"Destination": "volume02",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
# According to the data volume name 8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3 Find the volume in the linux Location
[root@localhost overlay2]# docker volume inspect 8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3
[
{
"CreatedAt": "2020-08-13T10:27:12+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3/_data",
"Name": "8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3",
"Options": null,
"Scope": "local"
}
]
# In /var/lib/docker/volumes/8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3/_data/ Found our test.java Documents
[root@localhost volumes]# cd /var/lib/docker/volumes/8c3486526093c755785725111b4063cd93a5ba88f9c2ac09f45741a0f1d08fd3/_data/
[root@localhost _data]# ls
test.java
Data volume container
Multiple mysql data synchronization!
# Pass --volumes-from Container name Realize data sharing among containers
# Start 1 Mirror image The name is docker01
[root@localhost _data]# docker run -it --name docker01 centos:1.0 /bin/bash
[root@a85fbed0ebc9 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
# Start with 1 Mirror image The name is docker02 Association docker01 At this point, docker01 Known as a data volume container
[root@localhost _data]# docker run -it --name docker02 --volumes-from docker01 centos:1.0
[root@a89fb82eeeb5 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
# In the container docker02 Adj. volume01 Directory test.txt
[root@a89fb82eeeb5 /]# cd volume01/
[root@a89fb82eeeb5 volume01]# ls
[root@a89fb82eeeb5 volume01]# touch test.txt
[root@a89fb82eeeb5 volume01]# ls
test.txt
# View container information
[root@a89fb82eeeb5 volume01]# [root@localhost _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a89fb82eeeb5 centos:1.0 "/bin/sh -c /bin/bash" About a minute ago Up About a minute docker02
a85fbed0ebc9 centos:1.0 "/bin/bash" 4 minutes ago Up 4 minutes docker01
# Enter the container named docker01 Container of
[root@localhost _data]# docker exec -it a85fbed0ebc9 /bin/bash
[root@a85fbed0ebc9 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
# Object that enters the container volume01 Directory
[root@a85fbed0ebc9 /]# cd volume01/
# Discover test.txt Documents
[root@a85fbed0ebc9 volume01]# ls
test.txt
# Create test01.txt
[root@a85fbed0ebc9 volume01]# touch test01.txt
[root@a85fbed0ebc9 volume01]# ls
test.txt test01.txt
# Enter the container named docker02 Container of
[root@localhost _data]# docker exec -it a89fb82eeeb5 /bin/bash
[root@a89fb82eeeb5 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
[root@a89fb82eeeb5 /]# cd volume01/
# Discover test01.txt Documents
[root@a89fb82eeeb5 volume01]# ls
test.txt test01.txt
# Start with 1 Mirror image The name is docker03 Association docker01
[root@localhost _data]# docker run -it --name docker03 --volumes-from docker01 centos:1.0
# Enter volume01 Directory Discover test.txt test01.txt Documents
[root@11d93f9bcd89 /]# cd volume01/
[root@11d93f9bcd89 volume01]# ls
test.txt test01.txt
# Test process: 1 , run centos:1.0 Mirror image The container name is docker01
# 2 , run centos:1.0 Mirror image The container name is docker02 Pass --volumes-from docker01 Share data with it
# 3 , run centos:1.0 Mirror image The container name is docker03 Pass --volumes-from docker01 Share data with it
# 4 , run centos:1.0 Mirror image The container name is docker04 Pass --volumes-from docker03 Share data with it
# 5 , run centos Mirror image The container name is docker05 Pass --volumes-from docker03 Share data with it
# After testing Discover:
# 1 , in any 1 Of a container volume01 Directory to add files, other 4 The added file will appear in the directory in the container for data sharing
# 2 , stop and delete The container name is docker01 Container of, other 4 In 1 container volume01 The files in the directory still exist
# 3 , stop and delete The container name is docker01 Container, to other 4 Any container 1 Of a container volume01 Directory to add files, the rest 3 Containers also do data sharing
# 4 The data volume names in each container are different, but correspond to the same 1 A linux Data directory in the system; That is, the data volume directories in each container point to linux The same in the system 1 Data directory
[root@localhost _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78cd51a35c41 centos "/bin/bash" 17 minutes ago Up 17 minutes trusting_tharp
e6e0988c50cd centos "/bin/bash" 17 minutes ago Up 17 minutes docker05
c5ebc03e6819 centos:1.0 "/bin/sh -c /bin/bash" 19 minutes ago Up 19 minutes docker04
11d93f9bcd89 centos:1.0 "/bin/sh -c /bin/bash" 22 minutes ago Up 22 minutes docker03
a89fb82eeeb5 centos:1.0 "/bin/sh -c /bin/bash" 31 minutes ago Up 31 minutes docker02
[root@localhost _data]# docker inspect e6e0988c50cd
"Mounts": [
{
"Type": "volume",
"Name": "fc54c991eea888057575be45a03fe22a32303a6b1239a0a4099dd201b0b41a62",
"Source": "/var/lib/docker/volumes/fc54c991eea888057575be45a03fe22a32303a6b1239a0a4099dd201b0b41a62/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
]
[root@localhost _data]# docker inspect c5ebc03e6819
"Mounts": [
{
"Type": "volume",
"Name": "fc54c991eea888057575be45a03fe22a32303a6b1239a0a4099dd201b0b41a62",
"Source": "/var/lib/docker/volumes/fc54c991eea888057575be45a03fe22a32303a6b1239a0a4099dd201b0b41a62/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
]
[root@localhost _data]# docker inspect 11d93f9bcd89
"Mounts": [
{
"Type": "volume",
"Name": "fc54c991eea888057575be45a03fe22a32303a6b1239a0a4099dd201b0b41a62",
"Source": "/var/lib/docker/volumes/fc54c991eea888057575be45a03fe22a32303a6b1239a0a4099dd201b0b41a62/_data",
"Destination": "volume01",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
]
Multiple mysql realize data sharing
docker run -d -p 3306:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01 mysql
Conclusions:
Configuration information is passed between containers, and the life cycle 1 of the data volume container lasts until there is no container in use.
However, if 1 is persisted locally, the local data will not be deleted at this time.