Docker RabbitMQ Container
Are you looking to set up a RabbitMQ container using Docker on your MacOS machine? Look no further! In this article, we'll walk you through the process of creating and configuring a Docker RabbitMQ container, including setting environment variables, starting the container, and testing the broker.
Step 1: Create an Environment File
The first step is to create an environment file that will store our RabbitMQ user password.
.env
DB_PASSWORD=Password1234
This file will be used to set the RABBITMQ_DEFAULT_PASS
environment variable in our Docker container.
Step 2: Create a Docker Compose File
Next, we'll create a Docker compose file that defines our RabbitMQ container.
rabbitmq-docker-compose.yml
version: '3.9'
services:
rabbitmq:
# Apple M1 Chip
# platform: linux/amd64
image: rabbitmq:3.11-management
container_name: rabbitmq
hostname: rabbitmq
restart: always
env_file:
- .env
environment:
RABBITMQ_DEFAULT_USER: rabbitmq
RABBITMQ_DEFAULT_PASS: $DB_PASSWORD
RABBITMQ_DEFAULT_VHOST: my_vhost
ports:
- 5672:5672
- 15672:15672
volumes:
- rabbitmq_datadir:/var/lib/rabbitmq
networks:
- rabbitmq-network
networks:
rabbitmq-network:
driver: bridge
volumes:
rabbitmq_datadir:
This file defines a single service, rabbitmq, which uses the official RabbitMQ 3.11-managemet image and maps port 15672 on your local machine to port 15672 in the container.
Step 3: Start the Container
Now that we have our environment file and Docker compose file set up, it's time to start the container! Run the following command:
docker-compose -f rabbitmq-docker-compose.yml up -d
This will start the container in detached mode, meaning it will run in the background.
Step4: Test RabbitMQ Container
Now we will create a queue and some messages.
Note: you should have rabbitmqadmin Client installed locally.
db_password=Password1234
rabbitmqadmin -H 127.0.0.1 -P 15672 -V my_vhost -u rabbitmq -p ${db_password} list vhosts
rabbitmqadmin -H 127.0.0.1 -P 15672 -V my_vhost -u rabbitmq -p ${db_password} declare queue name=my-new-queue durable=true
rabbitmqadmin -H 127.0.0.1 -P 15672 -V my_vhost -u rabbitmq -p ${db_password} publish exchange=amq.default routing_key=my-new-queue payload="hello, world" properties="{\"delivery_mode\":2}"
Test the queue & messages -
rabbitmqadmin -H 127.0.0.1 -P 15672 -V my_vhost -u rabbitmq -p ${db_password} list queues
rabbitmqadmin -H 127.0.0.1 -P 15672 -V my_vhost -u rabbitmq -p ${db_password} get queue=my-new-queue ackmode=ack_requeue_false
Also we can access RabbitMQ management console UI from browser at http://127.0.0.1:15672 with username rabbitmq and corresponding password.
Step 5: Stop the Container
Finally, we can stop the container by running the following command:
docker-compose -f rabbitmq-docker-compose.yml down
This will stop the container and remove it from memory.
That's it! We've successfully created and configured a Docker RabbitMQ container.