Skip to content

Connection Failed / Connection Refused #1242

@Ricardev

Description

@Ricardev

So, this is my docker-compose yml file:

version: '3.8'

services:
  #db:
   # image: postgres

  messageBroker:
    image: rabbitmq:management
    hostname: "messageBroker"
    healthcheck:
      test: rabbitmq-diagnostics -q ping
      interval: 5s
      timeout: 15s
      retries: 3
    networks:
      - services-network
    ports:
      - "15672:15672"
      - "5672:5672"
    environment:
      RABBITMQ_DEFAULT_USER: "admin"
      RABBITMQ_DEFAULT_PASS: "password"

  serviceDiscovery:
    image: steeltoeoss/eureka-server
    hostname: eureka-server
    networks:
      - services-network
    ports:
      - "8761:8761"
    
  order-api:
    image: ${DOCKER_REGISTRY-}orderapi
    hostname: orderapi
    environment:
      - Eureka__Client__ServiceUrl=http://serviceDiscovery:8761/eureka/
      - Eureka__Client__ShouldRegisterWithEureka=true
      - Eureka__Client__ValidateCertificates=false
    networks:
      - services-network
    depends_on:
      - serviceDiscovery
    build:
      context: .
      dockerfile: Services/Order/Dockerfile
    links:
      - "serviceDiscovery"

  product-api:
    image: ${DOCKER_REGISTRY-}productapi
    hostname: productapi
    restart: on-failure
    environment:
      - Eureka__Client__ServiceUrl=http://serviceDiscovery:8761/eureka/
      - Eureka__Client__ShouldRegisterWithEureka=true
      - Eureka__Client__ValidateCertificates=false
    networks:
      - services-network
    depends_on:
      messageBroker:
        condition: service_healthy
      serviceDiscovery:
        condition: service_started
    build:
      context: .
      dockerfile: Services/Products/Dockerfile
        
        
        
networks:
  services-network:

this is my config file which I connect to RabbitMq:

using RabbitMQ.Client;

namespace MessageBroker;

public static class MessageBrokerConfig
{
    public static IModel ChannelConfig()
    {
        var channel = new ConnectionFactory { Uri = new Uri("amqp://admin:password@messageBroker:5672") }
            .CreateConnection()
            .CreateModel();
        return channel;
    } 
}

but when I run docker-compose up I still got the error

product-api_1       | Unhandled exception. RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable
product-api_1       |  ---> System.AggregateException: One or more errors occurred. (Connection failed)
product-api_1       |  ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed
product-api_1       |  ---> System.Net.Sockets.SocketException (111): Connection refused

And the product service can register inside the Service Discovery without a problem, and I followed almost the same steps. And I know that the problem isn't the rabbitmq container taking time to be ready, because I can connect on my machine. And everytime the product service failed to launch, it restarts, but no matter how much time it takes, I still got this error. And the log of the messageBroker container shows it's healthy (and if wasn't, I would not be able to access through my machine ). I don't have any other ideas. Also, to test the port connection, I created a container with ubuntu image, and did telnet host-ip port and it was successfull in connect with the container.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions