title: docker-compose with rabbitmq
body: <p>I'm trying to setup a docker-compose script - to start a dummy: website, API, Gateway and RabbitMQ. (micro service approach)</p>
  
  <p><strong>Request pipeline:</strong></p>
  
  <p>Web >> Gateway >> API >> RabbitMQ</p>
  
  <p>My docker-compose looks like this:</p>
  
  <pre><code>version: "3.4"
  
  services:
    web:
      image: webclient
      build:
        context: ./WebClient
        dockerfile: dockerfile
      ports:
        - "4000:4000"  
      depends_on:
       - gateway
  
    gateway:
      image: gatewayapi
      build:
        context: ./GateWayApi
        dockerfile: dockerfile
      ports:
        - "5000:5000"
      depends_on:
       - ordersapi
  
    ordersapi:
      image: ordersapi
      build:
        context: ./ExampleOrders
        dockerfile: dockerfile
      ports:
        - "6002:6002" 
      depends_on:
        - rabbitmq
  
    rabbitmq:
      image: rabbitmq:3.7-management
      container_name: rabbitmq
      hostname: rabbitmq
      volumes:
        - rabbitmqdata:/var/lib/rabbitmq
      ports:
        - "7000:15672"
        - "7001:5672"
      environment:
        - RABBITMQ_DEFAULT_USER=rabbitmquser
        - RABBITMQ_DEFAULT_PASS=some_password
  </code></pre>
  
  <p><strong>This pipe works:</strong></p>
  
  <p>Web >> Gateway >> API</p>
  
  <p>I get a response from the API on the website.</p>
  
  <p>But when I try to push a message to rabbitmq from the API, I get the following error:</p>
  
  <p><code>System.AggregateException: One or more errors occurred. (Connection failed) ---&gt; RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed ---&gt; System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Connection refused 127.0.0.1:7001</code></p>
  
  <p>The RabbitMQ managment GUI still works on the defined port 7000. 
  Requests to port 7001 does not.</p>
  
  <p>However, if I start the API and RabbitMQ manually, it works like a charm. The API I simply start with a debugger (.Net core + IIS - default settings hitting F5 in VS) and this is the command I use to start the docker image manually:</p>
  
  <p><code>docker run -p 7001:5672 -p 7000:15672 --hostname localhost -e RABBITMQ_DEFAULT_USER=rabbitmquser -e RABBITMQ_DEFAULT_PASS=some_password rabbitmq:3.7-management</code></p>
  
  <p><strong>Update</strong></p>
  
  <p>This is how I inject the config in the .Net core pipe.
  </p>
  
  <p>startup.cs</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      // setup RabbitMQ
      var configSection = Configuration.GetSection("RabbitMQ");
      string host = configSection["Host"];
      int.TryParse(configSection["Port"], out int port);
      string userName = configSection["UserName"];
      string password = configSection["Password"];
  
      services.AddTransient&lt;IConnectionFactory&gt;(_ =&gt; new ConnectionFactory()
      {
          HostName = host,
          Port = port,
          UserName = userName,
          Password = password
      });
  
   services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
  }
  </code></pre>
  
  <p>controller.cs</p>
  
  <pre><code>private readonly IConnectionFactory _rabbitFactory;
  
  public ValuesController(IConnectionFactory rabbitFactory)
  {
      _rabbitFactory = rabbitFactory;
  }
  
  public void PublishMessage()
  {
      try
      {
          using (var connection = _rabbitFactory.CreateConnection())
          using (var channel = connection.CreateModel())
          {
              string exchangeName = "ExampleApiController";
              string routingKey = "MyCustomRoutingKey";
              channel.ExchangeDeclare(exchange: exchangeName, type: "direct", durable: true); 
              SendMessage("Payload to queue 1", channel, exchangeName, routingKey);
          }
      }
      catch (Exception e)
      {
          Console.WriteLine(e.InnerException);
      }
  }
  
  private static void SendMessage(string message, IModel channel, string exchangeName, string routingKey)
  {
      byte[] body = Encoding.UTF8.GetBytes(message);
      channel.BasicPublish(exchange: exchangeName,
                           routingKey: routingKey,
                           basicProperties: null,
                           body: body);
      Console.WriteLine($" Sending --&gt; Exchange: { exchangeName }    Queue: { routingKey }    Message: {message}");
  }
  </code></pre>