Docker basic knowledge notes

1, Cause

Data persistence problem

Docker packages the application with the running environment to form a container. If the data generated in the container is not generated through docker commit to save the data as part of the image, the data will naturally disappear after the container is deleted. In order to save data, we use data volume in docker.

The volume is designed for data persistence, which is completely independent of the life cycle of the container. Therefore, docker will not delete the mounted data volume when the container is deleted.

Data volume characteristics

  1. Data volumes can share or reuse data between containers
  2. Changes in the volume can take effect directly
  3. Changes in the data volume are not included in the update of the mirror
  4. The life cycle of a data volume continues until no container uses it

2, Related commands

1. Simple example

docker run -it -v Host Directory:Container directory

docker run -it -v /home/zhunian/testVolume:/home centos /bin/bash

Create test. In the container directory Java file, and then go to the host directory to check whether there is this file.

Check the inside of the container

docker inspect container id

2. Common commands

#Create data volume
➜  ~ docker volume create my-vol
my-vol

#View all data volumes
➜  ~ docker volume ls
DRIVER              VOLUME NAME
local               my-vol

#View the internal information of the specified data volume
➜  ~ docker volume inspect my-vol
[
    {
        "CreatedAt": "2022-04-13T18:47:34+08:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
        "Name": "my-vol",
        "Options": {},
        "Scope": "local"
    }
]

#Delete data volume
➜  ~ docker volume rm my-vol
my-vol

#Delete the data volume when deleting the container
docker rm -v container id



➜  ~ docker volume rm my-vol
my-vol
➜  ~ docker volume create my-vol
my-vol

#Delete unused data volumes (no containers used)
➜  ~ docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
my-vol

Total reclaimed space: 0B

The data volume is designed to persist data. Its life cycle is independent of the container. Docker will not automatically delete the data volume after the container is deleted, and there is no garbage collection mechanism to deal with the data volume without any container reference

mount command

$ docker run -d -P \
--name web \
# -v /src/webapp:/opt/webapp:ro \
--mount type=bind,source=/src/webapp,target=/opt/webapp,readonly \
training/webapp \
python app.py
 Add readonly After that, it is mounted as read-only. If you're in a container/src/webapp If you create a new file in the directory, the following error will be displayed
/src/webapp # touch new.txt
touch: new.txt: Read-only file system

 3. Named mount and anonymous mount

Anonymous mount: when specifying the data volume, the host path corresponding to the container path is not specified. The mapped host path is the default path. A randomly named folder is automatically generated in / var/lib/docker/volumes /.

# anonymous
➜  ~ docker run -d -P --name nginx01 -v /etc/nginx nginx
6bafee66436f3a45712916807890eb816bcb7c66ef0ad3d5d617c5af9992ebed

➜  ~ docker volume ls
DRIVER              VOLUME NAME
local               0c7c80ceaa1e21318533b4e9d29de7a2b8a0264b70e0a0b1558dfa0a73aa76bd

#Named
➜  ~ docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
df39c371c583c9b6edcf447d93e86064a75f5ff4b8a0e1b542651f6a6b220a7d
➜  ~ docker volume ls
DRIVER              VOLUME NAME
local               0c7c80ceaa1e21318533b4e9d29de7a2b8a0264b70e0a0b1558dfa0a73aa76bd
local               juming-nginx

#View dataset internal information
➜  ~ docker volume inspect juming-nginx
[
    {
        "CreatedAt": "2022-04-13T19:01:33+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
        "Name": "juming-nginx",
        "Options": null,
        "Scope": "local"
    }
]



Anonymous mount, named mount and specified path mount commands are different


-v path # anonymous mounting in container

-v volume name: path \

-v / host path: the path # specified in the container is mounted

Specify parameters related to data volume mapping:

ro -- readonly. If read-only is set, only the path of the host can be operated, and the corresponding path in the container cannot be operated.

rw -- readwrite
 

docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx

4.dockfile file setting data volume

# Build image from dockerfile file

# dockerfile file
FROM centos

VOLUME ["volume01","volume02"]

CMD echo "----end----"
CMD /bin/bash

#Build image
➜  dockfileTest docker build -t nginx:v2 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
 ---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"]
 ---> Running in cfa7f559e03d
Removing intermediate container cfa7f559e03d
 ---> 8d4db309bbe7
Step 3/4 : CMD echo "----end----"
 ---> Running in d545cf1da7ce
Removing intermediate container d545cf1da7ce
 ---> 7e59b4e08c9f
Step 4/4 : CMD /bin/bash
 ---> Running in d92788e5339a
Removing intermediate container d92788e5339a
 ---> 5a30ed51cbdd
Successfully built 5a30ed51cbdd
Successfully tagged nginx:v2

➜  dockfileTest docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
nginx               v2                  5a30ed51cbdd        About a minute ago   231MB

# Check the information Volumes in the image
➜  dockfileTest docker inspect 5a30ed51cbdd
[
    {
         ......
        "ContainerConfig": {
            "Hostname": "d92788e5339a",
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/sh\" \"-c\" \"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:7e59b4e08c9f7507a86baebbb6cb4a5bbf7b94ce2c6eb7f2aacf2f0477af0a7c",
            "Volumes": {
                "volume01": {},
                "volume02": {}
            },
      
        },


# Run mirror
➜  dockfileTest docker run -it nginx:v2 /bin/bash
[root@0ad3587947d9 /]# pwd
/
# There are volume01 and volume02 folders in the image
[root@0ad3587947d9 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume01	volume02

#View container ID
➜  dockfileTest docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                   NAMES
0ad3587947d9        nginx:v2            "/bin/bash"              About a minute ago   Up About a minute                           flamboyant_mayer

#View container information Mounts 

➜  dockfileTest docker inspect 0ad3587947d9
[
    {
        ......
        "Mounts": [
            {
                "Type": "volume",
                "Name": "17efdc1bdfe52fd8029c8314985b6466349f95ee9c275099a5360ffeb1cc9b1b",
                "Source": "/var/lib/docker/volumes/17efdc1bdfe52fd8029c8314985b6466349f95ee9c275099a5360ffeb1cc9b1b/_data",
                "Destination": "volume01",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "f47318e80edc6b7ccc3b697debaa1cd54c2bae9105f810047fa4238b16f6150d",
                "Source": "/var/lib/docker/volumes/f47318e80edc6b7ccc3b697debaa1cd54c2bae9105f810047fa4238b16f6150d/_data",
                "Destination": "volume02",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],


Because the host directory is not specified in the dockerfile, it is an anonymous mount. A randomly named path is generated in the / var/lib/docker/volumes / directory.

5. Container data volume

The main parameter is -- volumes from

#Start container 1
➜  ~ docker run -it --name nginx1 nginx:v2

# Start container 2
➜  ~ docker run -it --name nginx2 --volumes-from nginx1 nginx:v2
[root@daa21ba081ac /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume01	volume02
[root@daa21ba081ac /]# cd volume01 
[root@daa21ba081ac volume01]# ls
[root@daa21ba081ac volume01]# touch test.java
[root@daa21ba081ac volume01]# echo 'hello word' > test.java

#Enter container 1 to check whether there is content in the Volume01 directory
➜  ~ docker ps                   
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
daa21ba081ac        nginx:v2            "/bin/sh -c /bin/bash"   3 minutes ago       Up 3 minutes                            nginx2
18ae65037620        nginx:v2            "/bin/sh -c /bin/bash"   5 minutes ago       Up 4 minutes                            nginx1
➜  ~ docker exec -it 18ae65037620 /bin/bash
[root@18ae65037620 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume01	volume02
[root@18ae65037620 /]# cd volume01
[root@18ae65037620 volume01]# ls
test.java
[root@18ae65037620 volume01]# cat test.java 
hello word
[root@18ae65037620 volume01]# 



Reference: Detailed explanation of docker container data volume_ Code0cean blog - CSDN blog_ Docker's data volume

Tags: Back-end

Posted by Stingus on Wed, 13 Apr 2022 20:24:29 +0930