1. Docker overview
(1) Basic introduction
Docker is an open source application container engine, which is based on Go language and complies with Apache 2.0 0 protocol is open source.
Docker allows developers to package their applications and dependency packages into a lightweight and portable container, and then publish them to any popular Linux machine. It can also realize virtualization.
Containers completely use the sandbox mechanism, and there will be no interfaces between them (similar to iPhone app s). More importantly, the performance overhead of containers is very low.
After version 17.03, Docker is divided into CE (Community Edition) and EE (Enterprise Edition). We can use the Community Edition. Official website: https://docs.docker.com/
(2) Application scenario
-
Automated packaging and publishing of Web applications.
-
Automated testing and continuous integration and release.
-
Deploy and adjust databases or other background applications in a service-oriented environment.
-
Compile or extend the existing OpenShift or Cloud Foundry platform from scratch to build your own PaaS environment.
(3)Docker's advantages
Docker is an open platform for developing, delivering and running applications. Docker enables you to separate your applications from your infrastructure so that you can deliver software quickly. With docker, you can manage your infrastructure the same way you manage your applications. By using docker's method to quickly deliver, test and deploy code, you can greatly reduce the delay between writing code and running code in a production environment.
1. Deliver your applications quickly and consistently. Docker allows developers to work in a standardized environment using the local container of your application or service, thus simplifying the development life cycle.
Container is very suitable for continuous integration and continuous delivery (CI / CD) workflow. Please consider the following example scheme:
Your developers write code locally and share their work with colleagues using the Docker container.
They use Docker to push their applications into the test environment and perform automatic or manual tests.
When developers find errors, they can fix them in the development environment and then redeploy them to the test environment for testing and verification.
After the test, pushing the patch to the production environment is as simple as pushing the updated image to the production environment.
2. Responsive deployment and expansion
Docker is a container based platform that allows highly portable workloads. Docker containers can be run on developers' native machines, physical or virtual machines in data centers, cloud services or hybrid environments.
Docker's portability and lightweight features also enable you to easily complete the workload of dynamic management, and expand or dismantle applications and services in real time according to business requirements.
3. Running more workloads on the same hardware
Docker is light and fast. It provides a viable, cost-effective alternative to hypervisor based virtual machines, so you can leverage more computing power to achieve business goals. Docker is very suitable for high-density environments and small and medium-sized deployments, and you can do more with less resources.
2. Virtualization technology and container technology
Virtualization technology features: 1 More resources 2 Redundant steps 3 Slow start
Containerization Technology: containerization technology is not a complete operating system for simulation
Compare the difference between Docker and virtual machine:
1. Traditional virtual machine, virtual hardware, running a complete operating system, and then installing and running software on this system.
2. The applications in the docker container run directly on the content of the host. The container does not have its own kernel or virtual hardware.
3. Each container is isolated from each other. Each container has its own file system and does not affect each other.
Benefits of containerization:
3. Basic composition of docker
The basic composition of Docker is as follows:
explain:
4. Installation of docker
View the kernel of the system:
uname -r
The system kernel version is 3.10.0
[root@iZwz99sm8v95sckz8bd2c4Z ~]# uname -r 3.10.0-957.21.3.el7.x86_64
View system configuration
cat /etc/os-release
[root@iZwz99sm8v95sckz8bd2c4Z ~]# cat /etc/os-release NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7"
Installation steps of Docker:
(1) Uninstall old version
yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
(2) Download the required installation package
yum install -y yum-utils
(3) Set up a mirrored warehouse
yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo # foreign address # Set up Alibaba cloud Docker image warehouse yum-config-manager \ --add-repo \ https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # foreign address
(4) Update yum package index
yum makecache fast
(5) Install docker related configuration
Docker CE is the community version and docker EE is the enterprise version
yum install docker-ce docker-ce-cli containerd.io
When completed appears, the installation is successful.
(6) Start Docker
systemctl start docker # Check the current version number to see if it is started successfully docker version # Set startup and self startup systemctl enable docker
result:
Download the Hello world image for testing
View the downloaded hello world image
[root@iZwz99sm8v95sckz8bd2c4Z lib]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 11 months ago 13.3kB
5. Dock uninstallation
# 1. Uninstall dependency yum remove docker-ce docker-ce-cli containerd.io # 2. Delete resources/ var/lib/docker is the default working path of docker rm -rf /var/lib/docker
6. Configure alicloud image acceleration
(1) Go to Alibaba cloud's official website and search for container image services
(2) Execute the four official orders in turn
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://axvfsf7e.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
8. Docker container operation process
Start a container, and the operation flow of Docker is as follows:
9. Underlying principle
Docker is a client server system. The daemon of docker runs on the host and is accessed from the client through the Socker! After receiving the docker client instruction, the Docker Server will execute the instruction!
Why is Docker faster than VM Ware?
1. Docker has fewer abstraction layers than virtual machines
2. docker uses the kernel of the host, and the VM needs the Guest OS
When Docker creates a new container, it does not need to reload an operating system kernel like a virtual machine, but directly use the operating system of the host, while the virtual machine needs to load the Guest OS. The comparison between Docker and VM is as follows:
10. Common docker commands
10.1 basic command
docker version #View the version information of docker docker info #View the system information of docker, including the number of images and containers docker command --help #Help command (optional parameters can be viewed) docker COMMAND --help
Help document address for the command: https://docs.docker.com/engine/reference/commandline/docker/
10.2 mirror command
1.docker images view all images of the local host
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 11 months ago 13.3kB #Explanation: 1.REPOSITORY Mirrored warehouse source 2.TAG Mirrored label 3.IMAGE ID mirrored id 4.CREATED Creation time of the image 5.SIZE Mirror size # Optional parameters -a/--all List all mirrors -q/--quiet Show only mirrored id
2.docker search search image
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation... 10308 [OK] mariadb MariaDB is a community-developed fork of MyS... 3819 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create... 754 [OK] percona Percona Server is a fork of the MySQL relati... 517 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 86 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr... 79 centurylink/mysql Image containing mysql. Optimized to be link... 60 [OK] #Optional parameters Search the Docker Hub for images 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 #Search for images with more than 3000 collections [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search mysql --filter=STARS=3000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation... 10308 [OK] mariadb MariaDB is a community-developed fordockerk of MyS... 3819 [OK]
3.docker pull image name [: tag] Download Image
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull mysql Using default tag: latest #If you do not write tag, the default is latest latest: Pulling from library/mysql 6ec7b7d162b2: Pull complete #Layered download, the core of docker image - federated file system fedd960d3481: Pull complete 7ab947313861: Pull complete 64f92f19e638: Pull complete 3e80b17bff96: Pull complete 014e976799f9: Pull complete 59ae84fee1b3: Pull complete ffe10de703ea: Pull complete 657af6d90c83: Pull complete 98bfb480322c: Pull complete 6aa3859c4789: Pull complete 1ed875d851ef: Pull complete Digest: sha256:78800e6d3f1b230e35275145e657b82c3fb02a27b2d8e76aac2f5e90c1c30873 #autograph Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest #The real address of the download source #docker pull mysql is equivalent to docker pull docker io/library/mysql:latest
Specified version download
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull mysql:5.7 5.7: Pulling from library/mysql 6ec7b7d162b2: Already exists fedd960d3481: Already exists 7ab947313861: Already exists 64f92f19e638: Already exists 3e80b17bff96: Already exists 014e976799f9: Already exists 59ae84fee1b3: Already exists 7d1da2a18e2e: Pull complete 301a28b700b9: Pull complete 529dc8dbeaf3: Pull complete bc9d021dc13f: Pull complete Digest: sha256:c3a567d3e3ad8b05dfce401ed08f0f6bf3f3b64cc17694979d5f2e5d78e10173 Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7
4.docker rmi delete image
#1. Delete the specified image id [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f image id #2. Delete multiple image IDS [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f image id image id #3. Delete all image IDs [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f $(docker images -aq)
10.3 container command
For example, pull a centos container
docker pull centos
Run container
docker run [Optional parameters] image #Parameter description --name="name" Specify container name -d Run in background mode -it Run interactively,Enter the container to view the contents -p Specifies the port of the container ( -p ip:Host port:Container port configuration host ports are mapped to container ports -p Host port:Container port -p Container port ) -P Randomly assigned port(Capitalized P)
Enter container
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it centos /bin/bash [root@bd1b8900c547 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
Exit container
#Exit stop and exit the container (only exit if running in background mode) #Ctrl+P+Q do not stop container exit [root@bd1b8900c547 /]# exit exit [root@iZwz99sm8v95sckz8bd2c4Z ~]#
Lists the containers that have been run
#docker ps # Lists currently running containers -a # List the operation records of all containers -n=? # Displays the n recently created containers -q # Displays only the number of the container [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bca129320bb5 centos "/bin/bash" 4 minutes ago Exited (0) 3 minutes ago optimistic_shtern bd1b8900c547 centos "/bin/bash" 6 minutes ago Exited (0) 5 minutes ago cool_tesla cf6adbf1b506 bf756fb1ae65 "/hello" 5 hours ago Exited (0) 5 hours ago optimistic_darwin
Delete container
docker rm container id #Delete the specified container. You cannot delete the running container. Force the deletion to use rm -f docker rm -f $(docker ps -aq) #Delete all containers docker ps -a -q|xargs docker rm #Delete all containers
Start and stop containers
docker start container id #Start container docker restart container id #Restart container docker stop container id #Stops the currently running container docker kill container id #Force stop of current container
10.4 other common commands
1. Log viewing:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker logs --help Usage: docker logs [OPTIONS] CONTAINER Fetch the logs of a container 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:37Z) or relative (e.g. 42m for 42 minutes) -n, --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:37Z) or relative (e.g. 42m for 42 minutes) Common: docker logs -tf container id docker logs --tail number container id #num is the number of log entries to display #The docker container runs in the background. There must be a foreground process, otherwise it will stop automatically #Write a shell script to execute circularly to keep the centos container running [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d centos /bin/sh -c "while true;do echo hi;sleep 5;done" c703b5b1911ff84d584390263a35707b6024816e1f46542b61918a6327a570dc [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c703b5b1911f centos "/bin/sh -c 'while t..." 13 seconds ago Up 10 seconds pedantic_banach [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker logs -tf --tail 10 c703b5b1911f 2020-12-27T03:34:07.255599560Z hi 2020-12-27T03:34:12.257641517Z hi 2020-12-27T03:34:17.259706294Z hi 2020-12-27T03:34:22.261693707Z hi 2020-12-27T03:34:27.262609289Z hi 2020-12-27T03:34:32.267862677Z hi 2020-12-27T03:34:37.270382873Z hi 2020-12-27T03:34:42.272414182Z hi 2020-12-27T03:34:47.274823243Z hi 2020-12-27T03:34:52.277419274Z hi
2. View the process information in the container
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker top c703b5b1911f UID PID PPID C STIME TTY TIME CMD root 11156 11135 0 11:31 ? 00:00:00 /bin/sh -c while true;do echo hi;sleep 5;done root 11886 11156 0 11:43 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5
3. View the metadata of the container
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker inspect container id
4. Enter the currently running container
Because our container usually runs in the background mode, sometimes we need to enter the container to modify the configuration
Mode 1:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it c703b5b1911f /bin/bash [root@c703b5b1911f /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@c703b5b1911f /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 03:31 ? 00:00:00 /bin/sh -c while true;do echo hi;sleep 5;done root 279 0 0 03:54 pts/0 00:00:00 /bin/bash root 315 1 0 03:56 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5 root 316 279 0 03:56 pts/0 00:00:00 ps -ef
Mode 2:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker attach c703b5b1911f
After docker exec enters the container, open a new terminal where you can operate
docker attach enters the terminal where the container is executing and will not start a new process
Copy container files to host
docker cp container id: path within container destination host
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it c703b5b1911f /bin/bash [root@c703b5b1911f /]# cd home [root@c703b5b1911f home]# ls #touch new file [root@c703b5b1911f home]# touch test.java [root@c703b5b1911f home]# ls test.java [root@c703b5b1911f home]# exit exit [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c703b5b1911f centos "/bin/sh -c 'while t..." 35 minutes ago Up 35 minutes pedantic_banach [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker cp c703b5b1911f:/home/test.java /home [root@iZwz99sm8v95sckz8bd2c4Z ~]# ls /home hai pan test.java
The illustration of the command section is as follows:
11. Installation of graphical management tool Portaniner
Portaniner is a graphical management tool for Docker. Similar tools include Rancher(CI/CD reuse)
Download and run the portable image and set the local mapping port to 8088
[root@localhost conf]# docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer Unable to find image 'portainer/portainer:latest' locally latest: Pulling from portainer/portainer 94cfa856b2b1: Pull complete 49d59ee0881a: Pull complete a2300fd28637: Pull complete Digest: sha256:fb45b43738646048a0a0cc74fcee2865b69efde857e710126084ee5de9be0f3f Status: Downloaded newer image for portainer/portainer:latest 8c525a0137be22965bd1e3944da622a2c4248f8ad20883f4b3ea4f8a6b11e163 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7789d4505a00 portainer/portainer "/portainer" 6 seconds ago Up 5 seconds 0.0.0.0:8088->9000/tcp quirky_sinoussi
Set the password of admin user for the first login
If it is an alicloud server, remember to set a security group and select the local Docker. The overall interface preview is shown in the figure below:
12. Detailed explanation of docker image
12.1 what is mirroring
Image is a lightweight and executable independent software package, which is used to package the software running environment and the software developed based on the running environment. It contains all the contents required to run a software, including code, runtime (the dependency of a program running or being executed), library, environment variables and configuration files.
12.2 Docker image loading principle
The Docker image is actually composed of a layer by layer file system, which is the UnionFS federated file system.
12.3 layered understanding
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker image inspect nginx:latest [ { "Id": "sha256:ae2feff98a0cc5095d97c6c283dcd33090770c76d63877caa99aefbbe4343bdd", "RepoTags": [ "nginx:latest" ], "RepoDigests": [ "nginx@sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9" ], "Parent": "", "Comment": "", "Created": "2020-12-15T20:21:00.007674532Z", "Container": "4cc5da85f27ca0d200407f0593422676a3bab482227daee044d797d1798c96c9", "ContainerConfig": { "Hostname": "4cc5da85f27c", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.19.6", "NJS_VERSION=0.5.0", "PKG_RELEASE=1~buster" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD [\"nginx\" \"-g\" \"daemon off;\"]" ], "Image": "sha256:13bffe371b56f4aeed88218ec17d0c6f653a83b49bd3e211fc8cfa2ca5d7a3d3", "Volumes": null, "WorkingDir": "", "Entrypoint": [ "/docker-entrypoint.sh" ], "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGQUIT" }, "DockerVersion": "19.03.12", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.19.6", "NJS_VERSION=0.5.0", "PKG_RELEASE=1~buster" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "Image": "sha256:13bffe371b56f4aeed88218ec17d0c6f653a83b49bd3e211fc8cfa2ca5d7a3d3", "Volumes": null, "WorkingDir": "", "Entrypoint": [ "/docker-entrypoint.sh" ], "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGQUIT" }, "Architecture": "amd64", "Os": "linux", "Size": 132935043, "VirtualSize": 132935043, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/cb791e78a08db7091bf2ce1d78603f1758f52199e57f1805156fe30e39067aae/diff:/var/lib/docker/overlay2/1e73a72b25af68ee9abf4eb443f778d31226e12e9af428fcc14c7b044c83b258/diff:/var/lib/docker/overlay2/88c9c01762f2af8327db65d0b0d4a64785e87c9c2ab76c62e7d03619db03a985/diff:/var/lib/docker/overlay2/7304ab112ac4a9cb91fc6f74730be28fecbe19f042e92d321aa9181424cc4b2e/diff", "MergedDir": "/var/lib/docker/overlay2/48b288740bbb2b07b41ed43a4d17a005c46b08d3357d2960b5ef7db4b2de6618/merged", "UpperDir": "/var/lib/docker/overlay2/48b288740bbb2b07b41ed43a4d17a005c46b08d3357d2960b5ef7db4b2de6618/diff", "WorkDir": "/var/lib/docker/overlay2/48b288740bbb2b07b41ed43a4d17a005c46b08d3357d2960b5ef7db4b2de6618/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:87c8a1d8f54f3aa4e05569e8919397b65056aa71cdf48b7f061432c98475eee9", "sha256:5c4e5adc71a82a96f02632433de31c998c5a9e2fccdcbaee780ae83158fac4fa", "sha256:7d2b207c26790f693ab1942bbe26af8e2b6a14248969e542416155a912fec30d", "sha256:2c7498eef94aef8c40d106f3e42f7da62b3eee8fd36012bf7379becc4cd639a2", "sha256:4eaf0ea085df254fd5d2beba4e2c11db70a620dfa411a8ad44149e26428caee4" ] }, "Metadata": { "LastTagTime": "0001-01-01T00:00:00Z" } } ]
Hierarchical information is indicated here:
"RootFS": { "Type": "layers", "Layers": [ "sha256:87c8a1d8f54f3aa4e05569e8919397b65056aa71cdf48b7f061432c98475eee9", "sha256:5c4e5adc71a82a96f02632433de31c998c5a9e2fccdcbaee780ae83158fac4fa", "sha256:7d2b207c26790f693ab1942bbe26af8e2b6a14248969e542416155a912fec30d", "sha256:2c7498eef94aef8c40d106f3e42f7da62b3eee8fd36012bf7379becc4cd639a2", "sha256:4eaf0ea085df254fd5d2beba4e2c11db70a620dfa411a8ad44149e26428caee4" ] },
12.4 submitting images
use docker commit The command submission container becomes a new version docker commit -m=""Submitted description" -a="author" container id Target image name:[TAG]
Since there is nothing in the webapps folder of the default Tomcat image, you need to download it from webapps Copy files from dist to webapps folder. Now make your own image: from webapps Copy the file in dist to the webapps folder, and submit the image as a new image. Make the image have files in the default webapps folder. The specific commands are as follows:
#1. Copy folder [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it tomcat /bin/bash root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# cd ../ root@2a3bf3eaa2e4:/usr/local/tomcat# cp -r webapps.dist/* webapps root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2a3bf3eaa2e4 tomcat "/bin/bash" 4 minutes ago Up 4 minutes 8080/tcp competent_torvalds 7789d4505a00 portainer/portainer "/portainer" 24 hours ago Up 24 hours 0.0.0.0:8088->9000/tcp quirky_sinoussi [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it 2a3bf3eaa2e4 /bin/bash root@2a3bf3eaa2e4:/usr/local/tomcat# cd webapps root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager root@2a3bf3eaa2e4:/usr/local/tomcat/webapps# cd ../ root@2a3bf3eaa2e4:/usr/local/tomcat# read escape sequence [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2a3bf3eaa2e4 tomcat "/bin/bash" 8 minutes ago Up 8 minutes 8080/tcp competent_torvalds 7789d4505a00 portainer/portainer "/portainer" 24 hours ago Up 24 hours 0.0.0.0:8088->9000/tcp quirky_sinoussi #2. Commit the image as a new image [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker commit -m="add webapps" -a="Ethan" 2a3bf3eaa2e4 mytomcat:1.0 sha256:f189aac861de51087af5bc88a5f1de02d9574e7ee2d163c647dd7503a2d3982b [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mytomcat 1.0 f189aac861de 7 seconds ago 653MB mysql 5.7 f07dfa83b528 6 days ago 448MB tomcat latest feba8d001e3f 10 days ago 649MB nginx latest ae2feff98a0c 12 days ago 133MB centos latest 300e315adb2f 2 weeks ago 209MB portainer/portainer latest 62771b0b9b09 5 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 9 months ago 791MB #3. Operation container [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it mytomcat:1.0 /bin/bash root@1645774d4605:/usr/local/tomcat# cd webapps root@1645774d4605:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager wz99sm8v95sckz8bd2c4Z ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mytomcat 1.0 f189aac861de 7 seconds ago 653MB mysql 5.7 f07dfa83b528 6 days ago 448MB tomcat latest feba8d001e3f 10 days ago 649MB nginx latest ae2feff98a0c 12 days ago 133MB centos latest 300e315adb2f 2 weeks ago 209MB portainer/portainer latest 62771b0b9b09 5 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 9 months ago 791MB
13. Common container deployment
13.1 Nginx deployment
(1) Search and download images
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 14207 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con... 1932 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of... 797 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS... 137 jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho... 123 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp... 107 [OK] [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx 6ec7b7d162b2: Already exists cb420a90068e: Pull complete 2766c0bf2b07: Pull complete e05167b6a99d: Pull complete 70ac9d795e79: Pull complete Digest: sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images; REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 f07dfa83b528 5 days ago 448MB nginx latest ae2feff98a0c 11 days ago 133MB centos latest 300e315adb2f 2 weeks ago 209MB
You can view the detailed version information of Nginx on the official website of dockerhub: https://hub.docker.com/_/nginx
(2) Run test
docker run -d --name nginx01 -p 3334:80 nginx -d Background operation --name Name the container -p 3334:80 Map port 3334 of the host to port 80 of the container
Operation results:
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it nginx01 /bin/bash Error: No such container: nginx01 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d --name nginx01 -p 3334:80 nginx 20c896637ff5de8be835797109d62ee2465e28d9d716be5a8d550ef7d547fcf5 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 20c896637ff5 nginx "/docker-entrypoint...." 7 seconds ago Up 5 seconds 0.0.0.0:3334->80/tcp nginx01
Concept of port exposure:
(3) Configuration file
Enter the container and customize the configuration file
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it nginx01 /bin/bash root@20c896637ff5:/# whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx root@20c896637ff5:/# cd /etc/nginx root@20c896637ff5:/etc/nginx# ls conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf root@20c896637ff5:/# cd /etc/nginx root@20c896637ff5:/etc/nginx# ls conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
(4) Access test
For the local host access test, the curl command initiates a request. If you use Alibaba cloud server, you need to set a security group.
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 20c896637ff5 nginx "/docker-entrypoint...." 7 minutes ago Up 7 minutes 0.0.0.0:3334->80/tcp nginx01 [root@iZwz99sm8v95sckz8bd2c4Z ~]# curl localhost:3334 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
13.2 Tomcat deployment
(1) Download and run
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull tomcat Using default tag: latest latest: Pulling from library/tomcat 6c33745f49b4: Pull complete ef072fc32a84: Pull complete c0afb8e68e0b: Pull complete d599c07d28e6: Pull complete e8a829023b97: Pull complete d04be46a31d1: Pull complete db6007c69c35: Pull complete e4ad4c894bce: Pull complete 248895fda357: Pull complete 277059b4cba2: Pull complete Digest: sha256:57dae7dfb9b62a413cde65334c8a18893795cac70afc3be589c8336d8244655d Status: Downloaded newer image for tomcat:latest docker.io/library/tomcat:latest [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d -p 3335:8080 --name tomcat01 tomcat 7136295a6082cb0f805b025a1471bde02ead4864be3e2c9dcd337b1dde0a3113
(2) Enter container
1. There are fewer commands in the container
2. Alicloud image downloads the smallest image by default to ensure the smallest operating environment.
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker exec -it tomcat01 /bin/bash root@7136295a6082:/usr/local/tomcat# ls BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work root@7136295a6082:/usr/local/tomcat# cd webapps.dist root@7136295a6082:/usr/local/tomcat/webapps.dist# ls ROOT docs examples host-manager manager root@7136295a6082:/usr/local/tomcat/webapps.dist# cd ROOT root@7136295a6082:/usr/local/tomcat/webapps.dist/ROOT# ls RELEASE-NOTES.txt WEB-INF asf-logo-wide.svg bg-button.png bg-middle.png bg-nav.png bg-upper.png favicon.ico index.jsp tomcat.css tomcat.svg root@7136295a6082:/usr/local/tomcat/webapps.dist/ROOT# cd ../../ root@7136295a6082:/usr/local/tomcat# cd webapps root@7136295a6082:/usr/local/tomcat/webapps# ls root@7136295a6082:/usr/local/tomcat/webapps# cp -r /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps/ root@7136295a6082:/usr/local/tomcat/webapps# ls ROOT docs examples host-manager manager root@7136295a6082:/usr/local/tomcat/webapps# exit exit
(3) Access test
[root@iZwz99sm8v95sckz8bd2c4Z ~]# curl localhost:3335 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Apache Tomcat/9.0.41</title> <link href="favicon.ico" rel="icon" type="image/x-icon" /> <link href="tomcat.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <div id="navigation" class="curved container"> <span id="nav-home"><a href="https://tomcat.apache.org/">Home</a></span> <span id="nav-hosts"><a href="/docs/">Documentation</a></span> <span id="nav-config"><a href="/docs/config/">Configuration</a></span> <span id="nav-examples"><a href="/examples/">Examples</a></span> <span id="nav-wiki"><a href="https://wiki.apache.org/tomcat/FrontPage">Wiki</a></span> <span id="nav-lists"><a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></span> <span id="nav-help"><a href="https://tomcat.apache.org/findhelp.html">Find Help</a></span> <br class="separator" /> </div>
13.3 ElasticSearch deployment
Add '- e ES_JAVA_OPTS="-Xms128m -Xmx512m"'configure the memory size occupied by the virtual machine of ElasticSearch.
docker stats view resource usage
$ docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms128m -Xmx512m" elasticsearch:7.6.2 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d --name elasticsearch01 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms128m -Xmx512m" elasticsearch:7.6.2 3b8cd4991814896c523ee67b84ce198e32bd82b1a62d512b198138a58ca946f1 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3b8cd4991814 elasticsearch:7.6.2 "/usr/local/bin/dock..." 10 seconds ago Up 6 seconds 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch01 [root@iZwz99sm8v95sckz8bd2c4Z ~]# docker stats
Note summary: https://www.bilibili.com/video/BV1og4y1q7M4