Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Deploying RabbitMQ With Docker

Below is a BASH script you can use to deploy your own RabbitMQ server using docker. There are many different ways to configure your deployment, but this is a broad generic method that will:

  • Deploy with the web interface to manage your server.
  • Specify a username and password for managing the server.
  • Use a volume to keep state across containers being created/destroyed.
  • Uses port 80 for the web interface so that you can just go to the hostname in your browser without having to enter a port number.

Server Requirements

#!/bin/bash

# Settings
CONTAINER_NAME="rabbitmq-container"
DEFAULT_USER="root"
DEFAULT_PASSWORD="myPassword"
CLUSTER_COOKIE="hello world"
HOSTNAME="rabbitmq.mydomain.com"

# Don't change below this line.
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
docker kill $CONTAINER_NAME
docker rm $CONTAINER_NAME

docker run -d \
  --restart=always \
  --hostname $HOSTNAME \
  --name $CONTAINER_NAME \
  -p 80:15672 \
  -p 4369:4369 \
  -p 5671:5671 \
  -p 5672:5672 \
  -p 15671:15671 \
  -p 25672:25672 \
  -e RABBITMQ_ERLANG_COOKIE="$CLUSTER_COOKIE" \
  -e RABBITMQ_DEFAULT_USER="$DEFAULT_USER" \
  -e RABBITMQ_DEFAULT_PASS="$DEFAULT_PASSWORD" \
  -v $DIR/rabbitmq-state:/var/lib/rabbitmq \
  rabbitmq:3-management

Docker Compose

If you wish to deploy through Docker Compose, then here is an example file:

version: "3"

services:
  rabbitmq:
    image: rabbitmq:3-management
    container_name: rabbitmq
    restart: always
    ports:
      - "80:15672"
      - "4369:4369"
      - "5671:5671"
      - "5672:5672"
      - "15671:15671"
      - "25672:25672"
    volumes:
      - rabbitmq-data:/var/lib/rabbitmq
    environment:
      - RABBITMQ_ERLANG_COOKIE=${CLUSTER_COOKIE}
      - RABBITMQ_DEFAULT_USER=${DEFAULT_USER}
      - RABBITMQ_DEFAULT_PASS=${DEFAULT_PASSWORD}

volumes:
  rabbitmq-data:
    driver: local

Just be sure to create a .env file with the following variables (filling in the values).

COMPOSE_PROJECT_NAME="rabbitmq"

CLUSTER_COOKIE=""
DEFAULT_USER=""
DEFAULT_PASSWORD=""
Last updated: 2nd February 2023
First published: 16th August 2018