How To Set Up A RabbitMQ Server In Docker

dimitrilc 2 Tallied Votes 279 Views Share

Introduction

AMQP (Advanced Message Queuing Protocol) is a popular protocol used for communication between messaging middleware and clients. At the time of this writing, the latest version of the AMQP is 1.0, but we will focus on version 0-9-1 instead. 0-9-1 is the default version shipped with the popular AMQP Broker (server) implementation RabbitMQ.

In this tutorial, we will learn how to install and set up our own RabbitMQ server using Docker.

Goals

At the end of the tutorial, you would have learned:

  1. How to install RabbitMQ server with Docker.

Tools Required

  1. A computer that is known to work well with Docker Desktop, which should support hardware virtualization.
  2. If you are familiar with Docker Engine itself, and you are on Linux without hardware virtualization, then you can still attempt to follow this tutorial via CLI.

Prerequisite Knowledge

  1. Docker Desktop Basics.
  2. Basic Linux Administration.

Project Setup

To follow along with the tutorial, perform the steps below:

  1. If you do not already have it, install Docker Desktop on your computer.
    Start Docker Desktop and navigate to the Dashboard.

Screen_Shot_2022-09-06_at_3.47.32_PM.png

Install The Official RabbitMQ Image

First, we will install RabbitMQ using the official Docker image from Docker Hub. Before installing the image, let us inspect that command that we will use,

docker run -d -h container-hostname --name daniweb-rabbit -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=password -p 80:15672 -p 5672:5672 rabbitmq:3-management

You can find the breakdown of the command above in the following list:

  1. docker run: run a command in a new container.
  2. -d (--detatch): run the container in the background and prints the container ID.
  3. -h container-hostname (–hostname): sets the container’s Linux hostname to container-hostname.
  4. --name daniweb-rabbit: sets the container’s name to daniweb-rabbit. This is useful if you want the container to have a meaningful name. This is optional. If you do not set a name, then Docker will generate one for you.
  5. -e RABBITMQ_DEFAULT_USER=user: assigns the value user to the environment variable RABBITMQ_DEFAULT_USER, which RabbitMQ will then use to create the default user.
  6. -e RABBITMQ_DEFAULT_PASS=password: similarly, set the password for the default user to password.
  7. -p 80:15672 (--publish): binds port 15672 on the container to port 80 on the host (your computer). The port 15672 is used for the management interface, which we will look at later.
  8. -p 5672:5672 (--publish): binds port 5672 on the container to port 5672 on the host. This port is used by default for non-TLS connection from the client side.
  9. rabbitmq:3-management: finally, this is the docker image that we are trying to use. The default image is called rabbitmq, but we used the 3-management tag to specify that we want the variant with the management console instead.

After running the command, you should have received the ID for the container (because of the -d flag). Mine looks like this, but yours will be a different string.

2c0ba8b9a2eaa99e20411c9ae47c93db0cfd92a7274e0d940432afe6c93c5a38

View The Container In Docker Desktop Dashboard

After you have completed the previous step, the container should show up when you switch back to Docker Desktop’s Dashboard, under Containers.

Screen_Shot_2022-09-06_at_9.39.32_PM.png

If you select the image name rabbit:3-management (in blue), then you can see the commands that are executed on your container to set up your RabbitMQ server. The commands here are managed by the dockerfile of the image.

Screen_Shot_2022-09-06_at_9.52.05_PM.png

Alternatively, selecting the image name daniweb-rabbit will let you see the application logs and inspect the Linux environment for the container.

Screen_Shot_2022-09-06_at_10.28.54_PM.png

Remember the username, password, and port forwarding that we specified earlier in the tutorial? You can see them in the Inspect tab now.

Screen_Shot_2022-09-06_at_10.31.38_PM.png

Screen_Shot_2022-09-06_at_10.29.36_PM.png

Everything that you see in the Docker Desktop Dashboard is also available in the Docker CLI tools, if you prefer that instead.

Connect to the RabbitMQ Server

Finally, it is time to connect to our server. You can do this via the Dashboard by selecting the Open with Browser button

Screen_Shot_2022-09-06_at_10.38.18_PM.png

or simply navigating to localhost using a web browser (you can skip the port for port 80).

After the page loads, log in with user/password.

Screen_Shot_2022-09-06_at_10.43.35_PM.png

And then you will be greeted by the management interface.

Screen_Shot_2022-09-06_at_10.42.16_PM.png

Summary

Congratulations, you have learned how to set up your own RabbitMQ server in a Docker container in this tutorial. In the next tutorial, we will discuss how to set up our own exchanges and queues in RabbitMQ.

On Windows (non-WSL) and macOS, Docker Desktop will spin up a full-blown Linux VM, which consumes a vast amount of RAM. Please make sure to stop the container via the Docker Desktop Dashboard, and reboot if necessary, after you have completed the tutorial.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.