我有一个 Dockerfile,在其中安装 RabbitMQ 并执行一些配置(例如创建虚拟主机和交换)。不幸的是,当我从该 Dockerfile 启动容器时,配置不可用,并且我只有一个空白的 RabbitMQ 实例。如何在 Dockerfile 中预配置 RabbitMQ?这是我当前的 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
通过
CMD
指定我的配置成功了:
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