Configuring RabbitMQ On Its Startup With Docker

Elnatan Torres
4 min readJan 20, 2021

Recently, I have had to specify the configuration of a RabbitMQ server and integrate a .NET application with it. To do so, I had some concerns, such as:

  1. Constantly, I come across applications that are hard to get up and running because of their dependencies. Sometimes the ReadMe file is well written, but there are so many things you have to do to run the application that you get tired. Therefore, for this integration with RabbitMQ, I would like that the development was easy to configure and with the fewest possible commands.
  2. Sometimes it is hard to maintain the development environment (the machine of the developer) equal to the homologation environment in order to simulate and test real scenarios. Using RabbitMQ, for example, it may be exhausting for every developer to create every queue, exchange, binding, etc. It may even cause mistakes depending on the quantity of configurations, which could invalidate somes tests. By following this line of reasoning, I would like to guarantee that every developer would have the exact same copy of RabbitMQ by default.

If you have ever come across these problems with RabbitMQ, these steps will help you to solve it:

  1. Configure the rabbitmq.conf file

Rabbitmq.conf is the main configuration file of RabbitMQ, since it allows you to configure the server and plugins. We are going to use it to configure some global settings, such as:

  • default_user: by default, RabbitMQ will start its server with the default user, which is guest. But, in this example, we will change it to john123.
  • default_pass: by default, RabbitMQ will start its server with the default password, which is guest. But, in this example, we will change it to 123456.
  • listeners.tcp.default: by default, RabbitMQ will listen on all interfaces, using the standard (reserved) AMQP 0–9–1 and 1.0 port. We will set it to 5672.
  • management.tcp.port: we will set the management interface’s port to 15672.
  • management.load_definitions: the JSON file that contains the definitions of the RabbitMQ server. We will see how to configure it, but at this moment just accept its value as /etc/rabbitmq/definitions.json.

Your rabbitmq.conf file should look like this:

2. Configure the definitions.json file

This file configures the features of the RabbitMQ server, such as queues, exchanges, bindings, virtual hosts, etc. In this example, we will create a virtual host, assign permission to the default user, create a simple queue and bind it to an exchange. There are other configurations that are not so relevant like the RabbitMQ ‘s version and global parameters, so I will not talk about them. Another important matter is that the password_hash for a user must be generated using RabbitMQ’s algorithms. Having said that, the following file:

  1. Creates the virtual host demo-vhost
  2. Assigns full permission to the user john123 at the virtual host demo-vhost
  3. Creates a queue called demo-queue at the virtual host demo-vhost
  4. Binds the queue demo-queue to the exchange amq.direct (the amq.direct is a default exchange of RabbitMQ that delivers messages to the queue based on the routing key).

Your definitions.json file should look like this:

3. Configure the Dockerfile

The Dockerfile is a “receipt” of how to create the desired container using Docker. In this example, we will use the image rabbitmq:3.7-management, copy the previously configured files to Docker’s directory (rabbitmq.conf and definitions.json) and execute the rabbitmq.conf file.

The Dockerfile should look like this:

4. Configure the Docker-compose file

Only the three previous steps would be enough, but I chose to configure the docker-compose file too to exemplify how it would be in a real scenario where there are other dependencies to run.

The docker-compose file is the container’s orchestrator of Docker, which will be used to create a network and use it to run RabbitMQ server and orchestrate other configurations of the container, such as the mapping between the host server and Docker container at the ports key.

This configuration supposes that all the files are at the same directory.

The Docker-compose file should look like this:

5. Run the docker-compose file

Run the following command to execute the docker-compose file:

docker-compose up -d

After this step, you shall see the following output on the terminal and the RabbitMQ server running at localhost:15672 with the desired user, virtual host e queue created as desired.

Output on the terminal after executing the command of docker-compose
User that was created at RabbitMQ
Virtual host that was created at RabbitMQ
Queue that was created at RabbitMQ
Queue linked to the exchange at RabbitMQ

Another important observation is that the definitions of RabbitMQ can be found at http://localhost:15672/api/definitions and will also be updated after any modification made using the management interface, so you can use it to update your definitions.json file.

With this solution, I expect you can easily maintain the your local instance of RabbitMQ and equalize the development environment of all the developers of your squad.

--

--