Home > Back-end >  Can't create connection to RabbitMq instance in Docker
Can't create connection to RabbitMq instance in Docker

Time:12-28

I've followed How to use this image, pulling and executing the latest image in Docker. The sender application is not dockerized, hence running in the local environment.

docker pull rabbitmq
docker run -d --hostname my-rabbit --name bunny-queue rabbitmq:3

It seems to be running as expected and I can verify in the logs that the host name and database are as described in the article. I see no errors logged, only a few warnings about indices being reinitialized.

node : rabbit@my-rabbit
database dir : /var/lib/rabbitmq/mnesia/rabbit@my-rabbit

Then, I set up a factory and try to create a connection (as shown e.g. here).

ConnectionFactory factory = new() { HostName = "my-rabbit" };
using IConnection connection = factory.CreateConnection();
using IModel channel = connection.CreateModel();

I've tried different values for the HostName field (both with and without explicit port). I googled the exact exception thrown (None of the specified endpoints were reachable). I've never had issues with this part before so I sense it's related to me running the bunny locally in the Docker. The closest hit was this issue but for a remote server, while I'm running the local, default values as exemplified in the official docs. Someone suggests to do a full metal jacket configuration of the factory. I failed to make it work that way and I sense that the default values in the official docs should work for the basic scario, which implied that the issue is elsewhere.

I also tried to fire up another image including the managment tools as shown here. When I access (as a guest), I see that the AMQP protocol is bound to :: on port 5672. No errors, warnings nor issues reported as far I can tell.

docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.9-management

Not sure how to investigate further.

CodePudding user response:

You have several options:

  1. Bind ports of the container to your host's port and then connect to your host's ports. To bind container ports to your host ports, use the -p switch when starting the container
  2. Run your docker container in "host" network mode and connect to your host's ports. This can be achieved by specifying the --network host option. All ports of your application will be available as if you had started the application natively on the host.
  3. Create a docker network and run both RabbitMQ and your application inside the same network. Connect to the rabbitmq container's ports. To create a docker network, run docker network create name and then start your containers with --network name.
  4. Define your containers in a docker-compose file. All containers in a file will automatically share the same network. You can define custom networks too and connect certain containers to specific networks only.

As an example of #1, the bunny should be executed using the following.

docker run -detach --hostname rabbitmq --publish 5672:5672 --name bunny rabbitmq:3

Then, in the factory, the host of the local environment needs to be passed.

ConnectionFactory factory = new() { HostName = "localhost" };
using IConnection connection = factory.CreateConnection();
  • Related