After getting familiar with the installation and use of basic commands of Docker, we began to learn the advanced operations of Docker, including but not limited to creating Docker images, mounting data volumes, and visualization of Docker.

Docker submits the image

Boot image

Let’s start by installing tomcat

docker run -d -p 8080:8080 tomcat

Then visit: http://192.168.88.71:8080/

The container is started, but page 404 is actually not accessed by default

[root@hadoop101 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f7e60ca6bf5 tomcat "catalina.sh Run "6 seconds ago Up 4 seconds 0.0.0.0:8080->8080/ TCP, :::8080->8080/ TCP wonderful_satoshiCopy the code

Modify the container

In order to simplify the startup process, Tomcat has omitted the loading of the page, so if you visit 404, you can enter the container and modify the configuration

[root@hadoop101 ~]# docker exec -it 7f7e60ca6bf5 /bin/bash
root@7f7e60ca6bf5:/usr/local/tomcat# ls
BUILDING.txt     LICENSE  README.md      RUNNING.txt  conf  logs            temp     webapps.dist
CONTRIBUTING.md  NOTICE   RELEASE-NOTES  bin          lib   native-jni-lib  webapps  work
root@7f7e60ca6bf5:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@7f7e60ca6bf5:/usr/local/tomcat#
Copy the code

Tomcat static pages are backed up in the webapps.dist directory, so we can copy them to webapps.

Once again to visit: http://192.168.88.71:8080/, we are familiar with the page shows the normal.

Submit mirror

I then resubmit my modified container as a new image.

Docker commit -m "cp webapps files" -a "zhangbao" 7f7e60CA6bf5 tomcat001:1.0

[root@hadoop101 ~]# docker commit -m "cp webapps files" -a "zhangbao" 7f7e60ca6bf5  tomcat001:1.0
sha256:1bcaa367798ccbea30337645a4839db5878a40eaed92e5c5d66c3df13edae738
[root@hadoop101 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
tomcat001             1.0       1bcaa367798c   7 seconds ago   684MB
tomcat                latest    fb5657adc892   2 months ago    680MB
Copy the code

It’s a little bit bigger than before.

Start a new image

Let’s start the newly committed image

Docker run -d -p 8081:8080 Tomcat001:1.0

Visit http://192.168.88.71:8081/, according to normal page directly.

Container data volume

background

Start after a container, container configuration or data also in the container, when we delete the container, the container and internal configuration and the data will be lost, this is not normal, so the Docker provides the function of the data volume, namely the container configuration or data mapping to the host computer, the two-way binding, when we want to delete a container, The data can also be retained, and when we modify the configuration of the container, we do not need to go into the container, but can modify it directly in the mapping file.

Start a mirror and add a data volume

Docker run it -p 8080:8080 -v /opt/test:/opt tomcat001:1.0 /bin/bash

-v /opt/test:/opt indicates that the /opt/test directory in the container is mounted to the /opt/test directory on the host. In this way, bidirectional binding is implemented.

validation

Note: The previous command can not be CTRL + C or exit, otherwise the container will exit directly, use CTRL + P + Q background start

TXT file in /opt/test, and then check the changes in /opt

#The host machine
[root@hadoop101 opt]# cd /opt/test
[root@hadoop101 test]# touch test.txt
[root@hadoop101 test]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 19 22:33 test.txt

#container[root@hadoop101 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 04331e8EF590 Tomcat001:1.0 "/bin/bash" 7 minutes ago Up 7 minutes 0.0.0.0:8080->8080/ TCP, :::8080->8080/tcp crazy_mclaren [root@hadoop101 ~]# docker attach 04331e8ef590 root@04331e8ef590:/usr/local/tomcat# cd /opt/ root@04331e8ef590:/opt# ls test.txtCopy the code

Similarly, we modify the contents of the file in the container, the contents of the file can be viewed in real time on the host, and after stopping the container, we modify the file on the host, and then start the container, the files in the container are also modified.

Install the msyql

docker run -d -p 3301:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD = 123456 - name mysql001 mysql: 5.7

-p Host port: indicates the container port

-v Host directory: indicates the container directory. The value can be multiple v

-e Configures the variable

–name Container name

Here we mount the configuration file and data directory of the mysql container to the /home/mysql directory of the host machine

[root@hadoop101 ~]# cd /home/mysql/ [root@hadoop101 mysql]# ll total 0 drwxr-xr-x 2 root root 6 Mar 19 23:13 conf drwxr-xr-x 5 polkitd root 328 Mar 19 23:13 data [root@hadoop101 mysql]# cd data [root@hadoop101 data]# ls auto.cnf client-cert.pem ibdata1 ibtmp1 private_key.pem server-key.pem ca-key.pem client-key.pem ib_logfile0 mysql public_key.pem  sys ca.pem ib_buffer_pool ib_logfile1 performance_schema server-cert.pem [root@hadoop101 data]#Copy the code

Then we connect through Navicat and create a new database, test, to look at the data directory again

[root@hadoop101 data]# ls auto.cnf client-cert.pem ibdata1 ibtmp1 private_key.pem server-key.pem ca-key.pem client-key.pem ib_logfile0 mysql public_key.pem sys ca.pem ib_buffer_pool ib_logfile1 performance_schema server-cert.pem  test [root@hadoop101 data]#Copy the code

Data volumes are mounted

After getting familiar with the function of the container data volume, let’s look at the mounting of the data volume. There are many ways to mount the data volume. One of the ways we use above is to specify a path to mount the data volume. In fact, there are other mounting methods. Different mounting methods can be used in different scenarios as required.

Anonymous mount

-v is directly followed by the path inside the container

docker run -d -v /etc/nginx --name nginx001 nginx

[root@hadoop101 ~]# docker inspect nginx001
#Run the following command to mount /etc/nginx to /var/lib/docker/volumes
"Mounts": [
            {
                "Type": "volume",
                "Name": "cc9ff269b7ad67fab8866e7a446009207d2a3af24395003a8cb75f3981022eec",
                "Source": "/var/lib/docker/volumes/cc9ff269b7ad67fab8866e7a446009207d2a3af24395003a8cb75f3981022eec/_data",
                "Destination": "/etc/nginx",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
Copy the code

A named mount

-v Data volume name: Indicates the intra-container path

docker run -d -v zhangbao_nginx:/etc/nginx --name nginx002 nginx

[root@hadoop101 ~]# docker volume ls
DRIVER    VOLUME NAME
local     511511317ef0256d2d1570104e11a6ce897ba387267fbeec0192326dd6156067
local     cc9ff269b7ad67fab8866e7a446009207d2a3af24395003a8cb75f3981022eec
local     e5303012839aa87faef7719a0407f690a9da8af1fcf6dfc2ef51cf20a5aa441f
local     zhangbao_nginx
[root@hadoop101 ~]# docker volume inspect zhangbao_nginx
[
    {
        "CreatedAt": "2022-03-21T23:09:43+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/zhangbao_nginx/_data",
        "Name": "zhangbao_nginx",
        "Options": null,
        "Scope": "local"
    }
]

Copy the code

Anonymous mount and named mount, and the one we often use is to specify a path

Specify a path to mount

-v Host path: indicates the container path

docker run -d -v /etc/nginx:/etc/nginx --name nginx003 nginx

[root@hadoop101 ~]# docker inspect nginx003
"Mounts": [
            {
                "Type": "bind",
                "Source": "/etc/nginx",
                "Destination": "/etc/nginx",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
Copy the code

Docker visualization

portainer

A Docker management tool to provide web pages, the command is as follows:

docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

Visit: http://192.168.88.71:8088

Example Add the super management account admin / 88888888

Then select the local Docker environment

Once connected, we can see that there are seven images and click in

You can see that the local environment has 7 mirrors, 13 containers, and 10 data volumes.

If you look at the 7 images, you can see the images we used in the previous test, including a new modified image tomcat001:1.0 that we submitted ourselves.

A list of data volumes, including the anonymous data volumes and named data volumes that we created.

lazydocker

A terminal based Docker visual query tool.

Github:github.com/jesseduffie…

docker run --rm -it -v \
/var/run/docker.sock:/var/run/docker.sock \
-v /.config/lazydocker:/.config/jesseduffield/lazydocker \
lazyteam/lazydocker
Copy the code

The mouse and arrow keys control the view of the information box, and there are reminders throughout the bottom of the screen

Mouse and d-pad demo

Each view requires a very long command, so we can simplify it

echo "alias lzd='docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock -v /.config/lazydocker:/.config/jesseduffield/lazydocker lazyteam/lazydocker'" >> ~/.zshrc

Copy the code

For more information, please search on a public platform: player 1, this article number: 2002, and reply to obtain.