启动RabbitMQ Docker容器时如何添加初始用户? [英] How to add initial users when starting a RabbitMQ Docker container?
问题描述
当前,我正在使用 DockerHub 中的默认RabbitMQ映像启动RabbitMQ Docker容器。使用以下命令。
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'.
当前,我必须手动使用管理插件并通过网络添加用户/虚拟主机ui。启动RabbitMQ映像时,有什么方法可以提供默认设置吗?
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?
推荐答案
您可以创建扩展功能的简单Dockerfile基本图片并创建一个默认用户。
您需要的Docker文件如下:
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:
FROM rabbitmq
# Define environment variables.
ENV RABBITMQ_USER user
ENV RABBITMQ_PASSWORD user
ENV RABBITMQ_PID_FILE /var/lib/rabbitmq/mnesia/rabbitmq
ADD init.sh /init.sh
RUN chmod +x /init.sh
EXPOSE 15672
# Define default command
CMD ["/init.sh"]
和init.sh:
#!/bin/sh
# Create Rabbitmq user
( rabbitmqctl wait --timeout 60 $RABBITMQ_PID_FILE ; \
rabbitmqctl add_user $RABBITMQ_USER $RABBITMQ_PASSWORD 2>/dev/null ; \
rabbitmqctl set_user_tags $RABBITMQ_USER administrator ; \
rabbitmqctl set_permissions -p / $RABBITMQ_USER ".*" ".*" ".*" ; \
echo "*** User '$RABBITMQ_USER' with password '$RABBITMQ_PASSWORD' completed. ***" ; \
echo "*** Log in the WebUI at port 15672 (example: http:/localhost:15672) ***") &
# $@ is used to pass arguments to the rabbitmq-server command.
# For example if you use it like this: docker run -d rabbitmq arg1 arg2,
# it will be as you run in the container rabbitmq-server arg1 arg2
rabbitmq-server $@
此脚本还会初始化并公开RabbitMQ Webadmin的端口15672。
This script also initialize and expose the RabbitMQ webadmin at port 15672.
这篇关于启动RabbitMQ Docker容器时如何添加初始用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!