Currently i am starting RabbitMQ Docker container using the default RabbitMQ image from DockerHub. Using the following commands.
docker run --restart=always \
-d \
-e RABBITMQ_NODENAME=rabbitmq \
-v /opt/docker/rabbitmq/data:/var/lib/rabbitmq/mnesia/rabbitmq \
-p 5672:5672 \
-p 15672:15672 \
--name rabbitmq rabbitmq:3-management
I have a need where i want to provide defaults users / and virtual-hosts when the image is first started. For example to create a default 'test-user'.
Currently i have to do that manually by using the management plugin and adding the users / virtual-hosts via the web ui. Is there a way i can provide default settings when starting the RabbitMQ image?
Came up with a solution that suits my needs, leaving it here in case anybody else needs it.
Summary
The idea is to take a standard rabbitmq container with management plugin enabled and use it to create the required configuration, then export and use it to start new containers. The below solution creates a derived docker image but it also works to just mount the two files at runtime (e.g. using docker compose).
References
Components
custom image based on the original one, with this Dockerfile (using version 3.6.6):
rabbitmq.config example:
definitions.json example:
Alternave version
Deriving a new docker image is just one solution and works best when portability is key, since it avoids including host-based file management in the picture.
In some situations using the official image and providing configuration files from storage local to the host might be preferred.
The rabbitmq.config and definitions.json files are produced the same way, then mounted at runtime.
Notes:
docker run example:
docker compose example:
In my case
sleep 5
solution above did not work because RabbitMQ startup time was much longer and not predictable. Posting solution which waits until RabbitMQ is up and running:Dockerfile
init.sh
config_rabbit.sh