Docker Nginx containers and Tomcat containers implement load balancing and static separation operations
Download the Tomcat8 image
[root@localhost ~]# docker search tomcat8 NAME DESCRIPTION STARS OFFICIAL AUTOMATED ashince/tomcat8 Tomcat GUI Manager pre-configured docker ima… 5 podbox/tomcat8 2 [OK]
This tomcat includes the jdk and enables direct access, starting port 8080 on its own
[root@localhost ~]# docker pull ashince/tomcat8 Using default tag: latest latest: Pulling from ashince/tomcat8 06b22ddb1913: Pulling fs layer 336c28b408ed: Pull complete 1f3e6b8d80c3: Pull complete [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 2622e6cca7eb 2 weeks ago 132MB ashince/tomcat8 latest 02aedead27dd 22 months ago 314MB Start one Nginx container and two Tomcat containers [root@localhost ~]# docker run -itd -p 8080:8080 ashince/tomcat8 3e3f2aabe67de7ee3f4b6d62176e21aaa9d2302922845cb08ad37af7146b13c5 [root@localhost ~]# docker run -itd -p 8081:8080 ashince/tomcat8 644d59711c805a626b7c1c219aa018f744098a14dd41e54744d6b13e7ba66a2f [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cca55c4ad919 ashince/tomcat8 sh run" About a minute ago Up About a minute 0.0.0.0:8081->8080/tcp unruffled_lalande 08b58d2f41d6. Unruffled_lalande 08B58d2f41d6 ashince/tomcat8 "catalina.sh run" 7 minutes ago Up 7 minutes 0.0.0.0:8080->8080/tcp relaxed_williamson aeebcb0b40a2 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp priceless_ardinghelli. 2 hours ago up 2 hours 0.0.0.0:80->80/ TCP Priceless_ardinghelli
# Copy the configuration in the Nginx container to local modification, Because the container not vi vim command/root @ localhost ~ # docker cp 68 d2bdf336ed: / etc/nginx/conf. D/default. Conf., / root @ localhost ~ # ls anaconda-ks.cfg default.conf index.html [root@localhost ~]# grep -vE "#|^$" default.conf server { listen 80; listen [::]:80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }} # the IP address of the two tomcat/root @ localhost ~ # for the I in {72 e174adc77d, 080068 dae40a}; do docker inspect $i| grep -i ipaddr |tail -n 1; done "IPAddress": "172.17.0.4", "IPAddress": 172.17.0.3, copy static resources to the Nginx directory for separation, and modify the configuration file # to copy the ROOT directory of one Tomcat to a local directory. Because of dynamic and static separation Nginx need access to a static resource to the local/root @ localhost ~ # docker cp 72 e174adc77d: / usr/local/tomcat/webapps/root. # copy to Nginx released under the directory [root@localhost ~]# docker cp ROOT 68d2bdf336ed:/usr/share/nginx/html/ [root@localhost WEB-INF]# docker exec 68d2bdf336ed ls -l /usr/share/nginx/html/ROOT total 184 -rwxrwxrwx 1 root root 7064 Jun 21 2017 RELEASE-NOTES.txt drwxrwxrwx 2 root root 21 Jul 27 2017 WEB-INF -rwxrwxrwx 1 root root 26447 Jun 21 2017 asf-logo-wide.svg -rwxrwxrwx 1 root root 713 Jun 21 2017 bg-button.png -rwxrwxrwx 1 root root 1918 Jun 21 2017 bg-middle.png -rwxrwxrwx 1 root root 1392 Jun 21 2017 bg-nav-item.png -rwxrwxrwx 1 root root 1401 Jun 21 2017 bg-nav.png -rwxrwxrwx 1 root root 3103 Jun 21 2017 bg-upper.png -rwxrwxrwx 1 root root 21630 Jun 21 2017 favicon.ico -rwxrwxrwx 1 root root 12279 Jun 21 2017 index.jsp -rwxrwxrwx 1 root root 2376 Jun 21 2017 tomcat-power.gif -rwxrwxrwx 1 root root 5581 Jun 21 2017 tomcat.css -rwxrwxrwx 1 root root 2066 Jun 21 2017 tomcat.gif -rwxrwxrwx 1 root root 5103 Jun 21 2017 tomcat.png -rwxrwxrwx 1 root root 67795 Jun 21 2017 tomcat.svg # The modified Nginx configuration file is as follows: After the modification will copy the container/root @ localhost ~ # docker cp default. Conf 68 d2bdf336ed: / etc/nginx/conf. D/default. The conf/root @ localhost ~ # Docker exec 68 d2bdf336ed cat/etc/nginx/conf. D/default. Conf upstream tomcat_web {server 172.17.0.3:8080 weight = 100 max_fails=2 fail_timeout=15; server 172.17.0.4:8080 weight=100 max_fails=2 fail_timeout=15; } server { listen 80; listen [::]:80; server_name localhost; error_page 500 502 503 504 /50x.html; root /usr/share/nginx/html; location /{ proxy_pass http://tomcat_web; proxy_set_header host $host; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { root /usr/share/nginx/html/ROOT; expires 30d; access_log off; } location ~ .*\.(eot|ttf|otf|woff|svg)$ { root /usr/share/nginx/html/ROOT; expires 30d; access_log off; } location ~ .*\.(js|css)$ { root /usr/share/nginx/html/ROOT; expires 30d; access_log off; } location = /50x.html { root /usr/share/nginx/html; # Load new configuration item [root@localhost ~]# docker exec -it 68d2bdf336ed /bin/bash root@68d2bdf336ed:/# /usr/sbin/nginx -s reload 2020/06/29 07:12:05 [notice] 79#79: signal process started
Then access Nginx 80 port as shown in the figure:
Additional knowledge: Docker basic operation container self-startup Delete image and delete container
The Docker container automatically restarts Settings
After the reboot operating system is restarted, the docker service is not started, and the container is not started. How can the Docker service be automatically started after the restart
1. docker service automatically restarts Settings
[root@localhost ~]# systemctl enable docker.service
2, docker container automatic startup Settings
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 5a3221f0137b 10 months ago 126MB [root@localhost ~]# docker run -itd -p 80:80 nginx 3e28c4b5c6256c0ba04666751e426987d848b7afeb9c59774d5e9831dc78e5ee [root@localhost ~]# docker run -itd -p 81:80 nginx f0597c725fd6b7f4229aa9ab5de4a3cb29d09097a81dc8f64d1a60d469001379 [root@localhost ~]# docker port f0597c725fd6 80/tcp -> 0.0.0.0:81 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f0597c725fd6 nginx "nginx -g 'daemon of?? 30 seconds ago Up 29 seconds 0.0.0.0:81->80/tcp elastic_allen 3e28c4b5c625 nginx "nginx -g 'daemon of?? 30 seconds ago up 29 seconds 0.0.0.0:81->80/ TCP elastic_Allen 3e28C4B5C625 nginx "nginx -g 'daemon of?? 35 seconds ago Up 33 seconds 0.0.0.0:80->80/tcp tender_volhard. 35 seconds ago up 33 seconds 0.0.0.0:80->80/ TCP tender_volhard
3, docker container automatic startup Settings
[root@localhost ~]# docker update --restart=always f0597c725fd6 3e28c4b5c625 f0597c725fd6 3e28c4b5c625 [root@localhost ~]# reboot -h now Connection closed by foreign host. [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f0597c725fd6 nginx "nginx -g 'daemon of?? 13 minutes ago Up 2 minutes 0.0.0.0:81->80/tcp elastic_allen 3e28c4b5c625 nginx "nginx -g 'daemon of?? 13 minutes ago Up 2 minutes. 0.0.0.0:80->80/tcp tender_volhard
If a host has a large number of mirrors and containers, you can delete some or all of them when resetting the host. Then you need to do the following.
1. Delete the container
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 27c5c4d3cf86 nginx "nginx -g 'daemon of?? 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp gracious_nash 61cccfe238a8 nginx "nginx -g 'daemon of?? 2 minutes ago up 2 minutes 0.0.0.0:80->80/ TCP Gracious_NASH 61CCCFe238A8 nginx "nginx -g 'daemon of?? 2 minutes ago Up 2 minutes 0.0.0.0:81->80/tcp distracted_grothendieck [root@localhost ~]# docker ps-aq 27c5c4d3cf86. 2 minutes ago up 2 minutes 0.0.0.0:81->80/ TCP distracted_grothendieck [root@localhost ~]# docker ps-aq 27c5c4d3CF86 61cccfe238a8
1) Stop all containers first
[root@localhost ~]# docker ps -aq f0597c725fd6 3e28c4b5c625 8855c7777f83 466d2efe3dd9 20ca589b1a10 e5457b41cae6 314d1d01c941 [root@localhost ~]# docker stop $(docker ps -aq) f0597c725fd6 3e28c4b5c625 8855c7777f83 466d2efe3dd9 20ca589b1a10 e5457b41cae6 314d1d01c941 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2) Delete all containers (change the following variable to image id if only one container is deleted)
[root@localhost ~]# docker rm -f $(docker ps -aq) f0597c725fd6 3e28c4b5c625 8855c7777f83 466d2efe3dd9 20ca589b1a10 e5457b41cae6 314d1d01c941 [root@localhost ~]# docker ps -aq [root@localhost ~]#
2. Delete the mirror
1) View the mirror in host
docker images
2) Delete the mirror with the specified id
docker rmi <image id>
3) Delete all images
docker rmi $(docker images -q)
3, when the iamges to be deleted is associated with other mirrors and cannot be deleted
You can run the -f parameter to forcibly delete the command
docker rmi -f $(docker images -q)
The above article Docker Nginx container and Tomcat container to achieve load balancing and static separation operation is all the content shared by Xiaobian. I hope to give you a reference, and I hope you can support the script home.
- docker installs nginx to balance the load of springboot projects
- How does the docker swam cluster achieve load balancing
- Docker installs Nacos containers and implements load balancing according to Nginx
- docker builds nginx to implement load balancing example code
- Summary of problems in deploying Tomcat cluster and Nginx load balancing based on Docker
- docker swarm externally validates a solution that does not work when load balancing
- Method steps for implementing nginx load balancing using Docker Compose
- Explain Docker Swarm service discovery and load balancing principles in detail
- Using nginx and docker to achieve a simple load balancing
- Docker Deploys tenine for high Availability and load Balancing of back-end applications (Recommended)
Related article
-
ELK teamed up with Grafana to create visual monitoring to analyze nginx logs
This article mainly introduces the visual monitoring jointly created by ELK and Grafana to analyze nginx logs. If you need it, you can use it for reference. I hope it can be helpful2022-03-03
-
This section describes the usage of nginx command parameters
This article mainly introduces the nginx command parameter usage details of the relevant information, I hope that through this article can help you understand and apply this part of the knowledge, the need of friends can refer to2017-08-08 -
Method steps for Nginx to deploy multiple vue projects
This article mainly introduces the methods and steps of Nginx deployment of multiple vue projects, the article introduces very detailed through the example code, which has certain reference learning value for everyone's study or work, and the friends who need to study together with the small series below2023-06-06 -
Detail nginx configuration location summary and rewrite rule writing
This article mainly introduces a detailed explanation of nginx configuration location summary and rewrite rules writing, Xiaobian feel very good, now share with you, but also give you a reference. Let's take a look2018-02-02 -
Nginx server restart Closes the reload command
This article mainly introduces the Nginx server restart close reload command, very good, with reference value, need friends can refer to the next2017-03-03 -
Share Nginx next 10 security tips
Nginx is one of the most popular Web servers available today. It serves 7% of the world's web traffic and is growing at an alarming rate. It's an amazing server, and I'd love to deploy it2014-04-04 -
nginx configures the client to save the implementation of cookies
This article mainly introduces the implementation of nginx configuration client to save cookies, the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, the need for friends to learn together with the small series2023-03-03 -
Interpret the limit configuration parameter in nginx
This article mainly introduces the detailed function of the limit configuration parameter in nginx, I hope the content we collate can help you, learn it together.2018-01-01 -
nginx configuration to solve cross-domain access methods detailed
This article mainly introduces in detail how to configure nginx to solve the cross-domain access method, the example code in the article explains in detail, has a certain reference value, interested partners can follow Xiaobian together to learn2023-10-10 -
bug records of proxy_pass reverse proxy 502 in nginx configuration
This article mainly introduces the bug record of proxy_pass reverse proxy 502 in nginx configuration, which has a good reference value. I hope to help you. If there are mistakes or places that are not considered completely, please feel free to advise2024-01-01
Latest comments