title: Using RabbitMQ from inside a Docker container
body: <p>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:</p>
  
  <p><strong>Step 1 - Run outside Docker - works as expected</strong></p>
  
  <p>1) Create a simple ASP.NET Core 2.1 Console App:</p>
  
  <pre><code>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();
          }
      }
  }
  </code></pre>
  
  <p>2) Install RabbitMQ on local PC and test it works by browsing to: <a href="http://localhost:15672" rel="nofollow noreferrer">http://localhost:15672</a>.  I see the management portal as expected, so it is working.</p>
  
  <p>3) Run the Console app.  It runs and completes as expected.</p>
  
  <p><strong>Step 2 - Run inside Docker</strong></p>
  
  <p>1) Right click on the Console app and select: Add/Container Orchestration Support.  The DOCKERFILE and docker-compose are added.</p>
  
  <p>2) Add the following to Docker Compose:</p>
  
  <pre><code>rabbit:
      image: rabbitmq:3-management-alpine
      hostname: rabbit
      ports:
        - "15672:15672"
        - "5672:5672"
  </code></pre>
  
  <p>3) Stop the Rabbit MQ Service running on the local PC (the service was created in step 1, part 2)</p>
  
  <p>4) Amend the code in step 1 part 1 to say:</p>
  
  <pre><code>HostName = "Rabbit",
  </code></pre>
  
  <p>5) Run Docker Compose in Visual Studio.  Here is the error:</p>
  
  <p><a href="https://i.stack.imgur.com/48NEB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/48NEB.png" alt="enter image description here"></a></p>
  
  <p>I believe my question is similar to this one: <a href="https://stackoverflow.com/questions/44925747/rabbitmq-refuses-connection-when-run-in-docker?rq=1">RabbitMq refuses connection when run in docker</a>.  Why am I prompted with this error?</p>
  
  <p><strong>Update</strong></p>
  
  <p>In order to ensure RabbitMQ starts before the console app; I will amend the compose file with the following:</p>
  
  <pre><code>depends_on:
        - rabbitmq
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>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:</p>
  
  <p><a href="https://i.stack.imgur.com/Olhp6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Olhp6.png" alt="enter image description here"></a></p>
  
  <p>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:</p>
  
  <pre><code>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"
  </code></pre>