I have set up RabbitMQ server on my dev machine using this docker image.
I have used below command to setup my container
docker run -d --name my-rabbit -p 5672:15672 rabbitmq:3-management
Below is docker ps command output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a40704b7f3a4 rabbitmq:3-management "docker-entrypoint.s…" 13 minutes ago Up 12 minutes 4369/tcp, 5671-5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:5672->15672/tcp my-rabbit
Management console is accessible at http://localhost:5672 and I can login using default username and password ( guest/guest )
Below is my .Net Core code
public RabbitMQMnager()
{
var factory = new ConnectionFactory();
factory.Port = 5672;
Uri uri = new Uri("amqp://guest:guest@localhost:5672/");
var connection = factory.CreateConnection();
//Below are values of different connection string parameters
factory.HostName = "localhost";
factory.UserName = "guest";
factory.Password = "guest";
factory.VirtualHost = "/";
factory.Port = 5672;
var channel = connection.CreateModel(); //<- Exception here
}
Upon executing above code, I am getting below exception.
RabbitMQ.Client.Exceptions.BrokerUnreachableException: 'None of the specified endpoints were reachable'
Stack Trace
This exception was originally thrown at this call stack:
RabbitMQ.Client.Framing.Impl.Connection.StartAndTune()
RabbitMQ.Client.Framing.Impl.Connection.Open(bool)
RabbitMQ.Client.Framing.Impl.Connection.Connection(RabbitMQ.Client.IConnectionFactory, bool,
RabbitMQ.Client.Impl.IFrameHandler, string)
RabbitMQ.Client.Framing.Impl.AutorecoveringConnection.Init(RabbitMQ.Client.Impl.IFrameHandler)
RabbitMQ.Client.Framing.Impl.AutorecoveringConnection.Init(RabbitMQ.Client.IEndpointResolver)
RabbitMQ.Client.ConnectionFactory.CreateConnection(RabbitMQ.Client.IEndpointResolver, string)
I found similar questions but solution mentioned there is not enough for me. It looks like it has something to do with docker and the network created by docker.
Refused connection to RabbitMQ when using docker link
Other details
- .Net Core 3.1 app
RabbitMQ.Client -> 5.1.2
UPDATE 1
As per @ThisIsNoZaku answer exposing additional port solved my issue.
docker run -d --hostname my-rabbit --name my-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management