Home > Back-end >  I can't communicate between 2 dockers that connected to the same network
I can't communicate between 2 dockers that connected to the same network

Time:01-28

I have 2 dockers, one is the client and the other is the server.

After deployment, I can't reach the server from the client.

Here is my docker-compose file:

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

here is how I try to communicate from the client:

python
import reuests
token = requests.get('http://log_server:4000/login', auth=('clientLog', 'Bbn4uazkVvfyHxhcbrhk2MeAE9yPh4r4nRfHWv'))

This is the error:

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

What I do rung? They are both on the same network...

CodePudding user response:

It looks as if your log server is listening port 80, but you're trying to connect on port 4000.

Remember that port mappings don't affect container-to-container communication; they only affect on which host ports a service is exposed. Since you're connecting directly to the log_server container you should just be using port 80.

CodePudding user response:

When you do the port mapping in the log_server service you are opening a host port to have access to the mapped port inside the Docker container. From your machine(which is the host) you should be able to access the log server via localhost:4000.

Port mapping is about container/host, but log_server and client are two containers running on the same network. They have visibility of each other via docker internal DNS so it is correct to try to make a request to log_server, the problem is that you are trying to reach port 4000. Doing requests.get('http://log_server:80/login', or requests.get('http://log_server/login', should solve the issue.

  •  Tags:  
  • Related