1 uninstall the old version:
yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
2 installation environment
yum install -y yum-utils
3 set up warehouse
yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Update package index
yum makecache fast
Installing docker ce
yum install docker-ce docker-ce-cli containerd.io
Start docker
sudo systemctl start docker docker run hello-world uninstall yum remove docker-ce docker-ce-cli containerd.io sudo rm -rf /var/lib/docker sudo rm -rf /var/lib/containerd
Alicloud configuration image accelerator
For users with Docker client version greater than 1.10.0
You can modify the daemon configuration file / etc / docker / daemon JSON to use the accelerator
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://32c9xrxj.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
Search image docker search mysql [root@alichen /]# docker search mysql --filter stars=3000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation... 11556 [OK] mariadb MariaDB Server is a high performing open sou... 4398 [OK] download--help docker pull mysql [root@alichen /]# docker pull mysql Using default tag: latest #Do not write tag, default to the latest version latest: Pulling from library/mysql b380bbd43752: Pull complete #Layered Download f23cbf2ecc5d: Pull complete 30cfc6c29c0a: Pull complete b38609286cbe: Pull complete 8211d9e66cd6: Pull complete 2313f9eeca4a: Pull complete 7eb487d00da0: Pull complete 4d7421c8152e: Pull complete 77f3d8811a28: Pull complete cce755338cba: Pull complete 69b753046b9f: Pull complete b2e64b0ab53c: Pull complete Digest: sha256:6d7d4524463fe6e2b893ffc2b89543c81dec7ef82fb2020a1b27606666464d87 #autograph Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest Equivalent to docker pull mysql Specified version download [root@alichen /]# docker pull mysql:5.7 5.7: Pulling from library/mysql b380bbd43752: Already exists f23cbf2ecc5d: Already exists 30cfc6c29c0a: Already exists b38609286cbe: Already exists 8211d9e66cd6: Already exists 2313f9eeca4a: Already exists 7eb487d00da0: Already exists #Public does not need to be downloaded again a71aacf913e7: Pull complete 393153c555df: Pull complete 06628e2290d7: Pull complete ff2ab8dac9ac: Pull complete Digest: sha256:2db8bfd2656b51ded5d938abcded8d32ec6181a9eae8dfc7ddf87a656ef97e97 Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7 Delete mirror file docker rmi -f ID docker rmi -f $(parameter docker images -ap)Delete all mirrors
-
Create and start a new container
docker pull centos docker run [Optional parameters] image --name #Container name -d #Background operation -it#Run interactively, walk into the container and view the content -p#Determine container port -p#Host port: container port (common) -P#Random port docker run -it centos /bin/bash Delete container docker rm -f $(docker ps -aq)#Delete all containers docker rm container ID docker ps -aq|xargs docker rm #Delete all Container start and stop operations docker start container ID #start-up docker restart container ID#restart docker stop container ID#stop it docker kill container ID #Kill compulsion ctrl+p+q Push out non-stop container
View docker logs
docker logs -f -t --tail Display rows container ID -tf Show all logs --tail Display rows display log rows
View the process information in the docker container
dokcer top container ID [root@alichen /]# docker top container ID UID PID PPID C STIME root 6618 6600 0 15:52
docker inspect ID viewing container source data
[root@alichen /]# docker inspect 28449bc1777f [ { "Id": "28449bc1777ff637afc8e04608bf0fbd1e3084267d723a459e0547fc8f73c447", "Created": "2021-10-21T07:49:38.807680988Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2021-10-21T07:50:11.909376361Z", "FinishedAt": "2021-10-21T07:51:14.720992886Z" }, "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6", "ResolvConfPath": "/var/lib/docker/containers/28449bc1777ff637afc8e04608bf0fbd1e3084267d723a459e0547fc8f73c447/resolv.conf", "HostnamePath": "/var/lib/docker/containers/28449bc1777ff637afc8e04608bf0fbd1e3084267d723a459e0547fc8f73c447/hostname", "HostsPath": "/var/lib/docker/containers/28449bc1777ff637afc8e04608bf0fbd1e3084267d723a459e0547fc8f73c447/hosts", "LogPath": "/var/lib/docker/containers/28449bc1777ff637afc8e04608bf0fbd1e3084267d723a459e0547fc8f73c447/28449bc1777ff637afc8e04608bf0fbd1e3084267d723a459e0547fc8f73c447-json.log", "Name": "/youthful_spence", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "CgroupnsMode": "host", "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/d2b35a0fa293ddc3c569161542e8d777b0691bf567a78f710fbbe44774976557-init/diff:/var/lib/docker/overlay2/6ce32b6151ac996b0dcab013a0a4c0260970c458e9d02ba2de4ca0b8500cb66c/diff", "MergedDir": "/var/lib/docker/overlay2/d2b35a0fa293ddc3c569161542e8d777b0691bf567a78f710fbbe44774976557/merged", "UpperDir": "/var/lib/docker/overlay2/d2b35a0fa293ddc3c569161542e8d777b0691bf567a78f710fbbe44774976557/diff", "WorkDir": "/var/lib/docker/overlay2/d2b35a0fa293ddc3c569161542e8d777b0691bf567a78f710fbbe44774976557/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "28449bc1777f", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "Image": "centos", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20210915", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "cadc50c6aeb28ea561b303df23623b4478ba556f3c04d4b368e3ca7dcdb0c04f", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/cadc50c6aeb2", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "05489496d9933ad15165664e8254cfe856b47e2f71e0d8a5c5303db4405ecbce", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ]
Enter the running container
docker exec -it 7b22a30a140a /bin/bash#Enter the running container and open a new terminal [root@alichen /]# docker exec -it 7b22a30a140a /bin/bash [root@7b22a30a140a /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 07:52 pts/0 00:00:00 /bin/bash root 15 0 0 08:17 pts/1 00:00:00 /bin/bash root 29 15 0 08:17 pts/1 00:00:00 ps -ef docker attach container ID#Enter the running container to continue the terminal
Copy files from the container to the local machine
docker cp container id:In container path destination path
[the external chain image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-6jmxn4ba-1644912741962) (C: \ users \ CHENY \ appdata \ roaming \ typora user images \ image-202110211635166663. PNG)]
Installing nginx
docker search nginx docker pull nginx docker run -it nginx01 /bin/bash [root@alichen /]# docker exec -it nginx01 /bin/bash
Install tomcat
docker run -d -p 3332:8080 --name tomcat9 tomcat:9.0 docker exec -it tomcat9 /bin/bash
docker install elasticsearch Comparative resource consumption docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
View system resource usage
docker stats
visualization
-
portainer
[root@alichen /]# docker run -d -p 8088:9000 \
–restart=always -v /var/run/docker.
–restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
Manage test portal
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-rizprjn2-1644912741964) (C: \ users \ CHENY \ appdata \ roaming \ typora \ typora user images \ image-20211022092241703. PNG)]
How to commit your changed image
docker commit own image
The command is similar to git
docker commit -m = "submitted description" - a = "author" container ID target image name [tag]
test
docker run -it -p 8080:8080 tomcat:9.0 docker exec -it 25670e3875b6 /bin/bash docker commit -a="alichen" -m="add tomcat app" 25670e3875b6 tomcat9:1.0
Container data volume
What is a container data volume
The database cannot exist in the container. If you delete the data, you will lose # data persistence
mysql data is stored locally
Data sharing technology
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-kydo9fdn-1644912741966) (C: \ users \ CHENY \ appdata \ roaming \ typora user images \ image-20211022101539601. PNG)]
Persistence and synchronization of containers
Using data volumes
Mode 1 -v command mount
docker -it -v Host Directory: directory in container
[the external chain image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-vccsogky-1644912741966) (C: \ users \ CHENY \ appdata \ roaming \ typora \ typora user images \ image-20211022102036469. PNG)]
docker run -it -v /home/ceshi:/home centos /bin/bash run a container and enter the interactive operation
[the external chain image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ddbmqwjt-1644912741967) (C: \ users \ CHENY \ appdata \ roaming \ typora user images \ image-20211022102347954. PNG)]
actual combat
Install mysql
docker pull mysql:5.7
mysql start
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=pw --name *** mysql:tag
##Specific and anonymous mount
Anonymous mount
docker run -d -P --name nginx01 -v /home/nginx nginx#Anonymous mount #View volume docker volume ls [root@alichen /]# docker volume ls DRIVER VOLUME NAME local 6d68f3fc5f7b816242aec6c7ff11bdd2e961d078d59eb60a012927e4f2616a3d local 54c7a21cd74c1a4c3a52163b61268121d25f424b4a8cad808d27c6606bb26d83
How to check whether it is anonymous or named
-v #Anonymous mounting of path in container -v #Volume name: / named mount in container path -v #Host internal path: the path in the container is mounted specifically -v #Volume name:/Path tool in container#Name mount docker volume inspect Volume name #You can view the specific location #Generally, it is in / var/lib/docker/volume / volume name/_ data #expand docker run -d -P --name nginx01 -v /home/nginx:ro nginx#readonly is not operable in a read-only container docker run -d -P --name nginx01 -v /home/nginx:rw nginx#Readable and writable
Dockerfile
Definition: there are build files to build docker images
#An image is generated through this script. The image is layered, and the script is also generated at each layer
[root@alichen home]# mkdir docker-test [root@alichen home]# cd docker-test/ [root@alichen docker-test]# ls [root@alichen docker-test]# vim dockerfile [root@alichen docker-test]# cat dockerfile FROM centos VOLUME ["volume01","volume02"] CMD -----end----- CMD /bin/bash [root@alichen docker-test]# docker build -f dockerfile -t aliyuncentos . #Create container and load Sending build context to Docker daemon 2.048kB Error response from daemon: dockerfile parse error line 5: Unknown flag: ---end----- [root@alichen docker-test]# vim dockerfile [root@alichen docker-test]# docker build -f dockerfile -t aliyuncentos . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos ---> 5d0da3dc9764 Step 2/4 : VOLUME ["volume01","volume02"] ---> Running in 85eeb751e768 Removing intermediate container 85eeb751e768 ---> cd7d6a948f10 Step 3/4 : CMD echo"---end-----" ---> Running in 9b857fe78a4b Removing intermediate container 9b857fe78a4b ---> cb8f55320954 Step 4/4 : CMD /bin/bash ---> Running in ce37ba390956 Removing intermediate container ce37ba390956 ---> adc7df11240b Successfully built adc7df11240b Successfully tagged aliyuncentos:latest [root@alichen docker-test]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE aliyuncentos latest adc7df11240b 9 seconds ago 231MB
Data volume synchronization
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-gyzwbci8-1644912741967) (C: \ users \ CHENY \ appdata \ roaming \ typora user images \ image-20211022142109624. PNG)]
After the backup mechanism deletes docker01, docker02 or data between multiple containers exist,
#Create parent container docker run -it --name docker01 aliyuncentos /bin/bash #Create 02 docker run -it --name docker02 --volumes-from docker01 aliyuncentos
Dockerfile
Create a mirror, command parameter script
Steps:
- Write a dockerfile
- docker build builds an image
- docker run runs a container
- docker push publishes images and uploads them to Alibaba cloud warehouse or dockerhub
Official centos build command
FROM scratch | |
---|---|
ADD centos-7-x86_64-docker.tar.xz / | |
LABEL \ | |
org.label-schema.schema-version="1.0" \ | |
org.label-schema.name="CentOS Base Image" \ | |
org.label-schema.vendor="CentOS" \ | |
org.label-schema.license="GPLv2" \ | |
org.label-schema.build-date="20201113" \ | |
org.opencontainers.image.title="CentOS Base Image" \ | |
org.opencontainers.image.vendor="CentOS" \ | |
org.opencontainers.image.licenses="GPL-2.0-only" \ | |
org.opencontainers.image.created="2020-11-13 00:00:00+00:00" | |
CMD ["/bin/bash"] |
The official is the basic package.
Construction process
#Each keyword is capitalized, and the instructions are executed from top to bottom,
dockerfile is a standard for development, project release and enterprise delivery
Steps: development, deployment, operation and maintenance are indispensable
dockerfile: the build file defines all the steps and source code
Docker images: the image generated through the docerfile construction, and the final released and running product
docker container: a container is a mirror image that provides services
#FROM everything starts FROM here #MAINTAINER author. Name + email #RUN run command #ADD plus compression package #WORKDIR working directory #VOLUME sets a mount VOLUME location #EXPOSE specifies the exposed port #CMD specifies that only the last command to be executed when the container is started can be replaced #ENTRYPOINT specifies the command to be executed when the container is started, and the command can be appended #ONBUILD When constructing an inherited dockerfile, the ONBULILD instruction will be fired #COPY is similar to ADD, which copies our I see to the image #Environment variables during ENV construction
#Write a dockerfile by yourself FROM centos MAINTAINER chen<123@qq.com> ENV MYPATH /usr/local WORKDIR $MYPATH RUN yum -y install vim RUN yum -y install net-tools EXPOSE 80 CMD echo $MYPATH CMD echo"--end--" CMD /bin/bash #Build your own image docker build -f dockerfile-centos -t centos:1.0 . #Start your own image docker run -it f9f6c9921d6e
There are many familiar commands in dokcerfile. Only through testing can we find some slight differences
actual combat
tomcat image
FROM centos:1.0 MAINTAINER alichen34917662@qqcom # now add java and tomcat support in the container ADD jdk1.8.0_251.tar.gz /usr/local/ ADD apache-tomcat-8.5.61.tar.gz /usr/local/ ENV MYPATH /usr/local WORKDIR $MYPATH # configuration of java and tomcat ENV ENV JAVA_HOME /usr/local/jdk1.8.0_251 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.61 ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.61 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin # container listener port EXPOSE 8080 # startup web application services by self CMD /usr/local/apache-tomcat-8.5.61/bin/startup.sh && CMD /bin/bash #Run mirror docker run -d -p 8090:8080 --name tomcat -v /home/build/tomcat8/web:/usr/local/apache-tomcat-8.5.61/webapps -v /home/build/tomcat8/logs:/usr/local/apache-tomcat-8.5.61/logs diytomcat
Deploy to docker hub
docker login -u username -p
1. Log in to alicloud Docker Registry $ docker login --username=**** registry.cn-hangzhou.aliyuncs.com The user name used for login is the full name of Alibaba cloud account, and the password is the password set when opening the service. You can modify the credential password on the access credential page. 2. from Registry Pull image from $ docker pull registry.cn-hangzhou.aliyuncs.com/:[Mirror version number] 3. Push mirror to Registry $ docker login --username=**** registry.cn-hangzhou.aliyuncs.com $ docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com//mydiy: [image version number] $ docker push registry.cn-hangzhou.aliyuncs.com/mydiy:[Mirror version number] Replace the image in the example according to the actual image information[ImageId]and[Mirror version number]Parameters. 4. Select the appropriate mirror warehouse address from ECS When pushing an image, you can choose to use the intranet address of the image warehouse. The push speed will be improved and your public network traffic will not be lost. If you are using a machine located in VPC Network, please use registry-vpc.cn-hangzhou.aliyuncs.com As Registry Domain name login. 5. Examples use"docker tag"Command renames the image and pushes it to via the VPC address Registry. $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE registry.aliyuncs.com/acs/agent 0.7-dfb6816 37bb9c63c8b2 7 days ago 37.89 MB $ docker tag 37bb9c63c8b2 registry-vpc.cn-hangzhou.aliyuncs.com/acs/agent:0.7-dfb6816 use "docker push" The command pushes the mirror to the remote. $ docker push registry-vpc.cn-hangzhou.aliyuncs.com/acs/agent:0.7-dfb6816
[the external chain image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-cfasz3ww-1644912741968) (C: \ users \ CHENY \ appdata \ roaming \ typora \ user images \ image-20211025105022273. PNG)]
Docker network
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ze7gx56z-1644912741969) (C: \ users \ CHENY \ appdata \ roaming \ typora \ user images \ image-20211025110701767. PNG)]
[root@alichen /]# docker exec -it tomcat1 ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 62: eth0@if63: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever eth0@if63 yes docker Allocated sure ping through
[the external chain image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-kmiyfufr-1644912741970) (C: \ users \ CHENY \ appdata \ roaming \ typora \ user images \ image-20211025111241985. PNG)]
principle
Every time we start a container, we will assign an ip address. The assigned ip is a pair, a pair of virtual interfaces, a protocol, a communication, and evth pair is a bridge.
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-rcqf3kbx-1644912741970) (C: \ users \ CHENY \ appdata \ roaming \ typora \ user images \ image-20211025112715711. PNG)]
[the external chain image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-jtwhjk0s-1644912741971) (C: \ users \ CHENY \ appdata \ roaming \ typora \ typora user images \ image-20211025113203197. PNG)]
Create your own network
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet [root@alichen /]# docker run -d -P --name tomcat1 --net mynet tomcat bade0da55afbd075f14f289f30aea90626fcb1a0cf169e97baa550eaa9591931 [root@alichen /]# docker run -d -P --name tomcat2 --net mynet tomcat 8162a79a80472767f06c8077c363ef6334830801d5d63a868f6d41150e7b2148 [root@alichen /]# docker exec -it tomcat1 ping tomcat2 PING tomcat2 (192.168.0.3) 56(84) bytes of data. 64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.081 ms 64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.075 ms 64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.074 ms 64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=4 ttl=64 time=0.075 ms 64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=5 ttl=64 time=0.076 ms 64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=6 ttl=64 time=0.077 ms 64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=7 ttl=64 time=0.073 ms 64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=8 ttl=64 time=0.070 ms ^C --- tomcat2 ping statistics ---
Create your own network and connect all containers to each other. It is easier to use than docker0. It has helped us maintain the corresponding relationship. It is recommended that we use this network at ordinary times,
Benefits:
redis: different clusters use different networks to ensure that the cluster is safe and healthy
mysql: different clusters use different networks to ensure that the cluster is safe and healthy
[the external chain image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-8980hiqg-1644912741971) (C: \ users \ CHENY \ appdata \ roaming \ typora \ user images \ image-20211025154639946. PNG)]
Cross network connectivity requires connect command docker network connect mynet tomcat01 docker exec -it tomcat01 ping tomcat1
Actual deployment of redis
.075 ms
64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.074 ms
64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=4 ttl=64 time=0.075 ms
64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=5 ttl=64 time=0.076 ms
64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=6 ttl=64 time=0.077 ms
64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=7 ttl=64 time=0.073 ms
64 bytes from tomcat2.mynet (192.168.0.3): icmp_seq=8 ttl=64 time=0.070 ms
^C
— tomcat2 ping statistics —
Create your own network and connect all containers to each other, which is better than docker0 Easy to use. It has helped us maintain the corresponding relationship. It is recommended that we use such a network at ordinary times, ### Benefits: redis:Different clusters use different networks to ensure that the cluster is safe and healthy mysql: Different clusters use different networks to ensure that the cluster is safe and healthy [External chain picture transfer...(img-8980Hiqg-1644912741971)] ```shell Cross network connectivity requires connect command docker network connect mynet tomcat01 docker exec -it tomcat01 ping tomcat1