From entry to mastery (I), we have learned the basic common commands of Docker and how to use docker. In the next part, we will focus on how to use advanced and advanced use.

Docker visualization tool

Portainer is a visual container image graphical management tool, using Portainer can easily build, manage and maintain the Docker environment. And completely free, based on the container installation, convenient and efficient deployment.

# search docker search portainer | head - n3# pull mirror docker pull docker. IO # / portainer portainer perform image of container docker run - d - p8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/Portainer # Access registrationhttp://localhost:9000/
Copy the code

Containers and data volumes

If the entire container is deleted, all the data will be destroyed. To solve this problem, you can use the data volume to hang the container data on the host disk.

Commit the mirror

Bottom line: Docker images are read-only, and when the container starts, a new writable layer is added to the top of the image. This layer is often referred to as the container layer, and below the container are the mirror layers.

In addition to learning, docker commit has some special applications, such as saving the scene after being hacked.

The docker commit command can save the storage layer of the container as an image. In other words, on the basis of the original mirror image, the storage layer of the container is superimposed, and a new mirror image is formed. When we run this new image later, we will have the last file changes from the original container.

Docker commit -m="Submitter Information" -a="The author"[root@zhaosi ~]# docker commit -m= docker commit -m="cp weblist dir" -a="zhangsan" c4b104964c2b zs_tomcat:1.0
sha256:d81257471ca4836acba4ca2a5c160e92c75f99c12aa8494e69b397a45711ed0a [root@zhaosi ~]# docker images REPOSITORY TAG IMAGE ID  CREATED SIZE zs_tomcat1.0       d81257471ca4   10 seconds ago   672MB
yachtomcat            1.0       3ca80e740f23   2 weeks ago      672MB
nginx                 latest    4cdc5dd7eaad   2 weeks ago      133MB
tomcat                latest    36ef696ea43d   3 weeks ago      667MB
mysql                 latest    5c62e459e087   4 weeks ago      556MB
portainer/portainer   latest    580c0e4e98b0   4 months ago     79.1MB
centos                latest    300e315adb2f   7 months ago     209MB
Copy the code

[Warning] Docker commit means that all operations on an image are black box operations, and the resulting image is also called a black box image. In other words, no one knows what commands were executed and how the image was generated, except the person who made the image.

dockfile

From the docker Commit study, we can see that the customization of the image is actually the customization of each layer of added configuration, files. If we could write a script for each layer of modify, install, build, and operate, and use this script to build and customize the image, the problems of unrepeatability, transparency, and volume mentioned above would be solved. This script is called Dockerfile.

A Dockerfile is a text file containing a series of instructions, each of which builds a layer, so the contents of each Instruction describe how the layer should be built.

As mentioned earlier, each directive in a Dockerfile creates a layer, and RUN is no exception. The behavior of each RUN is the same as the process of creating a mirror manually: create a new layer, execute the commands on it, and commit the changes in the layer to form the new mirror.

Case 1: Simply build an image using Nginx

Touch mynginx && CD mynginx && touch Dockerfile # vi'

Hello, Docker!

'
> /usr/Share/nginx/HTML/index. The HTML # in the current directory, execute a build # can also specify a path, # docker build -f /path/to/a/Dockerfile docker build -t nginx:v3 ID CREATED SIZE nginx v4 1b03f171e7d47 minutes ago 133MB zs_tomcat 1.0 d81257471ca4 4 hours ago 672MB yachtomcat 1.0 3ca80e740f23 2 weeks ago 672MB nginx latest 4cdc5dd7eaad 2 weeks ago 133MB tomcat latest 36ef696ea43d 3 weeks ago 667MB mysql latest 5c62e459e087 4 weeks ago 556MB portainer/portainer latest 580c0e4e98b0 4 months ago 79.1MB centos latest 300e315adb2f 7 months ago 209MB Copy the code

Example 2: Build centos to upload docker-pub

Realize two functions: you can use vim and ifconfig two functions, complete implementation of dockerfile and publish to docker-pub.

# 1Mkdir -p mycentos && CD mycentos && touch mycentosDockfile-centos #2Edit vi mycentosDockfile-centos FROM centos MAINTAINER zhangsan<[email protected]> ENV MYPATH /usr/local WORKDIR $MYPATH RUN yum -y install vim RUN yum -y install net-tools EXPOSE8080

CMD echo $MYPATH
CMD echo "================end================="
CMD /bin/bash

# 3/ myCentosdockfile-centos-t zhangsan/dircentos:1.0#.4If:3If you forget to add the current account as spacename, you need to add a separate tag docker tag dircentos:1.0  zhangsan/dircentos:1.0

# 5Docker login -u zhangsan #6: push
docker push zhangsan/dircentos:1.0
Copy the code

General DockFile contains basic image information, maintainer information, image operation instructions and container startup execution instructions.

Docker runs the instructions for Dockerfile from top to bottom. To specify the base image, the first instruction must be FROM. A declaration that begins with the # character is considered a comment. RUN, CMD, FROM, EXPOSE, ENV, and other commands can be used in Docker files.

Volume mounting use

Docker run-it -v /home/data docker run-it -v /home/data docker run-it -v /home/data/home centos /Bin /bash # Check all details of the docker inspect container IDCopy the code

Named mount and anonymous mount

-v If the path in the container is not written to the host machine, it is an anonymous mount

Run -d -p --name nginx_01 -v /etc/nginx nginx 0bd0439abb6511c914de41975ca4f9d2e07eaab969d92981277092d4673bd6d7 local 0fe72108880078fa40e4060e8695a56e1518402069a6c0f559092ad9b5aadbb7 local 8f4456ba8f2288e680fdc466b4b35853ee134700e8d21d79911e91163a6e3c60 local 08 f05b86ac17558c8b32b36ff7ac01907e115e4c6e9c0bba0d5c91b1dc5d2cc3 # to check a container volume mount specific circumstances docker volume inspect container id # named mount docker run -d -P --name nginx_01 -v test-nginx :/etc/Nginx nginx # again to check the docker volume ls local 0 bd0439abb6511c914de41975ca4f9d2e07eaab969d92981277092d4673bd6d7 local 0fe72108880078fa40e4060e8695a56e1518402069a6c0f559092ad9b5aadbb7 local 8f4456ba8f2288e680fdc466b4b35853ee134700e8d21d79911e91163a6e3c60 local 08f05b86ac17558c8b32b36ff7ac01907e115e4c6e9c0bba0d5c91b1dc5d2cc3 local test-nginxCopy the code

Exercise: Hang data in mysql

--name Docker run -d -p --name docker run -d -p3301:3306\ 
-v /home/mysql/conf:/etc/mysql/conf.d\ 
-v /home/mysql/data:/var/lib/mysql\ 
-e MYSQL_ROOT_PASSWORD=123456\ 
--name mysql_01 mysql

Copy the code

For networking related docker Componse and Docker Swam Cluster, see [Docker] Front-end From Start to Proficient (2).

Refer to the article

  • Portainer click the connect error: a Failure always connect to the daemon at Docker Unix: / / / var/run/Docker. The sock.

  • Yeasy. Gitbook. IO/docker_prac…