1

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.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

1 Answers1

3

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();
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
knittl
  • 246,190
  • 53
  • 318
  • 364
  • This was quite structured and clear. The way I interpret it, #1 is keeping stuff decoupled except the exact ports exposed. The rest are making the bunny and the publisher parts of common network (sort of). I'd prefer to keep stuff as isolated as possible so exposing ports is the way to go. However, when I ran with explicit port binding (the second run, including management, I connected 5672's) I didn't get the error to vanish. Or am I missing something in the process? – Konrad Viltersten Dec 27 '21 at 11:22
  • Just so I don't mislead you. I ran the command `docker run -detach --hostname rabbitmq --publish 5672:5672 --name bunny rabbitmq:3` now and I see the port binding in the Docker client. Still, the same error. How can I confirm that the issue is (or isn't) with how the RabbitMq is set up on my machine's Docker client? – Konrad Viltersten Dec 27 '21 at 11:28
  • Nevermind. I just realized what you referred to, mate. I'm supposed to use `localhost` as the factory parameter **and** expose the ports explicitly when running the bunny in the Docker client. I'd like to amend that to you answer before I accept it. Some folks dislike when others tamper with their contribution so I'm asking permission first, as a token of respect. – Konrad Viltersten Dec 27 '21 at 11:31
  • Note that option 3 and 4 are more "decoupled" than option 1. Option 1 exposes the ports "globally" on your host, any application with access to your host can connect to them. Using 3 or 4 means that only applications in the same docker network have access to your services, which IMHO is a lot clearer and doesn't require the ports to be available on your host. – knittl Dec 27 '21 at 12:03
  • Thank you for that comment. If I'm to execute the publisher and the consumer in the same *docker network* as the RabbitMq, that requires me to execute those two under Docker as well, right? For now, I prefer to run them locally to keep the complexity at a minimum avoiding confusion since Docker isn't my strongest suite (yet). But the point of yours is valid (and welcome). Or did I misunderstand? – Konrad Viltersten Dec 27 '21 at 12:24
  • @KonradViltersten no, I think you got that right. Your use case makes sense. – knittl Dec 27 '21 at 18:47