How to run RabbitMQ in Docker
Docker gives you a way to run RabbitMQ without installing and configuring it directly on your machine or server. You get RabbitMQ in an isolated container, which keeps setup, testing, and teardown simple. This guide walks through running RabbitMQ in Docker, from a single node to a small cluster, using the message broker that most teams reach for first.
When Docker makes sense for RabbitMQ
Running RabbitMQ in Docker fits development and testing well. You can spin up an instance in seconds, throw it away when you are done, and avoid touching the packages on your host. For production you will want persistent volumes and a real cluster, both covered below.
Running RabbitMQ in Docker step by step
Prerequisites
- Docker installed on your machine. If you haven't installed Docker, you can download it from Docker's website.
Step 1: Pull the RabbitMQ image
Pull the official RabbitMQ image:
docker pull rabbitmq:3-management
This command pulls the RabbitMQ image with the management plugin enabled, which provides a web-based UI for managing your RabbitMQ server.
Step 2: Run a RabbitMQ container
- Start a RabbitMQ instance:
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
-druns the container in detached mode.--nameassigns a name to the container.-pmaps the ports from the container to your host machine. Port5672is for RabbitMQ server, and15672is for the management UI.
- Access the RabbitMQ Management Console:
- Open a web browser and navigate to
http://localhost:15672/. - Log in with the default username
guestand passwordguest.
Step 3: Configure RabbitMQ
You can mount a custom configuration file from your host to the container.
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 -v /path/to/your/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf rabbitmq:3-management
Step 4: Set up a RabbitMQ cluster
- Create a Docker network:
docker network create rabbitmq_cluster
- Start RabbitMQ instances on the network:
docker run -d --name rabbitmq1 --hostname rabbitmq1 --network rabbitmq_cluster rabbitmq:3-management
docker run -d --name rabbitmq2 --hostname rabbitmq2 --network rabbitmq_cluster rabbitmq:3-management
- Cluster the nodes:
First, access the shell of one of the containers:
docker exec -it rabbitmq1 bash
Then, cluster the nodes using rabbitmqctl:
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@rabbitmq2
rabbitmqctl start_app
Exit the container shell and repeat for other nodes if necessary. This gives you a working cluster for local testing; for a production-ready setup with quorum queues and proper node discovery, follow the dedicated RabbitMQ cluster setup guide.
- Verify the cluster status:
Access the shell of one of the nodes as before and run:
rabbitmqctl cluster_status
Step 5: Persist data
To persist data, you can mount a volume to the RabbitMQ container.
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 -v /path/to/data:/var/lib/rabbitmq rabbitmq:3-management
Moving to production
The steps above get RabbitMQ running quickly, which is exactly what you want for development and testing. Before you run the same setup in production, plan for persistent volumes, a multi-node cluster, and a way to monitor queue depth and node health so you catch problems before they page you. Treat the single-container setup as a starting point, not a production deployment.