To start RabbitMQ, we will use the official image for Docker from DockerHub, located here: https://hub.docker.com/_/rabbitmq/. We have already used Docker to start instances of databases. Starting RabbitMQ is similar:
docker run -it --rm --name test-rabbit -p 5672:5672 rabbitmq:3
We started a container called test-rabbit and forward port 5672 to the same port of the container. RabbitMQ images also exposes ports 4369, 5671, and 25672. If you want to use the advanced features of the message broker, you need to open these ports too.
If you want to start an instance of RabbitMQ and have access to it from other containers, you can set the --hostname arguments for the run command and use the provided name from other containers to connect to the RabbitMQ instance.
When a message broker instance starts, you may need to get some statistics from it. The rabbitmqctl command can be executed inside the container using the exec command from Docker:
docker exec -it test-rabbit rabbitmqctl
It prints the available commands. Add any of them to the command, like this:
docker exec -it test-rabbit rabbitmqctl trace_on
The preceding command activates tracing all the messages pushed to queues, which you can see with the following command:
docker exec -it test-rabbit rabbitmqctl list_exchanges
It prints the following:
Listing exchanges for vhost / ...
amq.headers headers
amq.direct direct
amq.topic topic
amq.rabbitmq.trace topic
direct
amq.fanout fanout
amq.match headers
Now, we can create a microservice that uses the message broker for interaction with the worker.