RabbitMQ with Docker

Running RabbitMQ in Docker is one of the fastest ways to get a broker up and running locally or in a containerised production environment. Docker eliminates manual Erlang installation, handles process management, and makes environment setup completely reproducible across machines.

Running RabbitMQ with Docker

Pull and start the official RabbitMQ image with the management plugin included:

docker run -d \
  --name rabbitmq \
  -p 5672:5672 \
  -p 15672:15672 \
  rabbitmq:3-management

This command:

  • Runs RabbitMQ in detached mode (-d)
  • Names the container rabbitmq
  • Maps port 5672 (AMQP) and 15672 (Management UI) to the host
  • Uses the 3-management tag which includes the Management plugin pre-enabled

Open http://localhost:15672 and log in with guest / guest.

Available Docker Image Tags

rabbitmq:latest          – Latest release, no management plugin
rabbitmq:3-management    – Latest 3.x with management plugin
rabbitmq:3.12-management – Specific minor version with management
rabbitmq:alpine          – Smaller Alpine Linux base image

Always pin to a specific version in production (rabbitmq:3.12-management) rather than using latest to avoid unexpected upgrades.

Setting Environment Variables

Customise the default user, password, and vhost via environment variables:

docker run -d \
  --name rabbitmq \
  -p 5672:5672 \
  -p 15672:15672 \
  -e RABBITMQ_DEFAULT_USER=admin \
  -e RABBITMQ_DEFAULT_PASS=SecurePass123 \
  -e RABBITMQ_DEFAULT_VHOST=/production \
  rabbitmq:3-management

Persisting Data with Volumes

By default, Docker containers lose all data when they stop. Mount a volume to persist RabbitMQ's data directory:

docker run -d \
  --name rabbitmq \
  -p 5672:5672 \
  -p 15672:15672 \
  -v rabbitmq-data:/var/lib/rabbitmq \
  -e RABBITMQ_DEFAULT_USER=admin \
  -e RABBITMQ_DEFAULT_PASS=SecurePass123 \
  rabbitmq:3-management

The named volume rabbitmq-data preserves all queues, messages, users, and vhosts across container restarts and recreations.

Custom Configuration via rabbitmq.conf

Mount a custom configuration file into the container:

# rabbitmq.conf (on your host machine)
default_user = admin
default_pass = SecurePass123
vm_memory_high_watermark.relative = 0.6
disk_free_limit.absolute = 2GB
cluster_partition_handling = pause_minority
docker run -d \
  --name rabbitmq \
  -p 5672:5672 \
  -p 15672:15672 \
  -v $(pwd)/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf \
  -v rabbitmq-data:/var/lib/rabbitmq \
  rabbitmq:3-management

Using Docker Compose

Docker Compose is the recommended approach for local development environments with multiple services:

# docker-compose.yml
version: '3.8'

services:
  rabbitmq:
    image: rabbitmq:3-management
    container_name: rabbitmq
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: admin
      RABBITMQ_DEFAULT_PASS: SecurePass123
      RABBITMQ_DEFAULT_VHOST: /
    volumes:
      - rabbitmq-data:/var/lib/rabbitmq
      - ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
    healthcheck:
      test: ["CMD", "rabbitmq-diagnostics", "ping"]
      interval: 30s
      timeout: 10s
      retries: 5
    restart: unless-stopped

  app:
    build: .
    depends_on:
      rabbitmq:
        condition: service_healthy
    environment:
      RABBITMQ_HOST: rabbitmq
      RABBITMQ_PORT: 5672

volumes:
  rabbitmq-data:

Start everything with:

docker-compose up -d

The healthcheck and depends_on: condition: service_healthy ensure the application container waits for RabbitMQ to be fully ready before starting.

Enabling Plugins in Docker

Create a enabled_plugins file and mount it into the container:

# enabled_plugins (no extension, plain text)
[rabbitmq_management,rabbitmq_prometheus,rabbitmq_shovel,rabbitmq_shovel_management].
docker run -d \
  --name rabbitmq \
  -p 5672:5672 \
  -p 15672:15672 \
  -p 15692:15692 \
  -v $(pwd)/enabled_plugins:/etc/rabbitmq/enabled_plugins \
  rabbitmq:3.12-management

Running a RabbitMQ Cluster with Docker Compose

version: '3.8'

services:
  rabbitmq1:
    image: rabbitmq:3-management
    hostname: rabbitmq1
    environment:
      RABBITMQ_ERLANG_COOKIE: "SHARED_SECRET_COOKIE"
      RABBITMQ_DEFAULT_USER: admin
      RABBITMQ_DEFAULT_PASS: SecurePass123
    ports:
      - "5672:5672"
      - "15672:15672"

  rabbitmq2:
    image: rabbitmq:3-management
    hostname: rabbitmq2
    environment:
      RABBITMQ_ERLANG_COOKIE: "SHARED_SECRET_COOKIE"
    depends_on:
      - rabbitmq1

  rabbitmq3:
    image: rabbitmq:3-management
    hostname: rabbitmq3
    environment:
      RABBITMQ_ERLANG_COOKIE: "SHARED_SECRET_COOKIE"
    depends_on:
      - rabbitmq1

After starting, join nodes 2 and 3 to node 1:

docker exec rabbitmq2 rabbitmqctl stop_app
docker exec rabbitmq2 rabbitmqctl reset
docker exec rabbitmq2 rabbitmqctl join_cluster rabbit@rabbitmq1
docker exec rabbitmq2 rabbitmqctl start_app

docker exec rabbitmq3 rabbitmqctl stop_app
docker exec rabbitmq3 rabbitmqctl reset
docker exec rabbitmq3 rabbitmqctl join_cluster rabbit@rabbitmq1
docker exec rabbitmq3 rabbitmqctl start_app

Useful Docker Commands for RabbitMQ

# View logs
docker logs rabbitmq

# Follow logs in real time
docker logs -f rabbitmq

# Run rabbitmqctl inside the container
docker exec rabbitmq rabbitmqctl list_queues

# Stop and remove the container
docker stop rabbitmq && docker rm rabbitmq

# Remove the data volume (wipes all data)
docker volume rm rabbitmq-data

Summary

Docker makes RabbitMQ setup instant and reproducible. Use the rabbitmq:3-management image to get the broker and Management UI in one container. Set credentials via environment variables. Mount a volume to /var/lib/rabbitmq for persistent data. Use Docker Compose to coordinate RabbitMQ with your application services and configure health checks so dependent services wait for the broker. For clustering, share the same Erlang cookie across containers and join nodes after startup.

Leave a Comment

Your email address will not be published. Required fields are marked *