dotnetcore console app : rabbitmq with docker Connection refused 127.0.0.1:5672
Asked Answered
S

2

7

rabbit connection from console app :

 var factory = new ConnectionFactory()
                {
                    HostName = Environment.GetEnvironmentVariable("RabbitMq/Host"),
                    UserName = Environment.GetEnvironmentVariable("RabbitMq/Username"),
                    Password = Environment.GetEnvironmentVariable("RabbitMq/Password")
                };

                using (var connection = factory.CreateConnection()) // GETTING ERROR HERE
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "rss",
                                         durable: fa...

I'm getting this error :

Unhandled Exception: RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Connection refused 127.0.0.1:5672

my docker-compose.yml file :

version: '3'

services:
  message.api:
    image: message.api 
    build:
      context: ./message_api
      dockerfile: Dockerfile
    container_name: message.api
    environment:
      - "RabbitMq/Host=rabbit"
      - "RabbitMq/Username=guest"
      - "RabbitMq/Password=guest"
    depends_on:
      - rabbit

  rabbit:
    image: rabbitmq:3.7.2-management
    hostname: rabbit
    ports:
      - "15672:15672"
      - "5672:5672"

  rsscomparator:
    image: rsscomparator 
    build:
      context: ./rss_comparator_app
      dockerfile: Dockerfile
    container_name: rsscomparator
    environment:
      - "RabbitMq/Host=rabbit"
      - "RabbitMq/Username=guest"
      - "RabbitMq/Password=guest"
    depends_on:
      - rabbit

I'm using dotnetcore console app. When I use this app in docker I'm getting error. I can reach rabbitmq web browser(http://192.168.99.100:15672) but app can not reach.

Shod answered 20/1, 2018 at 21:38 Comment(2)
Could there be an issue with / in RabbitMq/Host?Abnormality
in app my hostname is rabbit. it means app has to connect rabbit:5672? but. when app in docker, it always trying to connect 127.0.0.1:5672Shod
L
5

You are trying to connect from your container app to your rabbitmq app. You try to achieve this with 127.0.0.1:5672 in your console app container.

But this is pointing to your localhost inside this container, and not to your localhost on your host.

You are deploying your services using the same docker-compose without specifying network settings which means they are all deployed inside the same docker bridge network. This will allow you to let the containers communicate with each other using their container or service names.

So try to connect to rabbit:5672 instead of 127.0.0.1:5672. This name will be translated to the container IP (172.xx.xx.xx) which means you'll create a private connection between your containers.

Larondalarosa answered 21/1, 2018 at 13:56 Comment(5)
in app HostName = Environment.GetEnvironmentVariable("RabbitMq/Host") I'm trying to connect rabbit:5672 but app cannot do that. Only trying to connect 127.0.0.1:5672Shod
am i missing something that has to be done in docker ?Shod
I added network to docker-compose file then error is goneShod
@t.YILMAZ could you provide us your final docker-compose file please ? Or at least explain where did you put the network and what did you put inside. Thanks !Byyourleave
you can reach docker-compose.yml file here github.com/ylmz05/tracker/tree/master/RSSTrackerShod
P
0

I still had the same problem when trying to connect with rabbit:5672 after launching my containers via a docker-compose file. This is not the original issue here, but I got the same "connection refused" error.

I discovered that the rabbitMQ service takes a while to start, so other services (containers) try to connect before it's running. The main reason is that depends_on clause only guarantees that rabbitMQ is started, but not that it is running and healthy.

To fix this, you can set restart: unless-stopped or restart: on-failure to your other services depending on RabbitMQ.

rabbit:
    image: rabbitmq:3-management
    ports:
        - "15672:15672"
        - "5672:5672"

gobackend:
    build: ./go-backend
    ports:
        - "8080:8080"
    restart: unless-stopped
    depends_on:
        - rabbit

Another possible solution is to add a healthcheck clause in RabbitMQ service and check it in your other service, like this

rabbit:
    image: rabbitmq:3-management
    ports:
        - "15672:15672"
        - "5672:5672"
    healthcheck:
        test: rabbitmq-diagnostics -q ping
        interval: 10s
        timeout: 5s
        retries: 2
        start_period: 5s

gobackend:
    build: ./go-backend
    ports:
        - "8080:8080"
    depends_on:
        rabbit:
            condition: service_healthy
Paraffinic answered 23/6 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.