Using RabbitMQ from inside a Docker container

Viewed 2441

I am trying to use Docker with an application. Everything seems to work except Rabbit MQ. Therefore in order to create a test case I have done the following:

Step 1 - Run outside Docker - works as expected

1) Create a simple ASP.NET Core 2.1 Console App:

using System;
using RabbitMQ.Client;

namespace DockerRabbitMQ
{
    class Program
    {
        static void Main(string[] args)
        {
            RabbitMQConnect();
        }

        public static void RabbitMQConnect()
        {
            var factory = new ConnectionFactory
            {
                HostName = "localhost",
                UserName = "guest",
                Password = "guest"
            };

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

2) Install RabbitMQ on local PC and test it works by browsing to: http://localhost:15672. I see the management portal as expected, so it is working.

3) Run the Console app. It runs and completes as expected.

Step 2 - Run inside Docker

1) Right click on the Console app and select: Add/Container Orchestration Support. The DOCKERFILE and docker-compose are added.

2) Add the following to Docker Compose:

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

3) Stop the Rabbit MQ Service running on the local PC (the service was created in step 1, part 2)

4) Amend the code in step 1 part 1 to say:

HostName = "Rabbit",

5) Run Docker Compose in Visual Studio. Here is the error:

enter image description here

I believe my question is similar to this one: RabbitMq refuses connection when run in docker. Why am I prompted with this error?

Update

In order to ensure RabbitMQ starts before the console app; I will amend the compose file with the following:

depends_on:
      - rabbitmq

Update 2

I have tried following the instructions above using an MVC app instead of a console app (in step 1). I put the connection code in the Startup constructor (just for testing) and I see this:

enter image description here

Why do I see an error and why is it trying to connect to: 92.242.132.15:5672? The Docker Compose fore the MVC app (and rabbit mw) looks like this:

version: '3.4'

services:
  dockerrabbitmqmvc:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
      - ASPNETCORE_HTTPS_PORT=44336
    ports:
      - "54258:80"
      - "44336:443"
    volumes:
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro

      rabbitmq:
        image: rabbitmq:3-management-alpine
        hostname: rabbit
        ports:
          - "15672:15672"
          - "5672:5672"
        expose:
          - "15672"
          - "5672"
1 Answers

You have to expose the ports of rabbitmq to outside in your docker compose file after mapping the ports - something like below

rabbit:
    image: rabbitmq:3-management-alpine
    hostname: rabbit
    ports:
      - "15672:15672"
      - "5672:5672"
    expose:
     - 15672
     - 5672
Related