How to Create RabbitMQ Docker Container Image (Tutorial)

How to Create RabbitMQ Docker Container Image. RabbitMQ is a lightweight messaging system that may be deployed on premises or in the Cloud. Compatibility of RabbitMQ with multiple different protocols for messaging can be observed. To address the need for highly available and highly scalable options, RabbitMQ may be implemented in distributed and federated topologies.

What is a Docker Container?

Docker is a group of platforms that provide service tools that distribute software in containers using virtualization on the OS level. Containers are self contained, containing their own distinct software, libraries, and configuration files packaged as a code so the application runs quickly and reliably from one computing environment to another.

Docker container is a lightweight, standalone  software pack  including everything  that a business infrastructure needs in terms of: code, system tools, system libraries and settings.

What is RabbitMQ?

RabbitMQ supports a broad variety of OS systems and cloud settings, as well as a comprehensive set of developer tools for the most common programming languages.

The most extensively used open source message broker is RabbitMQ. A message broker is a computer program module that exchanges messages between message producers and consumers, allowing various software components to be effectively decoupled.

A message queue is kept for these communications. A queue is a large message buffer based on the FIFO concept (first in, first out algorithm). Messages may be sent to queues by several producers and consumers can try to acquire messages sent from these queues.

RabbitMQ is a lightweight messaging system that may be deployed on premises or in the Cloud. Furthermore, its official instructions are quite simple to follow. In this article we’ll go through how to create RabbitMQ Docker Container image.

Using Docker Compose, we’ll go through an approach to execute a RabbitMQ Docker image in a container.  Make sure to have installed and set up Docker Compose  and Docker before we begin.

How to Create RabbitMQ Docker Container Image

Step 1-Configure Docker To Use the RabbitMQ Docker Image

In this section of how to create RabbitMQ Docker Container Image we look at a number of images for various systems are available in the registry of RabbitMQ containers. In this post, we will utilize the RabbitMQ image with tag 3-management, which is a Docker image that comes preinstalled with the RabbitMQ management plugin.

We use the following command to fetch a RabbitMQ Docker image from DockerHub, provided the Docker Desktop has been installed and set up already.

					docker pull rabbitmq:3-management

We may now proceed to start a RabbitMQ container using the command listed below once the Docker image has been downloaded and stored locally.

					docker run --rm -it --hostname my-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management

The port 5672 is used to make connections with clients for RabbitMQ, while the port 15672 is used for the management website of RabbitMQ as stated in the command above. The command might quite possibly take a minute or more to be fully executed.

Step 2 – Open your Browser to Log into The RabbitMQ Management Page

Upon completion of the step above, we will proceed to instantiate a browser window to open this link, http://localhost:15672, in order to visit the RabbitMQ management page. In the screenshot below you can see how the login page for the website will look.

To log onto the website, we can make use of, guest:guest, which is the default username and password. The homepage of our website for RabbitMQ management is shown in the image below.

We can now take a look at the configurations, channels, exchanges, connections and queues for RabbitMQ after logging in. New login accounts with varied permissions may also be created.

Step 3 – Using Docker Compose

  In above steps of the guide in how to create RabbitMQ Docker Container Image we have utilized the default RabbitMQ definitions which are adequate for learning and certain development situations, but they are insufficient for the majority of real world applications. In practice, we normally need to create exchanges and queues which are predefined, as well as a group of users with certain permissions.

 It’s also recommended to create a definitions file by exporting the RabbitMQ definitions in that file and committing it to a version control system. This can allow us to keep track of definitions we change.

Backup and Export RabbitMQ Definitions

We often use RabbitMQ definitions that have the ability to be customised for various applications. To do this we can configure, for example, an exchange that we have predefined as topic which we can bound to, mytest, which is queue and our routing key can become order created, as depicted by the image below:

We can then proceed to remove the guest user that was et by default and create a user that is an admin, another user that produces messages and another user that consumes those messages.

Upon complete configuration of everything as shown above we are now ready to export our definitions via the clicking a button on the management website’s home page. This button is shown in the screenshot below:

One our RabbitMQ definitions have been exported and downloaded into a definitions file we can name our file, definitions.json, and store it in the rabbit/etc folder which is inside our folder titled solution.

Using Docker Compose to Load and Import Broker Definitions

We will now proceed to write a docker-compose.yml file. The file’s components should be as shown in the image below:

					version: '3.8'

        image: rabbitmq:3-management
        hostname: my-rabbit
            - ./rabbitmq/etc/definitions.json:/etc/rabbitmq/defu
            - ./rabbitmq/etc/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
            - ./rabbitmq/data:/var/lib/rabbitmq/mnesia/rabbit@my-rabbit
            - ./rabbitmq/logs:/var/log/rabbitmq/log
            - 5672:5672
            - 15672:15672

If we want RabbitMQ to load our own definitions, we must include line 8 in the docker-compose.yml file. Our definitions.json file is mounted to the Docker container as defined on line 8.

The volumes on lines 9 to 11 are optional, however they are usually utilised. The database and log files for the RabbitMQ server are located on lines 10 and 11.

In the event that we need to recover the message history, we may save them to our local drive. Note that under Git, these two local directories must be disregarded.

Line 9 refers to the rabbitmq.conf which is a configuration file. The RabbitMQ server and plugins may be set using the rabbitmq.conf configuration file. The configuration file for RabbitMQ 3.7.0  follows the sysctl format.

The RabbitMQ git repository offers a sample rabbitmq.conf file containing most of the configuration options we’ll need, as well as documentation for those options.

Run Docker-Compose up Command

We can quickly start up a RabbitMQ server with our unique definitions and parameters using our docker-compose.yml file.

The command that we will now proceed to use is docker-compose up. Running this command will make it so that our container is downloaded and set up completely as shown below:

You can access the administration Ui by going to http://localhost:15672/ and logging in using the docker-compose specified username and password as shown below, in this example our username is “myuser” and our password is “mypassword”:

Logging into the management website as the admin account is a simple approach to validate the custom definitions as instructed above. The guest user should no longer exist in the RabbitMQ server, and we should be able to see that.

The RabbitMQ instance should be up and running now as shown in the following screenshot:

The benefit of executing these sub-steps in step 3 is that we are now able to start RabbitMQ instances with the same behaviour in multiple contexts and platforms using customised specifications.

How to Create RabbitMQ Docker Container Image Conclusion

RabbitMQ may be used to construct cross language messaging systems since it supports different programming languages. Java, Javascript, Python, PHP, C and C++, Golang, and many more languages are supported.

This article detailed a step by step tutorial to enable you to run RabbitMQ using Docker. And hopefully you have been able to start RabbitMQ from a Docker image. RabbitMQ and Docker are both excellent development tools. 

I hope that this article helped you understand RabbitMQ in more detail and that you’ve successfully been able to execute RabbitMQ on your browser using a Docker container image  as well Docker-Compose. 

After having successfully completed this tutorial you should feel free to go ahead and attempt to implement the RabbitMQ notion in your preferred programming language.

Avatar for Emad Bin Abid
Emad Bin Abid

I'm a software engineer who has a bright vision and a strong interest in designing and engineering software solutions. I readily understand that in today's agile world the development process has to be rapid, reusable, and scalable; hence it is extremely important to develop solutions that are well-designed and embody a well-thought-of architecture as the baseline. Apart from designing and developing business solutions, I'm a content writer who loves to document technical learnings and experiences so that peers in the same industry can also benefit from them.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x