Search code examples
gonetwork-programmingdocker-composemicroservices

Connect refused between docker containers


Development Environment: MacOS

I'm trying to build a microservices application using golang which requires communication between containers. I have 2 services, service-2 on port 8080 and service-1 running on port 8084.

I'm getting connection refused while calling service-2 from service-1. PFB the error

2024/05/11 10:09:41 An error occured in handler Get "http://service-2:8080/buy": dial tcp 172.22.0.2:8080: connect: connection refused
2024/05/11 10:09:41 http: panic serving 192.168.65.1:17072: runtime error: invalid memory address or nil pointer dereference

PFB docker-compose.yml for reference

name: microservices

services:
  service-2:
    build:
      context: ./service-2
      dockerfile: ./Dockerfile
    ports:
      - "8080:8443"
    restart: always
  service-1:
    build:
      context: ./service-1
      dockerfile: ./Dockerfile
    ports:
      - "8084:8443"
    restart: always

I've searched and trying out different solutions from stackoverflow but none of them worked. Appreciate your help.


Solution

  • The error you're seeing is likely due to the fact that your service-2 container is not listening on port 8080. In your docker-compose.yml file, you have mapped port 8080 of the host machine to port 8443 of the service-2 container. This means that when you try to access service-2 on port 8080, it will actually try to connect to port 8443 inside the container.

    To fix this, you need to make sure that service-2 is listening on port 8443 and not on port 8080. You can do this by changing the port mapping in your docker-compose.yml file or by updating the code in service-2 to listen on port 8443 instead of 8080.