docker-compose.yml
version: "3"
#虚拟网络配置,分配虚拟IP地址,当前配置使容器之间组成局域网(与driver的选择有关)
networks:
my-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 192.168.0.0/24 #设置子网地址空间(容器IP范围)
services:
nginx:
image: nginx
container_name: "mynginx"
networks:
my-net:
ipv4_address: 192.168.0.2 #指定nginx容器IP地址
ports:
- "80:80" #对主机80端口的访问会映射到容器的80端口
volumes:
- /usr/local/docker/docker-compose/nginx/nginx.conf:/etc/nginx/nginx.conf
- /usr/local/docker/docker-compose/nginx/log:/var/log/nginx
- /usr/local/docker/docker-compose/nginx/html:/usr/share/nginx/html
#tomcat启动完成再启动nginx
depends_on:
- tomcat-1
- tomcat-2
- tomcat-3
tomcat-1:
image: tomcat
container_name: "mytomcat-1"
networks:
my-net:
ipv4_address: 192.168.0.3
ports:
- "8081:8080"
restart: "always"
volumes:
- /usr/local/docker/docker-compose/tomcat-cluster/tomcat-1/conf:/usr/local/tomcat/conf
- /usr/local/docker/docker-compose/tomcat-cluster/tomcat-1/logs:/usr/local/tomcat/logs
- /usr/local/docker/docker-compose/tomcat-cluster/tomcat-1/webapps:/usr/local/tomcat/webapps
tomcat-2:
image: tomcat
container_name: "mytomcat-2"
networks:
my-net:
ipv4_address: 192.168.0.4
ports:
- "8082:8080"
restart: "always"
volumes:
- /usr/local/docker/docker-compose/tomcat-cluster/tomcat-2/conf:/usr/local/tomcat/conf
- /usr/local/docker/docker-compose/tomcat-cluster/tomcat-2/logs:/usr/local/tomcat/logs
- /usr/local/docker/docker-compose/tomcat-cluster/tomcat-2/webapps:/usr/local/tomcat/webapps
tomcat-3:
image: tomcat
container_name: "mytomcat-3"
networks:
my-net:
ipv4_address: 192.168.0.5
ports:
- "8083:8080"
restart: "always"
volumes:
- /usr/local/docker/docker-compose/tomcat-cluster/tomcat-3/conf:/usr/local/tomcat/conf
- /usr/local/docker/docker-compose/tomcat-cluster/tomcat-3/logs:/usr/local/tomcat/logs
- /usr/local/docker/docker-compose/tomcat-cluster/tomcat-3/webapps:/usr/local/tomcat/webapps
Nginx配置文件nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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;
keepalive_timeout 65;
sendfile on;
# 需要注意的是如果tomcat都部署在docker内,这里的ip地址填对应docker容器的ip和端口号
# 注意ip和端口的对应,这里容易把端口号写成宿主机开放的端口号
upstream tomcat-cluster {
server 192.168.0.3:8080;
server 192.168.0.4:8080;
server 192.168.0.5:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcat-cluster;
}
# redirect server error pages to the static page.
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
来源:CSDN
作者:小伙子什么车
链接:https://blog.csdn.net/qq_40507817/article/details/104300667