Docker多個(gè)容器不能有相同端口號(hào)的處理方案
在docker里用同一份鏡像創(chuàng)建4個(gè)容器,網(wǎng)絡(luò)選擇bridge模式,A服務(wù)在四個(gè)容器中都使用了同一個(gè)端口號(hào)(6000);為了減少對(duì)外暴露的端口數(shù),另外使用了nginx對(duì)這4個(gè)服務(wù)實(shí)例做代理,4個(gè)服務(wù)實(shí)例分屬4個(gè)upstream,使用了類似/service1、/service2的路徑來(lái)訪問(wèn)4個(gè)實(shí)例。
此時(shí)從本地訪問(wèn)任一服務(wù),則會(huì)報(bào)502錯(cuò)誤,百思不得其解。
connect() failed (111: Connection refused) while connecting to upstream
compose文件
version: ’2’networks: nn: driver: bridgeservices: service-1: container_name: service-1 image: foo networks: - nn volumes: - ./logs/1:/apps/aaa/bbb-logs - ./common:/apps/aaa/bbb - ./xxx/1.xml:/ccc/targets.xml entrypoint: foo.sh command: start app=foo port=6000 service-2: container_name: service-2 image: foo networks: - nn volumes: - ./logs/2:/apps/aaa/bbb-logs - ./common:/apps/aaa/bbb - ./xxx/2.xml:/ccc/targets.xml entrypoint: foo.sh command: start app=foo port=6000 service-3: container_name: service-3 image: foo networks: - nn volumes: - ./logs/3:/apps/aaa/bbb-logs - ./common:/apps/aaa/bbb - ./xxx/3.xml:/ccc/targets.xml entrypoint: foo.sh command: start app=foo port=6000 service-4: container_name: service-4 image: foo networks: - nn volumes: - ./logs/4:/apps/aaa/bbb-logs - ./common:/apps/aaa/bbb - ./xxx/4.xml:/ccc/targets.xml entrypoint: foo.sh command: start app=foo port=6000 nginx: container_name: nginx image: nginx:1.15-alpine ports: - 6001:6001 networks: - nn volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf - ./logs/nginx:/var/log/nginx
nginx.conf
worker_processes 8;worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } http { include mime.types; default_type aplication/octet-stream; sendfile on; log_format main ’[$time_local]$remote_addr-$upstream_addr '$request' $status $body_bytes_sent’; upstream service1.local { server service-1:6000; } upstream service2.local { server service-2:6000; } upstream service3.local { server service-3:6000; } upstream service4.local { server service-4:6000; } server { listen 6001; client_max_body_size 100M; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location /service1/ {proxy_pass http://service1.local/; } location /service2/ {proxy_pass http://service2.local/; } location /service3/ {proxy_pass http://service3.local/; } location /service4/ {proxy_pass http://service4.local/; } location /nginx_status {stub_status on;access_log off; } }}
此時(shí)curl localhost:6001/service1/api/v1/....就會(huì)報(bào)上面的502錯(cuò)誤,按理說(shuō)每一個(gè)容器都是有各自的網(wǎng)卡,不同容器的端口號(hào)應(yīng)該不沖突才對(duì)。
解決方案暫時(shí)沒有較好的方案,只能對(duì)4個(gè)服務(wù)使用不同的端口號(hào),nginx也相應(yīng)地修改。
補(bǔ)充:同臺(tái)服務(wù)器部署多套docker容器,端口重定向問(wèn)題
在生成環(huán)境,部署多個(gè)容器,訪問(wèn)多個(gè)端口;
例如:-p 80:80 -p 81:81
81地址退出的時(shí)候,直接訪問(wèn)到80端口的地址。
誤區(qū):開始以為是cookie的問(wèn)題,因?yàn)樗⑿铝薱ookie(cookie是不區(qū)分端口號(hào))
最終找到了原因:redirect問(wèn)題,因?yàn)橥顺鲋囟ㄏ虻降卿涰?yè)面
解決方案:配置nginx參數(shù)proxy_set_header HOST $host; 改成 proxy_set_header HOST $host:81;
因?yàn)椴还茉鯓樱?qǐng)求參數(shù)投都帶上了端口號(hào)。
網(wǎng)上還有一種方法:修改proxy_redirect 參數(shù)(不過(guò)試過(guò)沒有生效)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. Python sorted排序方法如何實(shí)現(xiàn)2. Python基于requests實(shí)現(xiàn)模擬上傳文件3. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車4. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題5. python利用opencv實(shí)現(xiàn)顏色檢測(cè)6. Python文本文件的合并操作方法代碼實(shí)例7. Python 中如何使用 virtualenv 管理虛擬環(huán)境8. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效9. asp讀取xml文件和記數(shù)10. Python獲取B站粉絲數(shù)的示例代碼
