skip to Main Content

I know this looks duplicated, but I checked all the others questions and none of them solved my problem.

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
    links:
      - "serviceDiscovery"
      - "messageBroker"
        
        
        
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, I’m on this problem 3 days alredy and I’m going crazy. I checked tutorials, followed the steps and nothig.

3

Answers


  1. Chosen as BEST ANSWER

    Solved, guys! Well, the configuration was correct. However, whenever I created a new container, it didn't build. Therefore, even if I changed the code, it still had the image of the first version of the code, with the "localhost" as the hostname. So the only thing I did was delete the image of the service and Docker created a new one with the correct code. I see this obviously as a temporary solution, since it has to build everytime it runs/a new container is created. But this is another subject and I think it won't be hard to implement. Maybe with the arg --build in docker compose is enough. But I will only give attention to this later.


  2. Your docker-compose file does not have the networking setup correctly. IMO you don’t need the links. Here is a minimal docker-compose that worked for me. I removed the links and I removed the service discovery which isn’t in play here for connectivity between rabbitClient and the broker.

    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"
    
      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
        build:
          context: client
          dockerfile: Dockerfile
            
    networks:
      services-network:
    
    Login or Signup to reply.
  3. On the docker-compose, in the network is missing something, put it like this:

    networks:
        services-network:
            driver: bridge
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search