I have a Dockerfile in which I install RabbitMQ and perform some configuration (e.g. creating a vhost and exchange). Unfortunately when I start a container from that Dockerfile, the configuration is not available and I have only a blank RabbitMQ instance. How can I preconfigure RabbitMQ in a Dockerfile? Here is my current Dockerfile:
FROM node:16-bullseye
# install rabbitmq
RUN apt-get update && apt-get install -y erlang rabbitmq-server
# enable default rabbitmq plugins
RUN rabbitmq-plugins enable rabbitmq_management
RUN service rabbitmq-server start \
# create new vhost
&& rabbitmqctl add_vhost test-vhost \
&& rabbitmqctl set_permissions -p "test-vhost" "guest" ".*" ".*" ".*" \
# create exchange and queue
&& rabbitmqadmin declare exchange --vhost=test-vhost name=test-ex type=fanout durable=true \
&& rabbitmqadmin declare queue --vhost=test-vhost name=testq-1 durable=true \
&& rabbitmqadmin declare binding --vhost=test-vhost source=test-ex destination_type=queue destination=testq-1 routing_key=route1 \
# stop server
&& service rabbitmq-server stop
CMD rabbitmq-server start
Specifying my config via CMD
did the trick:
FROM node:16-bullseye
# install rabbitmq
RUN apt-get update && apt-get install -y erlang rabbitmq-server
# enable default rabbitmq plugins
RUN rabbitmq-plugins enable rabbitmq_management
CMD service rabbitmq-server start \
# create new vhost and admin user
&& rabbitmqctl add_vhost test-vhost \
&& rabbitmqctl add_user admin admin \
&& rabbitmqctl set_user_tags admin administrator \
&& rabbitmqctl set_permissions -p "test-vhost" "guest" ".*" ".*" ".*" \
&& rabbitmqctl set_permissions -p "test-vhost" "admin" ".*" ".*" ".*" \
# create exchange and queue
&& rabbitmqadmin declare exchange --vhost=test-vhost name=test-ex type=fanout durable=true \
&& rabbitmqadmin declare queue --vhost=test-vhost name=testq-1 durable=true \
&& rabbitmqadmin declare binding --vhost=test-vhost source=test-ex destination_type=queue destination=testq-1 routing_key=route1 \
&& /bin/bash