Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMq refuses connection when run in docker

My docker-compose file looks like this:

version: '2'

services:
  explore:
    image: explore
    build:
      context: ./Explore
      dockerfile: VsDockerfile
    environment:
      - "ElasticUrl=http://localhost:9200"
      - "RabbitMq/Host=localhost"
      - "RabbitMq/Username=guest"
      - "RabbitMq/Password=guest"
    networks:
      - localnet

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:5.4.3
    container_name: elasticsearch
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ports:
      - 9200:9200
    volumes:
      - ./esdata:/usr/share/elasticsearch/data
    networks:
      - localnet

  rabbit:
    image: rabbitmq:3.6.7-management
    hostname: rabbit
    ports:
      - 15672:15672
      - 5672:5672
    networks:
      - localnet

networks:
  localnet:
    external:
      name: localnet

If I type http://localhost:15672 in the browser, I get the rabbitmq interface, but if I tries to connect from my Explore project like this:

public SqlToRabbitProcessor(SqlToRabbitRepository sqlToRabbitRepository)
{
    _sqlToRabbitRepository = sqlToRabbitRepository;

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

    var rabbit = factory.CreateConnection();
    channel = rabbit.CreateModel();
}

Then it breaks in the line

var rabbit = factory.CreateConnection();

with the error saying

ExtendedSocketException: Connection refused 127.0.0.1:5672 System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)

ConnectFailureException: Connection failed RabbitMQ.Client.EndpointResolverExtensions.SelectOne(IEndpointResolver resolver, Func selector)

BrokerUnreachableException: None of the specified endpoints were reachable RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, string clientProvidedName)

like image 353
Joshlo Avatar asked Jul 05 '17 12:07

Joshlo


People also ask

How do I test rabbitmq connection?

Here are the recommended steps: Make sure the node is running using rabbitmq-diagnostics status. Verify config file is correctly placed and has correct syntax/structure. Inspect listeners using rabbitmq-diagnostics listeners or the listeners section in rabbitmq-diagnostics status.

What port does rabbitmq run on?

By default, RabbitMQ will listen on port 5672 on all available interfaces. It is possible to limit client connections to a subset of the interfaces or even just one, for example, IPv6-only interfaces.


1 Answers

As my comment under the question suggested, it's because the "localhost" defined in the web application part is it's containers localhost, and not the docker host..

just needed to change

- "ElasticUrl=http://localhost:9200"
- "RabbitMq/Host=localhost"

to

- "ElasticUrl=http://elasticsearch:9200"
- "RabbitMq/Host=rabbit"
like image 60
Joshlo Avatar answered Oct 17 '22 05:10

Joshlo