avatarChanghui Xu

Summary

This article provides a guide on how to quickly spin up RabbitMQ instances on Docker using the RabbitMQ Docker image and Docker Compose.

Abstract

The article begins by introducing RabbitMQ, a widely-deployed open-source message broker, and its features. It then proceeds to explain how to use the RabbitMQ Docker image with the management plugin installed and enabled by default. The article provides a step-by-step guide on how to pull the RabbitMQ Docker image from DockerHub, start a RabbitMQ container, and access the RabbitMQ management website. The article also discusses how to export RabbitMQ definitions and load them in a Docker container using Docker Compose. The author provides examples of customized RabbitMQ definitions and configurations and explains how to mount them to the Docker container. The article concludes by stating that both RabbitMQ and Docker are great tools in development and that the article aims to speed up the learning process for RabbitMQ.

Opinions

  • RabbitMQ is a widely-deployed open-source message broker that is lightweight and easy to deploy on premises and in the cloud.
  • The official tutorials for RabbitMQ are extremely easy to follow.
  • Using the RabbitMQ Docker image with the management plugin installed and enabled by default is a quick way to spin up RabbitMQ instances on Docker.
  • Exporting RabbitMQ definitions and loading them in a Docker container using Docker Compose allows for customized RabbitMQ instances with the same behaviors in different environments and platforms.
  • The author recommends using both RabbitMQ and Docker as great tools in development.
  • The article aims to speed up the learning process for RabbitMQ.
  • The author provides examples of customized RabbitMQ definitions and configurations and explains how to mount them to the Docker container.

Get Started with RabbitMQ on Docker

How to quickly spin up RabbitMQ instances with Docker and Docker Compose.

Get Started with RabbitMQ on Docker

RabbitMQ (link) is the most widely-deployed open source message broker. A message broker is a computer program module that exchanges messages between the message producers and consumers, thus is able to effectively decouple different software components. RabbitMQ is lightweight and easy to deploy on premises and in the cloud. Moreover, its official tutorials are extremely easy to follow.

In this article, we will talk about how to quickly spin up RabbitMQ instances on Docker. We will go through two ways to run a RabbitMQ Docker image in a container: (1) using the docker run command; (2) using Docker Compose. Getting familiar with these two approaches should greatly accelerate your learning on RabbitMQ.

I will not talk about any RabbitMQ client code here, because the official tutorials have already done an awesome job in explaining code details in different scenarios. For your information, the demos in this article are written in .NET (should be transferable to other languages), and the complete code can be found in my GitHub repository.

Now let’s dive into the world of RabbitMQ and Docker.

Using the RabbitMQ Docker Image

The RabbitMQ container registry (link) includes a variety of images for different platforms. In this article, we will use the RabbitMQ image with a tag 3-management, which is a Docker image with the RabbitMQ management plugin installed and enabled by default.

Assuming the Docker Desktop has been installed, we use the command docker pull rabbitmq:3-management to pull a RabbitMQ Docker image from DockerHub. After the Docker image is downloaded and saved locally, we can start a RabbitMQ container using the following command.

In the command above, the port 5672 is used for the RabbitMQ client connections, and the port 15672 is for the RabbitMQ management website. This command may take a minute to execute. After that, we can open a browser window and use the URL http://localhost:15672 to visit the RabbitMQ management website.The following screenshot shows the website login page.

The login page for a RabbitMQ management website

We can use the default username and password, guest:guest, to log into the website. The following screenshot shows the homepage of a management website.

The homepage for a RabbitMQ management website

After login, we are able to explore the RabbitMQ configurations, connections, channels, exchanges and queues. We can also set up new login accounts with different permissions.

Easy-peasy! We now have an instance of RabbitMQ server. We can keep everything untouched in the management website, and start learning RabbitMQ by following its official tutorials.

For example, following the publish/subscribe tutorial, I have created a demo solution in my GitHub repository. The solution contains two Console projects: one message producer and one message consumer. We can play with the solution by starting the producer program and sending messages. Since the exchange type is fanout, we can spin up two or more consumer Console apps to receive the same messages using exclusive queues. The following screen recording shows a demo process.

A screen recording for a RabbitMQ demo. The demo is a basic pub/sub model with one message producer and two consumers. Each consumer uses a server-named queue. Exchange type is Fanout.

Docker Compose

The default RabbitMQ definitions are sufficient for learning purposes and some development scenarios, but not for most real-world projects. In reality, we usually need to set up predefined exchanges and queues, as well as a set of users with their permissions. Moreover, when we update the RabbitMQ definitions, it’s a good idea to export the definitions as a file and commit the definitions file to a version control system.

In this section, I will first show you how to export the RabbitMQ definitions. Then I will show you how to load the definitions in a Docker container using Docker Compose.

Export/Backup RabbitMQ Definitions

It’s not uncommon that we have customized RabbitMQ definitions for our applications. For example, in a demo in my GitHub repository, I configured a predefined topic exchange, which is bound to a queue named “mytest” with a routing key of “order.created”. See the screenshot below.

I also deleted the default guest user, then created an admin user, a message producer user, and a message consumer user. See the screenshot below.

After everything is configured, we can export the definitions by clicking a button on the homepage of the management website, as in the following screenshot.

Let’s name the downloaded RabbitMQ definitions file as “definitions.json”, and place it in a folder “rabbit/etc” under our solution folder.

The management website also provides a functionality for us to import definitions. But we will not use this feature today, because we want the RabbitMQ server to have all our definitions at startup.

Load/Import Broker Definitions using Docker Compose

Even though there are many answers on StackOverflow related to this topic, most of them are out-of-date. Here, we will use the latest versions of all dependencies.

We create a docker-compose.yml file with its content as follows.

In the docker-compose.yml file, line 8 is required if we want to let RabbitMQ load our customized definitions. Line 8 mounts our definitions.json file to the Docker container.

Lines 9 to 11 are some optional volumes, but are frequently used. Lines 10 and 11 are the RabbitMQ server’s database and log files. We can store them in our local disk in case we want to restore the message history. Note that these two local folders need to be ignored in Git.

Line 9 is related to the rabbitmq.conf file (link). The configuration file rabbitmq.conf allows the RabbitMQ server and plugins to be configured. Starting with RabbitMQ 3.7.0, the configuration file is in the sysctl format. The RabbitMQ git repository has an example rabbitmq.conf file, which includes most of the configuration items we might want to set, along with documentation for those settings.

In my demo, I included a minimal rabbitmq.conf file, which is totally optional. The following code snippet shows the content and format of this rabbitmq.conf file.

Alright. With the docker-compose.yml file, we can easily spin up a RabbitMQ server with our customized definitions and configurations. The command we will use is docker-compose up. A quick way to verify the custom definitions is to log into the management website as the admin user (admin:admin123). We should see that the guest user is no longer existing in the RabbitMQ server.

With customized definitions, we are able to launch RabbitMQ instances with the same behaviors in different environments and platforms. That’s cool.

Both RabbitMQ and Docker are great tools in our development. I hope this article will speed up your learning on RabbitMQ.

Also, you are more than welcome to read my other articles related to RabbitMQ: Get Started with RabbitMQ 2: Consume Messages Using Hosted Service, and Get Started with RabbitMQ 3: Multi-Container App.

That’s all for today. Thanks for reading.

Rabbitmq
Docker
Technology
Programming
Software Engineering
Recommended from ReadMedium