1. Why Docker

Disadvantages of traditional VMS: Too many resources, too many redundant steps, and slow startup.

Advantages of Docker: Using containerization technology, project code is packaged into an image with environment configuration, and uploaded to the image warehouse, so that others can download the image and run it without complex environment configuration.

“Note: Containerization techniques do not emulate a complete operating system.” So Docker is popular because it’s lighter than a normal virtual machine!

Docker and virtual Machine technology differences:

  • Traditional VIRTUAL machines (VMS) virtualize a piece of hardware, run a complete operating system, and install and run software on the system.

  • Docker container applications directly run in the host computer content, container is not its own kernel, there is no virtual our hardware, so it is portable;

  • Each container is isolated from each other. Each container has its own file system, which does not affect each other.

Some terms in Docker

Docker terms:

  • Mirror image (image)

  • The container (the container)

  • Warehouse (repository)

Mirroring can be thought of as an overall package of some software and configuration environment.

The container can be thought of as a stripped-down Linux system that is an instance running through an image, which is the container. In this way, an image is similar to a Class type in Java, from which multiple instances can be created.

An image is something that exists on a hard disk, and when it runs, it forms a container, and the container is the program that actually runs. Except in Docker, we can go into the container, do something, and then commit the changes in the container to form a new image.

The repository is similar to Github. We will submit our written code to Github for storage and management, so that we can download the code in other places. A mirror warehouse is similar. If you have a good image, put it in the mirror warehouse so that others can deploy it directly with the image.

Docker installation and uninstallation

Environment preparation, Linux kernel 3.0 or above, I use ali cloud server (CentOS 8), view the relevant information command is as follows:

➜ ~ uname -r 4.18.0-193.14.2.el8_2.x86_64 # request 3.0 + ➜ ~ cat /etc/os-release NAME="CentOS Linux" VERSION="8 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="8" PLATFORM_ID="platform:el8" PRETTY_NAME="CentOS Linux 8 (Core)" ANSI_COLOR="0; 31" CPE_NAME="cpe:/o:centos:centos:8" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-8" CENTOS_MANTISBT_PROJECT_VERSION="8" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="8"Copy the code

Docker installation reference to their official document is completely can according to step (Docker official documents is quite detailed), url: docs.docker.com/engine/inst…

Here for the convenience of everyone to install the steps and corresponding notes to write out:

1, uninstall the old version, if the server has not installed Docker do not need to do this step.

yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
Copy the code

2. Required installation package:

yum install -y yum-utils
Copy the code

3, set the mirror warehouse, the official document is the default foreign warehouse, do not recommend to use, here recommend to use domestic, as follows:

yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Copy the code

Update yum Package index:

Yum makecache fast # yum makecache fast #Copy the code

4, install docker, Docker-CE represents the community version, and EE represents the enterprise version, here to install the community version.

yum install docker-ce docker-ce-cli containerd.io
Copy the code

IO >= 1.2.2-3 (containerd. IO >= 1.2.2-3)

(1) Reduce the version of Docker; (2) If you don’t want to reduce the docker version, update containerd. IO:

Wget https://download.docker.com/linux/centos/7/x86_64/edge/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm yum install - y containerd. IO - 1.2.6-3.3 el7. X86_64. RPMCopy the code

Then re-execute:

yum install docker-ce docker-ce-cli containerd.io
Copy the code

5. Start Docker.

systemctl start docker
Copy the code

6. Check whether the installation is successful.

[root@eric ~]# Docker version Client: Docker Engine - Community version: 19.03.13 API version: 1.40 Go version: Go1.13.15 Git commit: 4484c46d9d Built: Wed Sep 16 17:03:45 2020 OS/Arch: Linux/AMd64 Experimental: falseCopy the code

If the docker version information is displayed, the Docker is successfully installed.

7. Configure Aliyun.

The above code can be directly implemented step by step.

8, test run hello-world.

The first run automatically pulls the latest image from the mirror repository and then runs the image again.

docker run hello-world 
Copy the code

If the following information is displayed, the Hello-world image is successfully pulled and run.

Hello from Docker!
This message shows that your installation appears to be working correctly.
...
Copy the code

Docker run command operating principle:

How the run command works

9. View the image information downloaded from the current Docker.

➜ ~ Docker images REPOSITORY TAG IMAGE ID CREATED SIZE Hello -world latest BF756fb1AE65 4 months ago 13.3kBCopy the code

Uninstall Docker (why would you uninstall it unless you don’t use it) :

# yum remove docker-ce docker-ce cli containerd. IO # yum remove docker-ce /var/lib/docker # /var/lib/docker is the default working path of docker.Copy the code

Common Docker commands

There are many commands in Docker, and it is impossible to remember all of them. It is still like learning Linux commands, as long as you practice more, you can quickly master them. Plus, as long as Linux plays well, you can’t go wrong with Docker.

Docker command official documentation: docs.docker.com/reference/,…

The help command

Docker info displays information about the docker system, including the number of images and containers docker --help #Copy the code

The mirror command

Docker image ls can be used instead of docker search # search image docker pull # Download image docker rmI # delete imageCopy the code

These commands are described in detail below.

docker imagesView images on all local hosts.”

➜ ~ docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7e73346bdf465 24 hours ago 448MBCopy the code

Explanation:

REPOSITORY TAG # IMAGE ID # IMAGE ID CREATED # IMAGE creation time SIZE # IMAGE SIZECopy the code

The docker images command is optional:

Options: -a, --all Show all images (default hides intermediate images) # listing all mirrors -q, --quiet Only Show numeric IDs # displaying Only the id of the mirrorCopy the code

Such as:

➜ ~ docker images -aq # Show the id of all images e73346bdf465Copy the code

docker searchSearch image”

Usually, we will search the corresponding image in the official website of DockerHub, as follows:

Official website search image

But we can also do image search on the Docker command line, for example:

[root@eric ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation??  10202               [OK]                
mariadb                           MariaDB is a community-developed fork of MyS??  3753                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create??  746                                     [OK]
percona                           Percona Server is a fork of the MySQL relati??  511                 [OK]                  
Copy the code

Docker search command can also be added to the optional:

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
Copy the code

For example, we apply some filtering to the search results:

➜ ~ docker search mysql --filter=STARS=5000 NAME DESCRIPTION STARS OFFICIAL mysql mysql is a widely used, open-source relation... 9500 [OK]Copy the code

docker pullDownload the image”

Docker pull image name [:tag], for example, tomcat 8, command:

➜ ~ docker pull tomcat:8 # Default is latest 8: Pulling from library/tomcat 90fe46DD8199: Already exists # Layered download: Docker image's core federated file system 35a4F1977689: Already exists bbc37f14aded: Already exists 74e27dc593d4: Already exists 93a01fbfad7f: Already exists 1478df405869: Pull complete 64f0dd11682b: Pull complete 68ff4e050d11: Pull complete f576086003cf: Pull complete 3b72593ce10e: Pull complete Digest: Sha256:0 # c6234e7ec9d10ab32c06423ab829b32e3183ba5bf2620ee66de866df640a027 signature anti-counterfeiting Status: Downloaded newer image for tomcat: 8 docker. IO/library/tomcat: 8 # # real address specified download MySQL version, such as ➜ ~ docker pull MySQL: 5.7Copy the code

docker rmiDeleting a Mirror”

➜ ~ docker rmi -f image ID # Delete the specified image ➜ ~ docker rmi -f image ID Image ID Image ID # Delete multiple images ➜ ~ docker rmI -f $(docker images) -aq) # delete all mirror $() as passed argumentCopy the code

Container order

The container is the image in action.

Docker pull name [:tag] # create container docker run image # create container and start, Docker ps -a docker rm id docker rm id docker ps -a docker rm id Docker docker docker docker docker Docker restart docker stop Docker stop docker stop docker stop docker stop docker stop dockerCopy the code

Run container

Use the docker run image command to run the container. The options for this command are:

--name=" name "# tomcat01 tomcat02 -p # Specify the port of the container -p 8080(host):8080(container) -p (uppercase) # Randomly specify the portCopy the code

For example, to write a test here, start the MSYQL image and enter the container, use bash:

[root@eric ~]# docker run -it mysql /bin/bash root@6849133331da:/# ls bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media MNT opt Proc root run sbin SRV sys TMP usr var root@6849133331da:/# MySQL -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.32 MySQL Community Server (GPL) Copyright (C) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help; 'or '\h' for help.type '\c' to clear the current input statement. Mysql > show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 4 rows in the set (0.00 SEC) # exit database mysql > exit Bye # exit mysql root container @ 6849133331 da: / # exit exit [root@eric ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6849133331da mysql:5.7 "docker-entrypoint.s?? 11 days ago Up About a minute 0.0.0.0:3306->3306/ TCP, 33060/ TCP mysql01 # stop running CONTAINER [root@eric ~]# docker stop 6849133331da docker6849133331da [root@eric ~]# docker ps CONTAINER  ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@eric ~]#Copy the code

Some containers will stop running automatically after exiting, such as centos. If you want to keep exiting containers, you can use the key: CTRL + P + Q.

What if we want to use the database running in a container? It’s as simple as mapping its ports. When we run the database, we can use the following command: Docker run -d -p 3306:3306 mysql, which means running mysql database in background and mapping its port to server, then we enable the server firewall corresponding port 3306 and ali cloud security group 3306 port, Then you can use the public address and port number to connect to the database in our Docker container. For example, mine:

Connect to the Docker database

Enumeration container

Docker ps -a # lists all containers that have been runCopy the code

Exit container

CTRL + P + Q # The container does not stop exitingCopy the code

Delete Container

Docker rm container id # delete the specified container, cannot delete the running container, If you want to force to delete the rm - rf docker rm -f container id or $# (docker ps - aq) to delete the specified container or delete all information (including the history) docker ps - a - q | xargs docker rm # to delete all of the containerCopy the code

“Start and Stop container operations”

Docker stop docker stop docker stop docker stop docker kill docker stop docker stopCopy the code

Other commands

Background Startup Command

Sometimes we want the container to start in the background, for example, we want to run a centos container in the background, then we use the following command:

➜ ~ # command docker run - d image name docker run - d centos a8f922c255859622ac45ce3a535b7a0e8253329be4756ed6e32265d2dd2fac6cCopy the code

Now let’s look at the running container:

➜  ~ docker ps           
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
Copy the code

Centos stopped because no containers were running. This is a common pit, docker container to run in the background, must have a foreground process, Docker found no application, will automatically stop. For example, when docker is installed with nginx, when the container is started, it finds that it does not provide service, it will immediately stop, that is, there is no program.

View Logs

The options for using the docker logs command are:

docker logs --help Options: --details Show extra details provided to logs * -f, --follow Follow log output --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for  42 minutes) * --tail string Number of lines to show from the end of the logs (default "all") * -t, --timestamps Show timestamps --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)Copy the code

Display log:

Docker logs -t --tail n docker logs -t --tail n docker logs -tCopy the code

View Process Information in a Container

Docker Top Container IDCopy the code

View metadata of the Mirror

More important commands to view container details.

Docker inspect Container IDCopy the code

Enter the currently running container

We usually run the container in background mode, but sometimes we need to go into the container and modify some configuration. There are two ways to enter the container:

Docker attach container idCopy the code

The difference between the two methods:

Docker exec # opens a new terminal inside the current container. Docker attach # Enter the terminal where the container is being executedCopy the code

Testing:

➜ ~ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS 55321bcae33D centos "/bin/sh -c 'while t..." 10 minutes ago Up 10 minutes ➜ ~ docker exec -it 55321bcae33d /bin/bash [root@55321bcae33d /]# # Test attach mode ➜ ~ docker attach 55321bCAe33D Current code...Copy the code

“Copy container files to host”

Copy command:

Docker cp Container ID: container path Destination path of the hostCopy the code

Testing:

➜ ~ docker exec it 55321bcae33d /bin/bash [root@55321bcae33d /]# ls bin etc lib lost+found MNT proc run SRV TMP var dev home lib64 media opt root sbin sys usr # Create a new file [root@55321bcae33d /]# echo "hello" > test.java [root@55321bcae33d /]# cat test.java hello [root@55321bcae33d /]# exit exit ➜ ~ docker cp 55321bcae33d:/test.java / ➜ ~ CD / ➜ / ls etc test.java media root swapfile varCopy the code

conclusion

This article is an introduction to Docker. It starts from why Docker is chosen, that is, the advantages of Docker, and then describes several related terms in Docker — mirror, container and mirror warehouse. Then I introduced the installation and uninstall of Docker. Finally, it is more important to master some commands commonly used in Docker.