Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

What is a container data volume

Docker if the data in the container, then we delete the container, the data will be lost! Requirements: Requirements can be persisted. Just like when we use MySQL, after the container is deleted, the data is lost! There can be a data sharing technology between data, data generated in Docker containers, synchronized to the local. This is volume technology, directory mount, where we mount the directory of our container’s contents onto Linux.

Summary: Container persistence and synchronization, and data sharing between containers

Using a Data Volume

Command mount (-v host directory: in-container directory)

# Start an Ubuntu image, /home/ceshi $docker run -it -v /home/ceshi:/home ubuntu/bin/bash # $docker inspect b4b # "Mounts" : [{" Type ":" bind ", "Source" : "/ home/ceshi", # within the host address "Destination" : "/ home", # container address "Mode" : ""," RW ": true, "Propagation": "rprivate" } ], #... $home/ceshi/test01 # $home/ceshi/test01 # B4b # server /home/ceshi create a file $touch /home/ceshi/test02 # Start container $docker $docker attach b4b $ls home/ test test01 test02Copy the code

The host and the container are bound to each other, and changes made by one side are also made by the other. Even when a container is stopped, it can be modified. We only need to modify locally in the future, the container will be automatically synchronized

Problem thinking?

How is the mount data stored? Does the host and container keep a separate copy, or how is it stored?

The installationMySql

Get the mirror, Have won't have to get $docker pull mysql: 5.7 # dockerhub official sample $docker run - name some - mysql - v/my/own/datadir: / var/lib/mysql - e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag # MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag # Installing the mysql user name password need background # # - d - # - v p port mapping data volume mount environment configuration # # - e - name name $docker run - d - 3310: p. 3306 - v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 Mysql: $ls /home/mysql/ conf data: $ls /home/mysql/ conf data: $ls /home/mysql/ conf data: $ls /home/mysql/ conf dataCopy the code

Delete container data is still there?

$docker rm -f mysql01 $ls /home/mysql.conf dataCopy the code

After the container is deleted, the data volume mounted to the local area is still not lost, and the persistence function of the container is realized

Named mount and anonymous mount

# start nginx01, $docker run -d -p --name nginx01 -v /etc/nginx nginx $docker volume ls DRIVER volume name local E10b975bc9dbf6cf76c351832330e39b79f73a758dbd64486d008f0beef9f72e # this is anonymous mount, when we were in - v Just write the path of the container, no mount path # start nginx02, $docker run -d -p --name nginx02 -v test-nginx:/etc/nginx nginx # The local e10b975bc9dbf6cf76c351832330e39b79f73a758dbd64486d008f0beef9f72e local test - # nginx passed - v volume name: # to check the volume location $container path docker volume inspect test-nginx [ { "CreatedAt": "2021-09-12T21:53:47+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/test-nginx/_data", "Name": "test-nginx", "Options": null, "Scope": "local" } ]Copy the code

All of thedockerThe volume inside the container is in the case that no directory is specified/var/lib/docker/volumesWithin the

We can easily find our volumes with named mounts, most of the time using named mounts

How do I determine whether to mount a named or anonymous mount or a specified path?

  • Anonymous mount:-v Indicates the path inside a container
  • Named mount:-v Volume name: Indicates the internal path of a container
  • Specify mount:-v Host path: specifies the path within a container, host machine path/start

expand

Sometimes we find the mirror mount command -v followed by or or rw what? This sets permissions on the container mount path. Once you set this permission, the container has restrictions on what can be mounted

ro(readonly) – read-only

This can only be done from the host, not from inside the container

rw(readwrite) — Readable and writable

The default is RW, which can be modified both on the host and in the container

Dockerfilemount

What is theDockerfile

Dockerfile is used to build a docker image build file, command script. From this script, you can generate mirrors, one layer at a time, one for each command.

Write aDockerfile

Create a dockerfile with a random name, Dockerfile $vim Dockerfile # Dockerfile $vim "Volume02 "] CMD echo "----end----" CMD /bin/bash # save exit # build an image $docker build -f Dockerfile -t ZBC /ubuntu: 1.0.step 1/4  : FROM ubuntu ---> 1318b700e415 Step 2/4 : VOLUME ["volume01", "volume02"] ---> Running in 309b8ddda1cc Removing intermediate container 309b8ddda1cc ---> 8e5e5a84be12 Step 3/4 : CMD echo "----end----" ---> Running in c6146e3c7e94 Removing intermediate container c6146e3c7e94 ---> 93f472392dc6 Step 4/4: CMD /bin/bash ---> Running in f475af85ea8b Removing intermediate container f475af85ea8b ---> 7a307a77e7bf Successfully Built seven a307a77e7bf Successfully tagged ZBC/ubuntu: 1.0 # check build image $docker images | grep ubuntu ZBC/ubuntu 1.0 7a307a77e7bf 50 seconds ago 72.8MB # Run $docker run it ZBC/Ubuntu :1.0 /bin/bash $ls bin dev home lib32 libx32 MNT proc run srv tmp var volume02 boot etc lib lib64 media opt root sbin sys usr volume01 # volume01,volume02 $docker inspect cd16d3407407 "Mounts": [{"Type": "volume", "Name": $docker inspect cd16d3407407 "Mounts": [{"Type": "volume", "Name": $docker inspect cd16d3407407 "Mounts": [{"Type": "volume", "Name": "3b802fad624b4987cbcb3e10e3823ce10c7fbda24aad2241dbff414cda75af90", "Source": "/var/lib/docker/volumes/3b802fad624b4987cbcb3e10e3823ce10c7fbda24aad2241dbff414cda75af90/_data", "Destination": "volume02", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "3c3c710a3e08d2885f2c47bade0d11d4b6142e728f929d0556e5a4d5d397ddfc", "Source": "/var/lib/docker/volumes/3c3c710a3e08d2885f2c47bade0d11d4b6142e728f929d0556e5a4d5d397ddfc/_data", "Destination": "volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ],Copy the code

The above method of mounting images is common because we usually build our own images. If the dog does not mount the volume when it takes the image, it automatically mounts the volume. -v Volume name: inside the container path

Data volume container

# Start three containers and use the Docker image we just created. Note that Ubuntu has no foreground application. $docker run it --name ubuntu01 ZBC/Ubuntu :1.0 /bin/bash Whether volume01 and volume02 have been mounted? $ls bin dev home lib32 libx32 MNT proc run SRV TMP var volume02 boot etc lib lib64 media opt Root sbin sys usr volume01 $docker run it --name ubuntu02 --volumes-from ubuntu01 ZBC/Ubuntu :1.0 /bin/bash Whether volume01 and volume02 have been mounted? $ls bin dev home lib32 libx32 MNT proc run SRV TMP var volume02 boot etc lib lib64 media opt Mysql > alter table ubuntu sbin sys usr volume01 $docker attach Ubuntu 01 $touch /volume01/ubuntu_01_test $docker attach Ubuntu 02 $ls /volume01 Just like a subclass inherits its parent class, a subclass has some of the same things as its parent class. Can ubuntu02 or Ubuntu01 data be shared? $docker run it --name ubuntu03 --volumes-from ubuntu01 ZBC/Ubuntu :1.0 /bin/bash $ls /volume01 ubuntu_01_test # How about stopping Ubuntu01? There is no effect on mount data after stopping. Will the data still exist? $docker rm -f ubuntu01 $docker attach ubuntu03 $ls /volume01 ubuntu_01_test $dockcer inspect ubunut02 "Mounts": [{"Type": "volume", "Name": "b6f19722307651401d2d79722a2b93d191beba23872370dc2a7ab46e048a3eea", "Source": "/var/lib/docker/volumes/b6f19722307651401d2d79722a2b93d191beba23872370dc2a7ab46e048a3eea/_data", "Destination": "volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "be5c33495e24b4351c77b4a42ab5bf5589d1bdf16228037c74b8ecbeb16d0cf2", "Source": "/var/lib/docker/volumes/be5c33495e24b4351c77b4a42ab5bf5589d1bdf16228037c74b8ecbeb16d0cf2/_data", "Destination": "volume02", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], $ dockcer inspect ubunut03 "Mounts": [ { "Type": "volume", "Name": "b6f19722307651401d2d79722a2b93d191beba23872370dc2a7ab46e048a3eea", "Source": "/var/lib/docker/volumes/b6f19722307651401d2d79722a2b93d191beba23872370dc2a7ab46e048a3eea/_data", "Destination": "volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "be5c33495e24b4351c77b4a42ab5bf5589d1bdf16228037c74b8ecbeb16d0cf2", "Source": "/var/lib/docker/volumes/be5c33495e24b4351c77b4a42ab5bf5589d1bdf16228037c74b8ecbeb16d0cf2/_data", "Destination": "volume02", "Driver": "local", "Mode": "", "RW": true, "Propagation": ""}]. $docker exec -it ubuntu02 /bin/bash $touch /ubuntu_02_test $docker exec -it Ubuntu02 /bin/bash $ls /volume02 ubuntu_02_testCopy the code

use

Data is shared between multiple containers, such as between multiple mysql or redis

How can multiple mysql data be shared?

# the parent container $docker run - d - p 3310:3306 - v/home/mysql/conf: / etc/mysql/conf., d - v/home/mysql/data: / var/lib/mysql - e MYSQL_ROOT_PASSWORD=123456 --name mysql02 mysql:5.7 $docker run -d -p 3311:3306 -e MYSQL_ROOT_PASSWORD=123456 --volumes from mysql02 --name mysql01 mysql:5.7Copy the code

conclusion

  • Configuration information can be passed between containers, and the life cycle of the data volume container continues until no container is used.
  • But once persisted locally, the local data is not deleted