我有 2 個 docker,一個是客戶端,另一個是服務器。
部署后,我無法從客戶端訪問服務器。
這是我的 docker-compose 檔案:
version: "3"
services:
# should be locate once in the control node
log_server:
build: ./logServer/
restart: unless-stopped
ports:
- "4000:80"
networks:
- logging-net
volumes:
- persistLogs:/var/log/files
- sharedError:/var/errors
# We should have 1 on EVERY node and configure it to
# collect logs from all containers in the node
client:
depends_on:
- log_server
build: ./logClient/
restart: unless-stopped
environment:
- LOG_SERVER=log_server
networks:
- logging-net
volumes:
- nodeWideLogs:/var/log/files
- sharedError:/var/errors
deploy:
mode: global
networks:
logging-net:
external: true
volumes:
sharedError:
driver: local
persistLogs:
driver: local
nodeWideLogs:
driver: local
這是我嘗試與客戶溝通的方式:
python
import reuests
token = requests.get('http://log_server:4000/login', auth=('clientLog', 'Bbn4uazkVvfyHxhcbrhk2MeAE9yPh4r4nRfHWv'))
這是錯誤:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='log_server', port=4000): Max retries exceeded with url: /login (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0c2f470220>: Failed to establish a new connection: [Errno 111] Connection refused'))
我做什么?他們都在同一個網路上...
uj5u.com熱心網友回復:
看起來您的日志服務器正在偵聽埠 80,但您正在嘗試連接埠 4000。
請記住,埠映射不會影響容器到容器的通信;它們只影響服務暴露在哪些主機埠上。由于您直接連接到log_server容器,因此您應該只使用埠 80。
uj5u.com熱心網友回復:
當您在log_server服務中進行埠映射時,您將打開一個主機埠以訪問 Docker 容器內的映射埠。從您的機器(即主機)上,您應該能夠通過localhost:4000.
埠映射是關于容器/主機的,但是是在log_server同client一個網路上運行的兩個容器。他們通過 docker internal DNS 可以看到彼此,因此嘗試向 發出請求是正確的log_server,問題是您正在嘗試訪問埠 4000。這樣做requests.get('http://log_server:80/login',或requests.get('http://log_server/login',應該解決問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/423121.html
標籤:
