我无法在连接到同一网络的 2 个码头工人之间进行通信

I can't communicate between 2 dockers that connected to the same network

我有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'))

我在做什么? 他们都在同一个网络上...

您的日志服务器似乎正在侦听端口 80,但您正在尝试连接到端口 4000。

请记住,端口映射不会影响 container-to-container 通信;它们只影响服务公开的 host 端口。由于您直接连接到 log_server 容器,因此您应该只使用端口 80。