Skip to main content
Docker

Easy RabbitMQ Deployment with Docker Compose: A Step-by-Step Guide

In this blog post, I'll show you how to deploy RabbitMQ using Docker Compose, a simple and efficient way to set up a RabbitMQ server. Learn how to configure RabbitMQ and scale it up or down depending on your needs.

Christian Schou

RabbitMQ is a message broker that enables efficient communication between different applications or services. It can be used to facilitate asynchronous communication between microservices, distribute workload across different workers, and provide reliable message delivery in complex distributed systems. Docker is a popular platform for packaging, distributing, and running applications in containers. Docker Compose is a tool for defining and running multi-container Docker applications.

Using Docker Compose to deploy RabbitMQ makes it easy to spin up a RabbitMQ instance quickly and efficiently, without worrying about the underlying infrastructure. It also makes it easy to scale the RabbitMQ instance and manage the application's configuration. In this step-by-step guide, we will show you how to deploy RabbitMQ using Docker Compose. Whether you are new to RabbitMQ, Docker, or both, this guide will provide you with everything you need to know to get started.

Messaging that just works — RabbitMQ

Prerequisites

Before you can deploy RabbitMQ using Docker Compose, you will need to have a few things in place. Here are the prerequisites for this tutorial:

  1. Docker - You will need to have Docker installed on your system. If you don't have Docker installed already, you can download it from the official Docker website. Be sure to download the version that is appropriate for your operating system.
  2. Docker Compose - You will also need to have Docker Compose installed on your system. Docker Compose comes pre-installed with Docker Desktop for Windows and macOS, but if you are using Linux or if you need to install it separately, you can follow the instructions on the Docker Compose documentation page for Linux.
  3. Text editor - You will need a text editor to create and edit the Docker Compose file. You can use any text editor of your choice, such as Sublime Text, VS Code, or Notepad++.

Once you have these prerequisites in place, you will be ready to create and deploy a RabbitMQ instance using Docker Compose.

Writing the Docker Compose file

To deploy RabbitMQ with Docker Compose, we first need to create a Docker Compose file. The Docker Compose file is a YAML file that defines the services, networks, and volumes that make up our application.

Here is an example Docker Compose file that deploys a RabbitMQ instance.

version: '3'

services:
  rabbitmq:
    image: rabbitmq:management
    container_name: rabbitmq
    environment:
      - RABBITMQ_DEFAULT_USER=guest
      - RABBITMQ_DEFAULT_PASS=guest
    ports:
      - "5672:5672"
      - "15672:15672"

networks:
  default:
    driver: bridge

Let's go through each section of this file to understand what it does:

  • version: '3': This specifies the version of the Docker Compose file format that we are using.
  • services: This section defines the RabbitMQ service that we want to deploy. In this case, we are using the official RabbitMQ Docker image with the management plugin enabled.
  • image: rabbitmq:management: This specifies the RabbitMQ Docker image we want to use.
  • container_name: rabbitmq: This assigns a name to the RabbitMQ container.
  • environment: This section sets environment variables for the RabbitMQ container. In this example, we are setting the default username and password to "guest". Note that this is not recommended for production environments.
  • ports: This section maps the ports used by RabbitMQ to the corresponding ports on the host machine. In this case, we are mapping port 5672 for AMQP communication and port 15672 for the RabbitMQ management interface.
  • networks: This section specifies the network settings for the RabbitMQ container. In this example, we are using the default Docker bridge network.

Once you have saved this Docker Compose file, you can use the docker-compose up command to start the RabbitMQ container. The RabbitMQ management interface will be accessible at http://localhost:15672 and you can log in with the default credentials (guest/guest).

Configuring RabbitMQ

Once you have deployed RabbitMQ using Docker Compose, you can configure it to suit your needs. RabbitMQ provides a web-based management interface that you can use to configure various aspects of the RabbitMQ server.

To access the management interface, open a web browser and navigate to the IP address or domain name of your RabbitMQ instance followed by the management port number (by default, 15672). For example, if your RabbitMQ instance is running on the same machine as your web browser, you can access the management interface by navigating to http://localhost:15672.

Once you have accessed the management interface, you can log in with the default credentials (guest/guest) or with the username and password that you specified in the RABBITMQ_DEFAULT_USER and RABBITMQ_DEFAULT_PASS environment variables in your Docker Compose file.

From the management interface, you can perform various tasks such as creating and deleting queues, exchanges, and users. You can also view statistics about the RabbitMQ server and manage connections and channels.

Note that for security reasons, it is recommended to change the default username and password for the RabbitMQ server and to restrict access to the management interface to trusted users and IP addresses. You can find more information about securing RabbitMQ in the official RabbitMQ documentation.

In addition to the web-based management interface, you can also configure RabbitMQ using configuration files. By default, RabbitMQ reads its configuration from a file named rabbitmq.config in the /etc/rabbitmq/ directory. You can create a custom configuration file and mount it as a volume in your Docker Compose file to configure RabbitMQ. You can find more information about RabbitMQ configuration in the official RabbitMQ documentation.

A configuration example of rabbitmq.config

Here is an example rabbitmq.config file that you can use to configure RabbitMQ.

Configuration — RabbitMQ