Configuring RabbitMQ On Its Startup With Docker

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:

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 (the 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.

Backend developer, passionate by .NET, father of a turtle and a chinchilla and step father of three dogs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store