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.
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).
custom image based on the original one, with this Dockerfile (using version 3.6.6):
FROM rabbitmq:3.6.6-management
ADD rabbitmq.config /etc/rabbitmq/
ADD definitions.json /etc/rabbitmq/
RUN chown rabbitmq:rabbitmq /etc/rabbitmq/rabbitmq.config /etc/rabbitmq/definitions.json
CMD ["rabbitmq-server"]
rabbitmq.config example:
[
{rabbit, [
{loopback_users, []}
]},
{rabbitmq_management, [
{load_definitions, "/etc/rabbitmq/definitions.json"}
]}
].
definitions.json example:
{
"rabbit_version": "3.6.6",
"users": [
{
"name": "user1",
"password_hash": "pass1",
"hashing_algorithm": "rabbit_password_hashing_sha256",
"tags": ""
},
{
"name": "adminuser",
"password_hash": "adminpass",
"hashing_algorithm": "rabbit_password_hashing_sha256",
"tags": "administrator"
}
],
"vhosts": [
{
"name": "\/vhost1"
},
{
"name": "\/vhost2"
}
],
"permissions": [
{
"user": "user1",
"vhost": "\/vhost1",
"configure": ".*",
"write": ".*",
"read": ".*"
}
],
"parameters": [],
"policies": [],
"queues": [],
"exchanges": [],
"bindings": []
}
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 run --rm -it \
-v /etc/so/rabbitmq.config:/etc/rabbitmq/rabbitmq.config:ro \
-v /etc/so/definitions.json:/etc/rabbitmq/definitions.json:ro \
rabbitmq:3.6-management
docker compose example:
version: '2.1'
services:
rabbitmq:
image: "rabbitmq:3.6-management"
ports:
- 5672:5672
- 15672:15672
volumes:
- /etc/so/rabbitmq.config:/etc/rabbitmq/rabbitmq.config:ro
- /etc/so/definitions.json:/etc/rabbitmq/definitions.json:ro