1. Introduction
Are you worried about whether your code works as expected in a message broker like RabbitMQ? Get the confidence you need by testing it out locally. With Docker, setting up a RabbitMQ server on your machine to consume and process messages has never been easier, so let’s jump right into getting started with how to run RabbitMQ in Docker compose.
2. Install Docker
Docker is a container engine written in the go language. It is a platform that allows running applications under a shared configuration that uses Linux kernel features to create containers on top of the operating system.
Getting started with Docker is simple; navigate to the Docker homepage and download Docker for desktop for the chosen operating system. Here I will be using Docker for Mac.
Once downloaded and installed, the docker client has default settings which should be more than enough for this tutorial. However, you may want to change those in the future, so right-click on the Docker mini icon and select preferences -> resources. We can allocate a portion of the system resources that a Docker client can use. If you are using Windows, you must add a wslconfig file, as shown here. The client will restart if any of the resource settings are changed.
3. Configure RabbitMQ Docker Environment
Once the Docker is up, defining the RabbitMQ server configuration is time. In the typical project setup, you will likely use a docker-compose yml file containing the container RabbitMQ configuration. The config file is in yml format and can be used to define and run multi-container Docker applications using simple commands. Here is an example of a file that has a RabbitMQ image taken for the Docker hub:
Aside from the RabbitMQ server, this image includes the RabbitMQ management plugin.
version: '2' services: rabbitmq: image: rabbitmq:3-management-alpine ports: - "15672:15672" - "5672:5672"
The common name for such files is docker-compose.yml, which usually resides in the root project directory. To run docker compose configuration file, we need to run the “docker-compose up” command in the file directory using any command line. That will download the RabbitMQ docker image from the docker images hub and start it in a docker container. Make sure the docker is running on your machine.
At first, it might take a bit longer since we are retrieving the image from the Docker repository. However, when everything is running smoothly, you should end up with something like the picture above – the RabbitMQ instance will have started successfully in the docker container!
If we want to stop the container, we can use “Cntl + Z” or “docker-compose down” to stop all the containers belonging to the compose file.
4. RabbitMQ Server
Assuming everything went well and we have the RabbitMQ container running, we are now able to navigate to the RabbitMQ management UI at http://localhost:15672/ and log in using the default username and password (2x guest):
When configured correctly, our application configuration can detect the RabbitMQ server running. As a result, the RabbitMQ definitions, such as queues and exchanges, will be created automatically if defined inside the application.
5. Summary
In this article, we have looked at how to run RabbitMQ in Docker compose file. Docker has made running RabbitMQ a breeze! All it takes is a button to bring up your local development environment – no need for complex installations or hardware. You can also use Docker Compose files to pull from an endless array of images on the Docker Hub, ready and waiting for deployment in moments.
Daniel Barczak
Daniel Barczak is a software developer with a solid 9-year track record in the industry. Outside the office, Daniel is passionate about home automation. He dedicates his free time to tinkering with the latest smart home technologies and engaging in DIY projects that enhance and automate the functionality of living spaces, reflecting his enthusiasm and passion for smart home solutions.
Leave a Reply