Create 3 Tomcat containers, map the ports to 8080, 8081, and 8082 respectively, use the data volume to mount them, and add /opt/module/docker/tomcat3/ROOT1/ and /opt/module/docker/tomcat3/ in the host directory respectively. ROOT2/, /opt/module/docker/tomcat3/ROOT2/ is mounted to the webapps directory under the tomcat directory inside the container. The benefits of mounting are as follows:
- Data sharing: The same data can be mounted to multiple containers
- The container is deleted but the data is still there, which is safer.
docker run -d --name tomcat-8080 -p8080:8080 -v /opt/module/docker/tomcat3/ROOT1/:/usr/local/tomcat/webapps/ tomcat
docker run -d --name tomcat-8081 -p8081:8080 -v /opt/module/docker/tomcat3/ROOT2/:/usr/local/tomcat/webapps/ tomcat
docker run -d --name tomcat-8082 -p8082:8080 -v /opt/module/docker/tomcat3/ROOT3/:/usr/local/tomcat/webapps/ tomcat
Access three ports respectively
- docker starts nginx
docker run -d --name nginx-8000 -p 8000:80 ngxin
access
Enter the nginx container and find the Nginx configuration file
Use the cp command to copy the nginx configuration file in the container to the host
docker cp 9s2:/etc/nginx/nginx.conf /opt/module/docker/nginx/
Modify the configuration file and perform load balancing
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream backend {
server 192.168.240.131:8080;
server 192.168.240.131:8081;
server 192.168.240.131:8082;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
#include /etc/nginx/conf.d/*.conf;
}
At this time, restart nginx and mount the ngxin configuration file to the host machine.
docker run -d --name ngxin-8000 -p 8000:80 -v /opt/module/docker/nginx/nginx.conf:/etc/nginx/nginx.conf nginx
Since the port of nginx is mapped to 8000 of the host, accessing 8000 at this time is equivalent to accessing 3 tomcats in load balancing.
Docker creates a container and starts it in seconds. It also supports mounting, port mapping, etc. Through this, you can also discover the power of Docker, especially in building load balancing, rapid expansion and contraction, etc.