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?
I had to make a few changes to the script in the accepted answer to get it working based on the comments above.
Dockerfile
init.sh
I would like to add that sudo's response helped me a lot. But that it still missed a command to be added to the Dockerfile.
The rabbitmq.config and definitions.json file should be owned by the rabbitmq user & group. So after adding the files run chown.
The full Dockerfile in my case was the following:
The
rabbitmq.config
file has the following content being a merge from the default image's config and the added definitions loading:The definitions file can be exported from the management interface in the overview tab.
So you would first create a normal 'empty' rabbitmq container. Define whatever users, exchanges and queues you like. Then enter the management interface, export the definitions and create your own image using the file as described above.
Downloading the definitions is the easiest way to get the right password hashes in the definitions file for your own passwords. If you do not wish to do that you should follow the instructions as noted here (https://www.rabbitmq.com/passwords.html) to generate the correct hashes.
The newest version of the RabbitMQ image on Dockerhub has in-built functionality for changing the default username / password from "guest" / "guest" to something else.
Simply set the environment variables "RABBITMQ_DEFAULT_USER" and "RABBITMQ_DEFAULT_PASS" when starting the image.
As a docker command, you would run the image like this:
You can create a simple Dockerfile that extends the functionality of the basic image and creates a default user. The Docker file you need is the following:
And the init.sh:
This script also initialize and expose the RabbitMQ webadmin at port 15672.
Here is an example of how I add an unprivileged user gg
RUN useradd -d /home/gg -m -s /bin/bash gg RUN echo gg:gg | chpasswd RUN echo 'gg ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers.d/gg RUN chmod 0440 /etc/sudoers.d/gg
You can create a new image and use the rabbitmqctl command line.
For example using this Dockerfile:
And build the image using
I have not test my answer, I cannot use docker at work