Setup RabbitMQ with Docker Compose

This tutorial will show you how to setup RabbitMQ with Docker and docker-compose.

RabbitMQ is the most widely deployed open source message broker. RabbitMQ is lightweight and easy to deploy on premises and in the cloud. It supports multiple messaging protocols.Most organizations use RabbitMQ to send and receive messages to communicate between different application/services

For development purpose we can use Docker to spin up a local instance of the container and connect to it from our programming language of choice.

Using Docker command.

The easiest way to do this is to use the official RabbitMQ Docker image. Just use the official image in your Dockerfile and you’re good to go.

docker run -d --hostname my-rabbitmq --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.10.6-management-alpine
Code language: plaintext (plaintext)

Rabbitmq listens on port no 5672 and manage console is open on 15672.

Note

rabbitmq official images comes in many variants. Please see Image Variants section.

I am using image with management features enabled.

Using with Docker-Compose

version: '3' services: rabbitmq: container_name: rabbitmq image: rabbitmq:3.10.6-management-alpine hostname: my-rabbitmq ports: - 5672:5672 - 15672:15672
Code language: Java (java)

we start the RabbitMQ container with following command

docker-compose up
Code language: plaintext (plaintext)

Once the container starts we can access the management console by navigating to http://localhost:15672

The default username/password is guest/guest

You can change default username/password with environment varibeles.

version: '3' services: rabbitmq: container_name: rabbitmq image: rabbitmq:3.10.6-management-alpine environment: - RABBITMQ_DEFAULT_USER=admin - RABBITMQ_DEFAULT_PASS=admin ports: - 5672:5672 - 15672:15672 hostname: my-rabbitmq
Code language: Dockerfile (dockerfile)

If you want to persist the storage, you can also create volume.

version: '3' services: rabbitmq: container_name: rabbitmq image: rabbitmq:3.10.6-management-alpine hostname: my-rabbitmq volumes: - './rabbitmq:/var/lib/rabbitmq/mnesia' ports: - 5672:5672 - 15672:15672
Code language: Dockerfile (dockerfile)

If you want to run with docker command

docker run -d --hostname my-rabbitmq --name rabbitmq -p 5672:5672 -p 15672:15672 -v ./rabbit:/var/lib/rabbitmq rabbitmq:3.10.6-management-alpine
Code language: plaintext (plaintext)

Image Variants

The rabbitmq images come in many flavors, each designed for a specific use case.

Default Image

rabbitmq:<version>

This is the defacto image. If you are unsure about what your needs are, you probably want to use this one. It is designed to be used both as a throw away container (mount your source code and start the container to start your app), as well as the base to build other images off of.

Management Image

rabbitmq:<version>management

This is the image which contains management plugin installed and enabled by default, which is available on the standard management port of 15672, with the default username and password of guest / guest

Alpine Image

rabbitmq:<version>-alpine

rabbitmq:<version>managementalpine

This image is based on the popular Alpine Linux project, available in the alpine official image. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general.

This option is appropriate when the final picture size is the most important consideration. The key disadvantage is that it uses musl libc rather than glibc and friends, therefore applications will frequently encounter troubles depending on the complexity of their libc requirements/assumptions.

Custom Image

You can also create custom image by using one of the above image as the base image and enabling the plugins at tuntime.

FROM rabbitmq:3.8-management RUN rabbitmq-plugins enable --offline rabbitmq_mqtt rabbitmq_federation_management rabbitmq_stomp
Code language: Dockerfile (dockerfile)

Troubleshooting

Cookie file /var/lib/rabbitmq/.erlang.cookie must be accessible by owner only
Code language: plaintext (plaintext)

When you are mapping rabbitmq volume to /var/lib/rabbitmq you might see above error.

To overcome the issue you can point to volume to /var/lib/rabbitmq/mnesia

You can download all docker compose file from GitHub

References

https://hub.docker.com/_/rabbitmq

https://github.com/docker-library/rabbitmq/issues/171

Similar Posts