Docker Nginx container and Tomcat container to achieve load balancing and dynamic and static separation operations

Docker Nginx container and Tomcat container to achieve load balancing and dynamic and static separation operations

Download Tomcat8 image

[root@localhost ~]# docker search tomcat8
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ashince/tomcat8 Tomcat GUI Manager pre-configured docker image… 5                    
podbox/tomcat8 2 [OK]
 
 

This tomcat contains jdk and can be accessed directly after it is started. It starts port 8080 by itself.

[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 an 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 "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:8081->8080/tcp 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 

#Copy the configuration in the Nginx container to the local machine for modification, because there is no vim command in the container [root@localhost ~]# docker cp 68d2bdf336ed:/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;
  }
}
 
#IP addresses of two tomcats [root@localhost ~]# for i in {72e174adc77d,080068dae40a};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 dynamic and static separation, and modify the configuration file at the same time #Copy the ROOT directory of one of the Tomcats to the local computer, because Nginx needs to access static resources locally for dynamic and static separation [root@localhost ~]# docker cp 72e174adc77d:/usr/local/tomcat/webapps/ROOT .
 
#Copy to the Nginx release 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 modification, it will be copied to the container [root@localhost ~]# docker cp default.conf 68d2bdf336ed:/etc/nginx/conf.d/default.conf 
[root@localhost ~]# docker exec 68d2bdf336ed 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 items [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 port 80 as shown in the figure:

Additional knowledge: Docker basic operations Container self-start Deleting images and deleting containers

Docker container automatic restart settings

After restarting the operating system, I found that the docker service was not started and the container was not started. How can I start it automatically after restarting?

1. Docker service automatic restart 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?? 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
 

When a host has many images and containers, you can choose to delete some or all of them when you need to reset them. 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:81->80/tcp distracted_grothendieck
[root@localhost ~]# docker ps -aq
27c5c4d3cf86
61cccfe238a8

1) First you need to stop all containers

[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 (if you want to delete only one, just change the variable behind to image id)

[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 image

1) View the image in the host

docker images

2) Delete the image with the specified id

docker rmi <image id>

3) Delete all images

docker rmi $(docker images -q)

3. When the iamges to be deleted are related to other images and cannot be deleted

You can force deletion by using the -f parameter

docker rmi -f $(docker images -q)

The above article about how to use Docker Nginx container and Tomcat container to achieve load balancing and dynamic and static separation is all I have to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Basic configuration example of Nginx with Apache or Tomcat for dynamic and static separation
  • Using Nginx+uWsgi to separate the dynamic and static parts of Python's Django framework site
  • Simple implementation of nginx+tomcat reverse proxy and dynamic and static separation
  • Detailed explanation of nginx to separate static and dynamic tomcat
  • nginx realizes load balancing and dynamic and static separation
  • Nginx sample code for implementing dynamic and static separation
  • Detailed example of deploying Nginx+Apache dynamic and static separation
  • Sample code for nginx to achieve dynamic and static separation
  • Nginx implements dynamic and static separation example explanation
  • Nginx dynamic and static separation implementation case code analysis
  • Detailed explanation of the process of realizing dynamic and static separation in Springmvc nginx
  • Analysis of the principle of Nginx+Tomcat to achieve load balancing and dynamic and static separation
  • The principle and configuration of Nginx load balancing and dynamic and static separation
  • Example of how nginx implements dynamic and static separation
  • Detailed instructions for nginx from installation to configuration (installation, security configuration, anti-hotlinking, dynamic and static separation, HTTPS configuration, performance optimization)
  • Implementation of Nginx+Tomcat load balancing and dynamic and static separation cluster
  • Server load balancing nginx+tomcat to achieve dynamic and static separation
  • Nginx dynamic and static separation configuration implementation and description

<<:  Enter two numbers in html to realize addition, subtraction, multiplication and division functions

>>:  In-depth explanation of the various methods and advantages and disadvantages of JavaScript inheritance

Recommend

How to deploy services in Windows Server 2016 (Graphic Tutorial)

introduction Sometimes, if there are a large numb...

Deleting files with spaces in Linux (not directories)

In our daily work, we often come into contact wit...

Count the list tags in HTML

1. <dl> defines a list, <dt> defines ...

JavaScript implements div mouse drag effect

This article shares the specific code for JavaScr...

How to clear the timer elegantly in Vue

Table of contents Preface optimization Derivative...

How to implement online hot migration of KVM virtual machines (picture and text)

1. KVM virtual machine migration method and issue...

How to underline the a tag and change the color before and after clicking

Copy code The code is as follows: a:link { font-s...

Docker data management and network communication usage

You can install Docker and perform simple operati...

Pycharm2017 realizes the connection between python3.6 and mysql

This article shares with you how to connect pytho...

Tutorial on installing PHP on centos via yum

First, let me introduce how to install PHP on Cen...

Detailed explanation of how to use Tomcat Native to improve Tomcat IO efficiency

Table of contents Introduction How to connect to ...

Linux five-step build kernel tree

Table of contents 0. The kernel tree that comes w...

How to change the host name in Linux

1. View the current host name [root@fangjian ~]# ...