Docker Compose一键部署Nginx反向代理Tomcat集群
目录结构如下
mysql/conf/my.cnf
1 2 3 4 5 6 7 8 9 | [mysqld] user = mysql port = 3306 datadir = / var / lib / mysql socket = / var / lib / mysql / mysql.sock pid - file = / var / run / mysql / mysql.pid log_error = / var / log / mysql / error.log character_set_server = utf8 max_connections = 3600 |
nginx/Dockerfile
1 2 3 4 5 6 7 8 9 10 | FROM centos: 6 MAINTAINER lizhenliang RUN yum install - y gcc gcc - c + + make openssl - devel pcre - devel ADD nginx - 1.12 . 1.tar .gz / tmp RUN cd / tmp / nginx - 1.12 . 1 && . / configure - - prefix = / usr / local / nginx && make - j 2 && make install RUN rm - f / usr / local / nginx / conf / nginx.conf COPY nginx.conf / usr / local / nginx / conf EXPOSE 80 CMD [ "/usr/local/nginx/sbin/nginx" , "-g" , "daemon off;" ] |
nginx/nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | user root; worker_processes auto; error_log logs / error.log info; pid logs / nginx.pid; events { use epoll; } http { include 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 logs / access.log main; sendfile on; keepalive_timeout 65 ; upstream www.example.com { #ip_hash; server tomcat01: 8080 ; server tomcat02: 8080 ; server tomcat03: 8080 ; } server { listen 80 ; server_name localhost; location / { proxy_pass http: / / www.example.com; } location ~ \.(html|css|js|jpg|png|gif)$ { root / opt / webapps / ROOT; } } } |
tomcat/Dockerfile
1 2 3 4 5 6 7 8 9 | FROM centos: 6 MAINTAINER lizhenliang ADD jdk - 8u45 - linux - x64.tar.gz / usr / local ENV JAVA_HOME / usr / local / jdk1. 8.0_45 ADD apache - tomcat - 8.0 . 46.tar .gz / usr / local COPY server.xml / usr / local / apache - tomcat - 8.0 . 46 / conf EXPOSE 8080 ENTRYPOINT [ "/usr/local/apache-tomcat-8.0.46/bin/catalina.sh" , "run" ] |
docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | version: '3' services: nginx: hostname: nginx build: context: . / nginx dockerfile: Dockerfile ports: - 80 : 80 links: - tomcat01:tomcat01 - tomcat02:tomcat02 - tomcat03:tomcat03 volumes: - . / webapps: / opt / webapps depends_on: - mysql - tomcat01 - tomcat02 - tomcat03 tomcat01: hostname: tomcat01 build: . / tomcat links: - mysql:mysql - db volumes: - . / webapps: / usr / local / apache - tomcat - 8.0 . 46 / webapps tomcat02: hostname: tomcat02 build: . / tomcat links: - mysql:mysql - db volumes: - . / webapps: / usr / local / apache - tomcat - 8.0 . 46 / webapps tomcat03: hostname: tomcat03 build: . / tomcat links: - mysql:mysql - db volumes: - . / webapps: / usr / local / apache - tomcat - 8.0 . 46 / webapps mysql: hostname: mysql image: mysql: 5.6 ports: - 3306 : 3306 volumes: - . / mysql / conf: / etc / mysql / conf.d - . / mysql / data: / var / lib / mysql environment: MYSQL_ROOT_PASSWORD: 123456 MYSQL_DATABASE: db MYSQL_USER: user MYSQL_PASSWORD: user123 |
构建在后台运行
1 | docker - compose up - d |
webapps的ROOT目录放一个jsp文档index.jsp
启动后访问测试
其实后端是三台tomcat提供服务
进入nginx容器修改nginx配置文件
1 | docker exec - it compose_nginx_tomcat_nginx_1 bash |
添加内容显示后端代理的地址
PS:修改后需要退出容器使用docker-compose restart重启才能使配置生效,在容器内重启nginx不生效
刷新即可看到负载均衡
停止运行的容器
1 | docker - compose stop |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2017-12-21 Python全栈day10(基本数据类型及其常用方法)
2017-12-21 Python全栈day10(运算符)
2017-12-21 Rsync文件同步机备份工具使用