- Linux Machine
- Docker installed.
- Docker compose installed.
Setting up Docker Compose
If you want to make your code more portable and share the same version of RabbitMQ with your developer colleagues, I highly recommend using Docker.
Create a docker-compose.yml file into your project root folder.
version: "3.0" services: rabbitmq: image: rabbitmq:3.8.14-management container_name: 'project-rabbitmq' restart: always ports: - 5672:5672 - 15672:15672
Install and up rabbitmq container.
docker-compose up -d
That’s it. This configuration will take a pull of rabbitmq:3.8.14-managment image from docker hub and install on your machine.
You can access your rabbitm mq ui from http://localhost:15672. Default user and password would be guest, guest.
What is the issue with above container?
As per above, rabbitmq container is up and running file. You would also able to connect ui and create queue, publish message and consume message with rabbitmq consumer.
But there is issue, The Data Persistent issue. Means if you recreate a container with above configuration then the new container will be up and your old data will be lost , that could me major issue as per you application structure.
How to fix data persistent issue?
So as per the document statement on official rabbitmq website is:
One of the important things to note about RabbitMQ is that it stores data based on what it calls the “Node Name”, which defaults to the hostname. What this means for usage in Docker is that we should specify
--hostnameexplicitly for each daemon so that we don’t get a random hostname and can keep track of our data:
Means whenever we are run up command in docker-compose, it will create a new container with random hostname and linked to default network. And the old container will be erased with its data. So this is we need to fix.
Here is updated docker-compose.yml file that would fix this issue for you.
version: "3.0" services: rabbitmq: hostname: 'dev-rabbitmq' image: rabbitmq:3.8.14-management container_name: 'project-rabbitmq' restart: always ports: - 5672:5672 - 15672:15672 volumes: - '~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/mnesia/' networks: - test-network networks: test-network: driver: bridge
- image: where we tell Docker which image to pull. We’re using an Alpine implementation of RabbitMQ with the
managementplugin. The Alpine distro is the one you’ll want to use if you want to save disk space.
- container_name: this represents the container created from the image above.
- ports: the list of ports that will be mapped from the container to the outside world, for interacting with the queue and the web UI.
- volumes: where we map the
datafrom the container to our local folder. This allows us to view the files directly in their local folder structure instead of having to connect to the container.
- networks: where we specify the network’s name that the container will be using. This helps to separate container network configurations.
- hostname: hostname of the container so, that it would get random name for each creation.
docker-compose up -d
This configuration would resolve your issue. Please check and let us if you still facing issue on comment section.