title: Can't run on docker a C# script with RabbitMQ
body: <p>I'm trying to establish a communication between two container using RabbitMQ.
  The two containers are two c# scripts, and the first generates random numbers every second and the other tells if the number is even or odd. I tested the communication in my local machine (Windows) and it worked, but now that I'm trying to run the first image (that should generate random numbers) I have several errors. The error stacktrace is this:</p>
  <pre><code>Unhandled exception. RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable
   ---&gt; System.AggregateException: One or more errors occurred. (Connection failed)
   ---&gt; RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed
   ---&gt; System.Net.Sockets.SocketException (111): Connection refused
     at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
     at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
     at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.&lt;&gt;c.&lt;.cctor&gt;b__4_0(Object state)
  --- End of stack trace from previous location ---
     at RabbitMQ.Client.Impl.TcpClientAdapter.ConnectAsync(String host, Int32 port)
     at RabbitMQ.Client.Impl.TaskExtensions.TimeoutAfter(Task task, TimeSpan timeout)
     at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpoint endpoint, TimeSpan timeout)
     --- End of inner exception stack trace ---
     at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpoint endpoint, TimeSpan timeout)
     at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectUsingAddressFamily(AmqpTcpEndpoint endpoint, Func`2 socketFactory, TimeSpan timeout, AddressFamily family)
     at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectUsingIPv4(AmqpTcpEndpoint endpoint, Func`2 socketFactory, TimeSpan timeout)
     at RabbitMQ.Client.Impl.SocketFrameHandler..ctor(AmqpTcpEndpoint endpoint, Func`2 socketFactory, TimeSpan connectionTimeout, TimeSpan readTimeout, TimeSpan writeTimeout)
     at RabbitMQ.Client.Framing.Impl.IProtocolExtensions.CreateFrameHandler(IProtocol protocol, AmqpTcpEndpoint endpoint, Func`2 socketFactory, TimeSpan connectionTimeout, TimeSpan readTimeout, TimeSpan writeTimeout)
     at RabbitMQ.Client.ConnectionFactory.CreateFrameHandler(AmqpTcpEndpoint endpoint)
     at RabbitMQ.Client.EndpointResolverExtensions.SelectOne[T](IEndpointResolver resolver, Func`2 selector)
     --- End of inner exception stack trace ---
     at RabbitMQ.Client.EndpointResolverExtensions.SelectOne[T](IEndpointResolver resolver, Func`2 selector)
     at RabbitMQ.Client.Framing.Impl.AutorecoveringConnection.Init(IEndpointResolver endpoints)
     at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, String clientProvidedName)
     --- End of inner exception stack trace ---
     at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, String clientProvidedName)
     at RabbitMQ.Client.ConnectionFactory.CreateConnection(String clientProvidedName)
     at NewTask.Program.Main(String[] args) in C:\Users\lucac\Desktop\NewTask\NewTask.cs:line 12
  PS C:\Users\lucac\Desktop\NewTask&gt; docker run --rm --network rabbitmq -p 5671:15672 --name generatore producer:latest          
  Unhandled exception. RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable
   ---&gt; System.AggregateException: One or more errors occurred. (Connection failed)
   ---&gt; RabbitMQ.Client.Exceptions.ConnectFailureException: Connection failed
   ---&gt; System.Net.Sockets.SocketException (111): Connection refused
     at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
     at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
     at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.&lt;&gt;c.&lt;.cctor&gt;b__4_0(Object state)
  --- End of stack trace from previous location ---
     at RabbitMQ.Client.Impl.TcpClientAdapter.ConnectAsync(String host, Int32 port)
     at RabbitMQ.Client.Impl.TaskExtensions.TimeoutAfter(Task task, TimeSpan timeout)
     at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpoint endpoint, TimeSpan timeout)
     --- End of inner exception stack trace ---
     at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectOrFail(ITcpClient socket, AmqpTcpEndpoint endpoint, TimeSpan timeout)
     at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectUsingAddressFamily(AmqpTcpEndpoint endpoint, Func`2 socketFactory, TimeSpan timeout, AddressFamily family)
     at RabbitMQ.Client.Impl.SocketFrameHandler.ConnectUsingIPv4(AmqpTcpEndpoint endpoint, Func`2 socketFactory, TimeSpan timeout)
     at RabbitMQ.Client.Impl.SocketFrameHandler..ctor(AmqpTcpEndpoint endpoint, Func`2 socketFactory, TimeSpan connectionTimeout, TimeSpan readTimeout, TimeSpan writeTimeout)
     at RabbitMQ.Client.Framing.Impl.IProtocolExtensions.CreateFrameHandler(IProtocol protocol, AmqpTcpEndpoint endpoint, Func`2 socketFactory, TimeSpan connectionTimeout, TimeSpan readTimeout, TimeSpan writeTimeout)
     at RabbitMQ.Client.ConnectionFactory.CreateFrameHandler(AmqpTcpEndpoint endpoint)
     at RabbitMQ.Client.EndpointResolverExtensions.SelectOne[T](IEndpointResolver resolver, Func`2 selector)
     --- End of inner exception stack trace ---
     at RabbitMQ.Client.EndpointResolverExtensions.SelectOne[T](IEndpointResolver resolver, Func`2 selector)
     at RabbitMQ.Client.Framing.Impl.AutorecoveringConnection.Init(IEndpointResolver endpoints)
     at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, String clientProvidedName)
     --- End of inner exception stack trace ---
     at RabbitMQ.Client.ConnectionFactory.CreateConnection(IEndpointResolver endpointResolver, String clientProvidedName)
     at RabbitMQ.Client.ConnectionFactory.CreateConnection(String clientProvidedName)
     at NewTask.Program.Main(String[] args) in C:\Users\lucac\Desktop\NewTask\NewTask.cs:line 12
  </code></pre>
  <p>These are the steps I takes:</p>
  <ul>
  <li><code>docker pull rabbitmq:3.8-management</code></li>
  <li><code>docker network create rabbitmq</code></li>
  <li><code>docker run --network rabbitmq -d --name some-rabbit --hostname localhost -p 5672:5672 -p 15672:15672 rabbitmq:3.8-management</code></li>
  <li><code>docker build -t producer:latest .</code></li>
  <li><code>docker run --rm --network rabbitmq -p 5672:5672 --name generatore producer:latest</code></li>
  </ul>
  <p>And here's the code:</p>
  <pre><code>using System;
  using RabbitMQ.Client;
  using System.Text;
  using System.Threading;
  using System.Net;
  using System.Net.Sockets;
  using System.Linq;
  
  namespace NewTask
  {
      class Program
      {
          static void Main(string[] args)
          {
              // var name = Dns.GetHostName(); 
              // var ip = Dns.GetHostEntry(name).AddressList.FirstOrDefault(x =&gt; x.AddressFamily == AddressFamily.InterNetwork);
              // var ip = Environment.GetEnvironmentVariable(&quot;host&quot;);
              // var factory = new ConnectionFactory() 
              // { 
              //     HostName = &quot;localhost&quot;, // localhost
              //     UserName = ConnectionFactory.DefaultUser,  
              //     Password = ConnectionFactory.DefaultPass,
              //     VirtualHost = &quot;/&quot;, // maybe isn't useful
              //     Port = AmqpTcpEndpoint.UseDefaultPort 
              // };
              var factory = new ConnectionFactory()
              {
                  Uri = new Uri(&quot;amqp://guest:guest@localhost:5672/&quot;),
                  DispatchConsumersAsync = false,
                  ConsumerDispatchConcurrency = 1 
              };
              using(var connection = factory.CreateConnection())
              using(var channel = connection.CreateModel())
              {
                  channel.QueueDeclare(queue: &quot;random&quot;,
                                      durable: false,
                                      exclusive: false,
                                      autoDelete: false,
                                      arguments: null);
                  
                  Random rnd = new Random();
                  while (true)
                  {
                      int number = rnd.Next(0, 100);
  
                      var body = Encoding.UTF8.GetBytes(number.ToString()); 
  
                      channel.BasicPublish(exchange: &quot;&quot;,
                                          routingKey: &quot;random&quot;,
                                          basicProperties: null,
                                          body: body);
                      Console.WriteLine(&quot; [x] Inviato: {0}&quot;, number); 
  
                      Thread.Sleep(1000); // 1 second
                  }
  
              }
          }
      }
  }
  </code></pre>
  <p>And the Dockerfile, if could be useful:</p>
  <pre><code>FROM mcr.microsoft.com/dotnet/sdk:5.0
  
  ENV host=&quot;localhost&quot;
  
  COPY bin/Release/net5.0/publish/ NewTask/
  
  WORKDIR /NewTask
  
  EXPOSE 5672 15672
  
  ENTRYPOINT [&quot;dotnet&quot;, &quot;NewTask.dll&quot;]
  </code></pre>
  <p>I'm not a Docker expert, and I've done other attempts following similar questions and similar errors but unfortunately no one resolved the problem for me. If someone have suggestions or advices, anything could be really helpful for me!</p>
  <p>Thanks for the help!</p>