採用ubuntu系統,docker安裝自行百度html
1.安裝tomcatmysql
docker run -d -p 8088:8080 tomcat docker run -d -p 8089:8080 tomcat
安裝兩個實例,端口分別爲8088,8089nginx
2.安裝nginxsql
docker run -p 8080:80 -d docker.io/nginx
安裝nginx,端口爲8080。在測試的過程當中,發現ubuntu上對80端口有限制,不能直接訪問,因此映射爲8080docker
3.修改nginx配置ubuntu
3.1查看實例tomcat
sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 516b1562b0e3 nginx "nginx -g 'daemon of…" 21 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp musing_dewdney 36e48585253f tomcat "catalina.sh run" About an hour ago Up About an hour 0.0.0.0:8089->8080/tcp keen_wescoff 7eb77e6a33e5 tomcat "catalina.sh run" About an hour ago Up About an hour 0.0.0.0:8088->8080/tcp frosty_grothendieck 2069f54ea331 mysql:5.7 "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:3306->3306/tcp, 33060/tcp mysqltest
3.2進入nginx的bashbash
sudo docker exec -it 516b1562b0e3 bash
3.3修改配置app
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; proxy_buffer_size 128k; proxy_buffers 32 128k; proxy_busy_buffers_size 128k; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; # 注意,這裏的server名字即org.tonny.balance不能帶下劃線,tomcat是上面安裝的tomcat upstream org.tonny.balance { server 192.168.126.128:8088 weight=1; server 192.168.126.128:8089 weight=1; } server { #注意,這裏保留80,由於前面的docker運行命令,會映射80端口 listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # 不帶數據的請求 location / { root html; index index.html index.htm; #訪問映射 proxy_pass http://org.tonny.balance; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
3.4重啓nginxtcp
sudo docker restart 516b1562b0e3
4.測試
經過ip,端口訪問便可