5

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"
r3mus n0x
  • 5,954
  • 1
  • 13
  • 34
w0051977
  • 15,099
  • 32
  • 152
  • 329
  • In your docker-compose.yml here, it looks like the `rabbitmq` service is offset one tab more than the `dockerrabbitmqmvc`. I doubt that's actually how you have it, because it wouldn't run, but just wanted to make sure everything was aligned here as it is locally for you. – bluescores Oct 29 '18 at 17:55

1 Answers1

2

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
Mithun Manohar
  • 516
  • 1
  • 6
  • 18
  • Where does this go please? – w0051977 Oct 28 '18 at 08:44
  • Also it takes a few moments for rabbitmq to start up during which you may get a connection refused messages. – Mithun Manohar Oct 28 '18 at 08:46
  • Thank you. That works. I guess I need to add a depends on to ensure RabbitMQ is started up first (can you see my update - is there anything else I need to do to ensure RabbitMQ is available)? – w0051977 Oct 28 '18 at 08:51
  • 1
    See if 'depends' helps to make sure rabbit starts completely. Else check out solutions suggested in https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y for adding a wait. Please accept answer if it solves your problem ! – Mithun Manohar Oct 28 '18 at 08:55
  • I want to make sure I fully understand this. Could you see my update 2? +1 for the hyperlink to the other question (I will upvote when I mark the answer - once I understand). – w0051977 Oct 28 '18 at 09:46
  • If I change the hostname to my PC name (for the MVC app), then it works. Why do I have to do this with the MVC app specifically? – w0051977 Oct 28 '18 at 10:16
  • Is your app also run as docker container composed using the docker compose file? – Mithun Manohar Oct 28 '18 at 10:21
  • yes. I have posted the Docker Compose file (see latest update to question). Thanks. – w0051977 Oct 28 '18 at 10:23
  • I notice ports are not enclosed in quotes for rabbit docker compose definitions. Please change to `ports: - "5672:5672" - "15672:15672"` – Mithun Manohar Oct 28 '18 at 10:47
  • Is adding `expose` when you have `ports` defined something specific to .NET or MVC? I run RabbitMQ in docker-compose for a Django app and definitely don't have `expose` in there. – bluescores Oct 28 '18 at 11:35
  • @bluescores, thanks. No, I am just trying to get this to work. Do you have any suggestions? justanotherguys suggestion appears to work. – w0051977 Oct 28 '18 at 21:56
  • @justanotherguy, I have added quotes to the ports, however this has made no difference. Do you have any other suggestions? Thanks. – w0051977 Oct 28 '18 at 21:57
  • @bluescores, could you explain what you mean by: "definitely don't have expose in there". Do you have EXPOSE at all? Thanks. – w0051977 Oct 29 '18 at 17:33
  • @w0051977 Correct, I don't have `expose:` at all in mine. If you track down the Dockerfile for the RabbitMQ image you have here, it's exposing the ports there so you are just duplicating that declaration, it should have no effect. Also, when a port is mapped in docker-compose.yml (like `- "15672:15672"`) the expose is implied here, with the value on the right of the colon. – bluescores Oct 29 '18 at 17:54