Skip to content

Setting up RabbitMQ Message Broker using Docker

This article was originally published on https://medium.com/@fernandochristyanto/setting-up-rabbitmq-message-broker-using-docker-9da4a209bab3

Prerequisites

  • Docker (I use docker for mac)

After making sure you have docker installed, start up the docker daemon (on mac, you do this by opening the docker.app).

I will not explain Docker and RabbitMQ in depth here because they require a topic for their own and goes beyond the scope of this post.

What are we going to achieve here?

We are going to containerize a RabbitMQ process using the rabbitmq:3.7 image, and also create an administrator for monitoring.

Create a folder named rabbitmq-3.7. This will be the folder containing all our docker files, configs and rabbitmq data.

Inside that folder, create a file, name it Dockerfile with the contents same as below. The Dockerfile uses rabbitmq:3.7 image from Dockerhub. It also copies init.sh (which we will create later) into the container root folder and then execute it.

FROM rabbitmq:3.7

WORKDIR /usr/local/bin/
ADD ./init.sh .
RUN chmod +x ./init.sh

CMD ["./init.sh"]

Next, we’d need to create our init.sh. This script will be responsible for enabling plugins and creating users.

#!/usr/bin/env bash
set -e
(
count=0;
# Execute list_users until service is up and running
until timeout 5 rabbitmqctl list_users >/dev/null 2>/dev/null || (( count++ >= 60 )); do sleep 1; done;

# Enable plugins
rabbitmq-plugins enable rabbitmq_management

# Register user
if rabbitmqctl list_users | grep $RABBITMQ_USER > /dev/null
then
  echo "User '$RABBITMQ_USER' already exist, skipping user creation"
else
  echo "Creating user '$RABBITMQ_USER'..."
  rabbitmqctl add_user $RABBITMQ_USER $RABBITMQ_PASSWORD
  rabbitmqctl set_user_tags $RABBITMQ_USER administrator
  rabbitmqctl set_permissions -p / $RABBITMQ_USER ".*" ".*" ".*"
  echo "User '$RABBITMQ_USER' creation completed"
fi
) &

# Call original entrypoint
exec docker-entrypoint.sh rabbitmq-server $@

Lastly, create a file named docker-compose.yml, this enables us to use Docker Compose to quickly get our container up and running without much typing for the long run.

version: "3"
services:
  rabbitmq:
    build: 
      context: .
      dockerfile: Dockerfile
    container_name: "rabbitmq-3.7"
    restart: always
    ports:
    - 5672:5672
    - 15672:15672
    volumes:
    - ./rabbitmq/etc/:/etc/rabbitmq/
    - ./rabbitmq/data/:/var/lib/rabbitmq/
    - ./rabbitmq/logs/:/var/log/rabbitmq/
    environment: 
    - RABBITMQ_DEFAULT_USER=guest
    - RABBITMQ_DEFAULT_PASS=guest
    - RABBITMQ_NODENAME=rabbitnode@localhost
    - RABBITMQ_USER=rabbit
    - RABBITMQ_PASSWORD=rabbit
volumes:
  rabbitmq:

Running our container

Open up terminal and change directory to the rabbitmq-3.7 folder then run docker-compose up.

It will pull rabbitmq:3.7 image from dockerhub, and compose our image as we specified in the docker-compose and Dockerfile. Wait for the server to start, then you should be able to rabbitmq running.