“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

1. Why does Docker appear

1.1 requirements

Software development is generally divided into two parts, the first is local development, the second is server deployment. If a project is developed locally and then put on a server, the installation environment can sometimes be very complex, and because of this complexity, large companies have operations staff dedicated to project release. To solve this problem, Docker technology came into being.

1.2 Technical Features

Environment isolation :Docker is a virtualization technology that features environment isolation. As we know, since it is virtualization technology, environmental isolation must be one of the major characteristics. For example, we used the virtual machine virtualBox before. Many system images can be installed through this software, and then the isolation between systems can be achieved.

Small image: The environment configuration in Docker, each small piece is a container, has its own set of environment, and the kernel is also a simplified version of the kernel. Can achieve very small size

Allow for faster delivery and deployment: In today’s agile world, it is important to develop applications quickly. The traditional way is to write a bunch of documents for the operation and maintenance personnel to use, and then the operation and maintenance personnel configure the environment according to the document and run the developed JAR package into. So now with Docker, developers just need to package up the JAR + environment, throw it to o&M and get o&M up and running. In addition, if the application needs to be upgraded, then the environment needs to be changed, which is very troublesome and painful. After using Docker, the new content can be packaged and sent to operation and maintenance for execution again.

2. The history of the Docker

Just like the Logo of Docker, the idea of Docker comes from containers. What problem does the container solve? On a large ship, goods can be arranged neatly, and all kinds of goods can be standardized by containers, which do not interfere with each other. There would be no need for ships for fruit and ships for chemicals. As long as the goods are packed in separate containers, a big ship can take them all away.

After the birth of Docker technology, it did not attract the attention of the industry. DotCloud, a small start-up, is also struggling under intense competition. Just when they were about to give up, the idea of “open source” popped into their minds. In March 2013, “Solomon Hykes,” the 28-year-old co-founder of dotCloud and the father of Docker, officially decided to open source the Docker project.

If you do not open, you will be surprised. More and more IT engineers are discovering Docker’s strengths and flocking to join the Docker open source community. Docker’s popularity has risen so quickly that it is jaw-dropping. The month of open source, Docker version 0.1 was released. Every month since, Docker has released a version. By June 9, 2014, Docker version 1.0 was released.

At this time, Docker has become the most popular open source technology in the industry. Even the likes of Google, Microsoft, Amazon, VMware and other giants have expressed their support for it. After Docker became so popular, dotCloud simply changed its name to Docker Inc.

3. The advantages of the Docker

“More efficient Use of System Resources”

Docker has a higher utilization of system resources because the container does not require additional overhead such as hardware virtualization and running a full operating system. Whether it is application execution speed, memory consumption or file storage speed, it is more efficient than traditional VIRTUAL machine technology. Therefore, a host with the same configuration can often run more applications than virtual machine technology.

“Faster startup time”

Traditional VIRTUAL machine technology usually takes several minutes to start application services, while Docker container applications can be started in seconds or even milliseconds because they run directly in the host kernel and do not need to start the complete operating system. Greatly saving the development, testing, deployment time.

“Consistent Operating Environment”

A common problem in development is environmental consistency. Because the development environment, test environment, and production environment are inconsistent, some bugs are not found in the development process. The image of Docker provides a complete runtime environment in addition to the kernel, ensuring the consistency of the application running environment, so that there will no longer be “no problem with this code on my machine”.

“Continuous Delivery and Deployment”

The most desirable thing for development and operations (DevOps) people is a single build or configuration that can run anywhere. With Docker, continuous integration, continuous delivery and deployment can be achieved by customizing application images. Developers can use Dockerfile for image building and Integration testing with Continuous Integration systems, while operations can quickly deploy the image directly into a production environment. Even automatic Deployment with Continuous Delivery/Deployment systems. Moreover, using Dockerfile to make image construction transparent, not only the development team can understand the application operating environment, but also facilitate the operation and maintenance team to understand the application operating conditions, helping to better deploy the image in the production environment.

“Easier Migration”

Docker ensures the consistency of execution environment, making application migration easier. Docker can run on many platforms, whether physical machine, virtual machine, public cloud, private cloud, or even laptop, and its running results are consistent. Therefore, users can easily migrate an application running on one platform to another without worrying that the application will not run properly due to the change of the operating environment.

“Easier maintenance and extension”

Docker uses layered storage and image technology, which makes it easier to reuse the repeated parts of the application, easier to maintain and update the application, and it is also very simple to further expand the image based on the basic image. In addition, Docker team maintains a large number of high-quality official images together with various open source project teams, which can be used directly in the production environment and further customized as a basis, greatly reducing the cost of image production of application services.

4. Comparison between containers and VMS

The following image compares the difference between Docker and traditional virtualization. It can be seen that container virtualization is implemented at the operating system level and directly reuse the local host operating system, while traditional virtualization is implemented at the hardware level.

Compared with traditional virtual machines, Docker has advantages of fast startup speed and small footprint. In traditional virtual machines, virtual hardware is created to run a complete operating system, and software is installed and run on this system. Applications in the container run directly inside the host computer. The container has no kernel of its own and no virtual hardware, so it is light.

Introduction to Docker

1. The Docker installation

Under the guidance of the website to install — docs.docker.com/engine/inst… (the following uses centos7 as an example.)

Install the yum-utils package (which provides the yum-config-manager utility) and set up the stable repository.

$ sudo yum install -y yum-utils
​
$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
Copy the code

Install the latest versions of Docker Engine and Containerd

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

Start the Docker.

$ sudo systemctl start docker
Copy the code

Verify that the Docker Engine is installed correctly by running the Hello-world image.

docker run hello-world
Copy the code

As shown in the figure above, the installation is successful. When docker Run is executed, the process is as shown in the figure below:

2. Basic Docker commands

2.1 Help Commands

Docker info # System level information, including the number of images and containers docker command --helpCopy the code

Docs.docker.com/engine/refe… — Official help document

2.2 Mirroring Commands

Docker images – View images on all localhosts

  1. REPOSITORY # Mirror warehouse
  2. TAG # mirror tag
  3. IMAGE ID # Mirror ID
  4. CREATED The creation time of the image
  5. SIZE # Mirror size

Docker pull Pull-down image

Docker pull image name [:tag]Copy the code

Docker RMI delete image

# docker rmi -f IMAGE ID # docker rmi -f IMAGE ID # docker rmi -f IMAGE ID # docker rmi -f IMAGE ID # docker rmi -f IMAGE ID -aq) # delete all mirrorsCopy the code

2.3 Container Commands

A container is created on the basis of an image. You can start it as a container only when you have an image. Take centos as an example

Create a new container and start

Docker run [Optional] image # Parameter Description --name= "name" Container name tomcat01 tomcat02 -p Specified container port -p 8080:8080 -p IP: host port: container port -p Host port: Container port (common) -p container port container port -p Randomly specified port # test, [root@iZ2zeg4ytp0whqtmxbsqiiZ ~]# docker run -it centos /bin/bash [root@74e82b7980e7 /]# ls # Bin etc lib lost+found MNT proc run SRV TMP var dev home lib64 media opt root sbin sys usr # [root@77969f5dcbf9 /]# exit exit [root@iZ2zeg4ytp0whqtmxbsqiiZ /]# ls bin dev fanfan lib lost+found mnt proc run srv tmp  var boot etc home lib64 media opt root sbin sys usrCopy the code

List all running containers

# docker ps command # list currently running containers -a # list currently running containers including history containers -n=? [root@iZ2zeg4ytp0whqtmxbsqiiZ /]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@iZ2zeg4ytp0whqtmxbsqiiZ /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 77969f5dcbf9 centos "/bin/bash" 5 minutes ago Exited (0) 5 minutes ago xenodochial_bose 74e82b7980e7 centos "/bin/bash" 16 minutes ago Exited (0) 6 minutes ago silly_cori a57250395804 bf756fb1ae65 "/hello" 7 hours ago Exited (0) 7 hours ago elated_nash 392d674f4f18 bf756fb1ae65 "/hello" 8 hours ago Exited (0) 8 hours ago distracted_mcnulty 571d1bc0e8e8 bf756fb1ae65 "/hello" 23 hours ago Exited (0) 23 hours ago magical_burnell [root@iZ2zeg4ytp0whqtmxbsqiiZ /]# docker ps -qa 77969f5dcbf9 74e82b7980e7 a57250395804 392d674f4f18 571d1bc0e8e8Copy the code

Remove the container

Docker rm -f containers id # delete specified docker rm -f $(docker ps - aq) # delete all containers docker ps - a - q | xargs docker rm -f # to delete all of the containerCopy the code

Start and stop container operations

Docker start container id # start the container docker restart container id # container restart docker stop containers id # stop the current running docker kill container id # forced to stop the current containerCopy the code

2.4 Other Common Commands

Background startup container

Command docker run -d Specifies the image nameCopy the code

However, Docker found centos stopped after ps — it should be noted that docker container uses background running, there must be a foreground process, Docker found no application, it will automatically stop

See the log

Docker logs -tf --tail number Container id [root@iZ2zeg4ytp0whqtmxbsqiiZ /]# docker logs -tf --tail 1 8d1621e09bff 2020-08-11T10:53:15.987702897z [root@8d1621e09bff /]# exit # log output # display log -tf # display log --tail number # display log numberCopy the code

View the metadata of the mirror

Docker inspect Container IDCopy the code

Enter the currently running container

[root@iZ2zeg4ytp0whqtmxbsqiiZ /]# docker execit df358bc06b17 /bin/bash [root@df358bc06b17 /]# ls bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr [root@df358bc06b17 /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Aug11 pts/0 00:00:00 /bin/bash root 29 0 0 01:06 PTS /1 00:00:00 /bin/bash root 43 29 0 01:06 PTS /1 00:00:00 ps -ef # After entering the container, open a new terminal. You can use # docker attach # to enter the terminal that the container is executing. No new process is startedCopy the code

Docker deployment

1. The following uses nginx as an example

1.1 install Nginx Docker

Go to the Docker Hub for a search, you can see the help document # 2. [root@iZ2zeg4ytp0whqtmxbsqiiZ home]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos Nginx latest 0d120b6CCAA8 32 hours ago 215MB nginx latest 08393e824c32 7 days ago 215MB [root@iZ2zeg4ytp0whqtmxbsqiiZ home]# docker run -d --name nginx01 -p 3344:80 nginx # fe9dc33a83294b1b240b1ebb0db9cb16bda880737db2c8a5c0a512fc819850e0 [root@iZ2zeg4ytp0whqtmxbsqiiZ home]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fe9dc33a8329 nginx "/ docker-entryPoint...." 4 seconds ago Up 4 seconds 0.0.0.0:3344->80/ TCP nginx01 [root@iZ2zeg4ytp0whqtmxbsqiiZ home]# curl localhost:3344 # Local access test # Enter the container [root@iZ2zeg4ytp0whqtmxbsqiiZ home]# docker exec -it nginx01 /bin/bash root@fe9dc33a8329:/# whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx root@fe9dc33a8329:/# cd /etc/nginx/ root@fe9dc33a8329:/etc/nginx# ls conf.d koi-utf mime.types nginx.conf uwsgi_params fastcgi_params koi-win modules scgi_params win-utfCopy the code

1.2Port exposure concept

After the docker is installed with nginx, run the -p command to map the local port 3344 to the container port 80 (nginx default port 80), as if to establish a connection, by accessing the local port 3344 is equivalent to accessing the container port 80. But need to open the firewall port to port security group and exposure (if the firewall), different service providers to provide the server has a corresponding console, firewall configuration can be found on the console on the server, the server itself also has a firewall, the firewall can open also can close, also need to open ports) exposure. It can only be accessed if the ports are exposed.

2. Container data volume

There can be a data sharing technique between containers! The data generated in the Docker container is synchronized to the local — this is volume technology, directory mount, the directory in the container is mounted to the Linux directory, to prevent the container due to accidental data loss, even if the container is damaged local still stored in the container mounted directory data

Conclusion: Container persistence and synchronization operations, data can also be shared between containers!

2.1. Using a Data volume

Mode 1: Run the command directly to mount -v

[root@iZ2zeg4ytp0whqtmxbsqiiZ home]# docker run -v /home/ceshi:/home centos /bin/bashCopy the code

The data that hangs in the container is synchronized, even if the container is stopped and the local file is modified and the container is started again. You can view mounted information in the container metadata

Mode 2: Anonymous and named mount

Docker run -d -p --name nginx01 -v /etc/nginx nginx [root@iZ2zeg4ytp0whqtmxbsqiiZ ~]# docker volume ls DRIVER VOLUME NAME local 561b81a03506f31d45ada3f9fb7bd8d7c9b5e0f826c877221a17e45d4c80e096 local Fb6ca083005094cbd49572a0bffeec6daadfbc5ce772909bb00be760882 # 36083 this kind of circumstance is anonymous mount, we only write the container behind the -v path, not write outside the container path! [root@iZ2zeg4ytp0whqtmxbsqiiZ ~]# docker run -d -p --name nginx02 -v juming-nginx:/etc/nginx 26da1ec7d4994c76e80134d24d82403a254a4e1d84ec65d5f286000105c3da17 [root@iZ2zeg4ytp0whqtmxbsqiiZ ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 26DA1ec7d499 nginx "/ docker-entryPoint...." 3 seconds ago Up 2 seconds 0.0.0.0:32769->80/ TCP nginx02 486de1da03cb nginx "/ docker-entryPoint...." 3 minutes ago Up 3 minutes 0.0.0.0:32768->80/ TCP nginx01 [root@iZ2zeg4ytp0whqtmxbsqiiZ ~]# docker volume ls DRIVER VOLUME NAME local 561b81a03506f31d45ada3f9fb7bd8d7c9b5e0f826c877221a17e45d4c80e096 local # 36083 fb6ca083005094cbd49572a0bffeec6daadfbc5ce772909bb00be760882 local juming - nginx by -v volume name: # docker volume inspect juming-nginx [root@iZ2zeg4ytp0whqtmxbsqiiZ ~]# docker volume inspect juming-nginx [ { "CreatedAt": "2020-08-12T18:15:21+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data", "Name": "juming-nginx", "Options": null, "Scope": "local" } ]Copy the code

All docker container volume, without a specified directory in/var/lib/docker/volumes/XXXXX / _data while forming

Ps:

Docker run -d -p --name nginx02 -v docker run -d -p --name nginx02 -v juming-nginx:/etc/nginx:ro nginx docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx # ro Seeing ro indicates that the path can only be accessed from the host, not the container contentsCopy the code

3. The container to commit

Docker images are read-only, and when the container is started, a new writable layer is loaded on top of the image! This layer is called the container layer, and everything below the container is called the mirror layer

The Docker Commit operation converts the current container into an image, but it takes the actions you performed on the container to create a new image, so when you run the image again it creates a new container layer for you to operate on.

The commit operation:

Docker commit -m=" commit description "-a=" author" [TAG] docker commit -a="ytKing" -m="test commit" d798A5946C1f tomcat007:1.0Copy the code

Dockerfile Builds the Docker image

1. Dockerfile concept

A Dockerfile is a text file that contains a collection of commands.

If the name of a Dockerfile file is in this directory, you do not need to specify the location of the Dockerfile when building. Otherwise, -f is required to specify the file location and name

Warning! Caution against using the root/as the PATH for the build context, as it causes the build to transfer the entire contents of the hard drive to the Docker daemon.Copy the code

Create a folder

Create dockerfile

Docker build -t image name: tag. // The last dot indicates that the resource is in the current directory and cannot be omittedCopy the code

Validate the current image: Ideally, the container is started and ready to use after port mapping

After entering the container, start nginx –./usr/sbin/nginx nginx takes effect

Modify dockerfile

FROM centos
RUN yum update -y
RUN yum install -y nginx
RUN ./usr/sbin/nginx
Copy the code

You can’t go straight to Nginx after creating the container because there are no foreground tasks

Modify again can run

FROM centos
RUN yum update -y
RUN yum install -y nginx
CMD nginx -g "daemon off;"
Copy the code

2. Dockerfile constitute

2) RUN // The command to be executed when creating the image, not the command to be executed in the container 3) CMD CMD commands have three forms: CMD ["executable","param1","param2"] (exec form, this is the preferred form) This is the preferred table) CMD ["param1","param2"] (as (as default parameters to ENTRYPOINT) CMD command param1 param2 (Shell Form)Copy the code

3. Use Dockerfile to deploy the background JAR package

The mysql and Redis databases are deployed in separate Docker containers

3.1 – mysql container

Pull the official mirror

Docker pull mysql: 5.7Copy the code

Create a mysql mapping directory locally

mkdir -p /root/mysql/data /root/mysql/logs /root/mysql/conf
Copy the code

Create *.cnf file in /root/mysql/conf

touch my.cnf
Copy the code

Create containers to map data, logs, and configuration files to native

docker run -p 3306:3306 --name mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs:/logs -v / root/mysql/data: / var/lib/mysql - e MYSQL_ROOT_PASSWORD = root - d mysql: 5.7Copy the code

-d: indicates the background running container

-p maps container ports to local ports

-v Mounts the host directory to the container directory

-e Sets parameters

Start mysql container

docker start mysql
Copy the code

3.2- Project packaging

Put the backend project into jar package and put it on the server. Create a DockerFileDemo folder on the server to store jar package and Dockerfile

mkdir dockerfiledemo
Copy the code

Dockerfile:

3.3-Dockerfile Command Description

FROM: Specifies the base mirror. It must be the first command

MAINTAINER: MAINTAINER information

LABEL: adds metadata to an image

ADD: ADD local files to the container. Tar files are automatically decompressed (network compressed resources are not decompressed), and network resources can be accessed, similar to wget

COPY: similar to ADD, COPY does not automatically decompress files and cannot access network resources

WORKDIR: working directory, similar to the CD command

RUN: indicates the command executed during image construction

CMD: called after the container is built, that is, when the container is started.

ENTRYPOINT: Configures the container to be executable. With CMD, you can omit “application” and use only parameters.

ENV: Sets environment variables

EXPOSE: Specifies the port for external interaction

VOLUME: specifies the persistent directory

ARG: Used to specify variables passed to the build run time

USER: Specifies the USER name or UID used to RUN the container. Subsequent runs also use the specified USER. When specifying a USER using USER, you can use a USER name, UID, GID, or a combination of the two. When a service does not require administrator rights, you can use this command to specify a running user. And you can create the required users before

ONBUILD: Used to set the mirror trigger

3.4- Compile Dockerfile file to generate Docker image (image name cannot be uppercase)

docker build -t ytking-essays .
Copy the code

3.5- Run the built image

Port 8088 is the same as port 8088 in the configuration file of the project. After port 8088 is enabled for the security group and firewall of the server, you can access the interface through the API

3.6 – the test

The postman test succeeded. Procedure

View the output logs of container operation

Docker logs -f Container IDCopy the code

Docker Hub operation

Hub.docker.com/ – dockerhub url

By creating a user on DockerHub, you can upload your image to DockerHub as an open source image for others to use (similar to Github).

You can upload your image by logging in to your DockerHub account on the server

[root@iZ2zeg4ytp0whqtmxbsqiiZ ~]# docker push diytomcat The push refers to repository [docker.io/library/diytomcat] 2eaca873a720: Preparing 1b38cc4085a8: Preparing 088ebb58d264: Preparing c06785a2723d: Preparing 291F6e44771A: Preparing denied: Requested access to the resource is denied #Copy the code

Dockerhub will be rejected if you want to upload your own image to dockerHub via push. You need to add your own dockerHub user name and version number before the image name.

Docker push ytking/diytomcat: 1.0Copy the code

Ytking is my own username, version 1.0, so I can successfully upload the image, and pull is the same as long as I change push to pull.