如何在 Docker 容器中配置 rabbitmq.config?
How to configure rabbitmq.config inside Docker containers?
我正在使用官方 RabbitMQ Docker 图像 (https://hub.docker.com/_/rabbitmq/)
我试过在 运行
之后编辑容器内的 rabbitmq.config
文件
docker exec -it <container-id> /bin/bash
不过,这似乎对容器中的rabbitmq服务器运行没有影响。重新启动容器显然也无济于事,因为 Docker 启动了一个全新的实例。
所以我假设为 Docker 容器配置 rabbitmq.config
的唯一方法是在容器启动之前设置它 运行,我可以部分使用图像支持的环境变量。
遗憾的是,并非所有配置选项都受环境变量支持。例如,我想在 rabbitmq.config
中设置 {auth_mechanisms, ['PLAIN', 'AMQPLAIN', 'EXTERNAL']}
。
然后我找到了 RABBITMQ_CONFIG_FILE
环境变量,它应该允许我指向我想用作我的配置文件的文件。但是,我尝试了以下方法但没有成功:
docker service create --name rabbitmq --network rabbitnet \
-e RABBITMQ_ERLANG_COOKIE='mycookie' --hostname = "{{Service.Name}}{{.Task.Slot}}" \
--mount type=bind,source=/root/mounted,destination=/root \
-e RABBITMQ_CONFIG_FILE=/root/rabbitmq.config rabbitmq
默认 rabbitmq.config
文件包含:
[ { rabbit, [ { loopback_users, [ ] } ] } ]
启动后容器中的内容
在 Docker 容器中配置 rabbitmq.config
的最佳方法是什么?
配置文件位于 /etc/rabbitmq/rabbitmq.config
中,所以如果您使用类似这样的东西挂载自己的配置文件(我在这里使用 docker-compose 来设置图像)
volumes:
- ./conf/myrabbit.conf:/etc/rabbitmq/rabbitmq.config
应该这样做
我可以 运行 RabbitMQ 使用以下 bash
脚本安装配置:
#RabbitMQ props
env=dev
rabbitmq_name=dev_rabbitmq
rabbitmq_port=5672
#RabbitMQ container
if [ "$(docker ps -aq -f name=${rabbitmq_name})" ]; then
echo Cleanup the existed ${rabbitmq_name} container
docker stop ${rabbitmq_name} && docker rm ${rabbitmq_name}
echo Create and start new ${rabbitmq_name} container
docker run --name ${rabbitmq_name} -d -p ${rabbitmq_port}:15672 -v $PWD/rabbitmq/${env}/data:/var/lib/rabbitmq:rw -v $PWD/rabbitmq/${env}/definitions.json:/opt/definitions.json:ro -v $PWD/rabbitmq/${env}/rabbitmq.config:/etc/rabbitmq/rabbitmq.config:ro rabbitmq:3-management
else
echo Create and start new ${rabbitmq_name} container
docker run --name ${rabbitmq_name} -d -p ${rabbitmq_port}:15672 -v $PWD/rabbitmq/${env}/data:/var/lib/rabbitmq:rw -v $PWD/rabbitmq/${env}/definitions.json:/opt/definitions.json:ro -v $PWD/rabbitmq/${env}/rabbitmq.config:/etc/rabbitmq/rabbitmq.config:ro rabbitmq:3-management
fi
我的 rabbitmq/dev
目录中还有以下 config
个文件
definitions.json
{
"rabbit_version": "3.7.3",
"users": [{
"name": "welib",
"password_hash": "su55YoHBYdenGuMVUvMERIyUAqJoBKeknxYsGcixXf/C4rMp",
"hashing_algorithm": "rabbit_password_hashing_sha256",
"tags": ""
}, {
"name": "admin",
"password_hash": "x5RW/n1lq35QfY7jbJaUI+lgJsZp2Ioh6P8CGkPgW3sM2/86",
"hashing_algorithm": "rabbit_password_hashing_sha256",
"tags": "administrator"
}],
"vhosts": [{
"name": "/"
}, {
"name": "dev"
}],
"permissions": [{
"user": "welib",
"vhost": "dev",
"configure": ".*",
"write": ".*",
"read": ".*"
}, {
"user": "admin",
"vhost": "/",
"configure": ".*",
"write": ".*",
"read": ".*"
}],
"topic_permissions": [],
"parameters": [],
"global_parameters": [{
"name": "cluster_name",
"value": "rabbit@98c821300e49"
}],
"policies": [],
"queues": [],
"exchanges": [],
"bindings": []
}
rabbitmq.config
[
{rabbit, [
{loopback_users, []},
{vm_memory_high_watermark, 0.7},
{vm_memory_high_watermark_paging_ratio, 0.8},
{log_levels, [{channel, warning}, {connection, warning}, {federation, warning}, {mirroring, info}]},
{heartbeat, 10}
]},
{rabbitmq_management, [
{load_definitions, "/opt/definitions.json"}
]}
].
我正在使用官方 RabbitMQ Docker 图像 (https://hub.docker.com/_/rabbitmq/)
我试过在 运行
之后编辑容器内的rabbitmq.config
文件
docker exec -it <container-id> /bin/bash
不过,这似乎对容器中的rabbitmq服务器运行没有影响。重新启动容器显然也无济于事,因为 Docker 启动了一个全新的实例。
所以我假设为 Docker 容器配置 rabbitmq.config
的唯一方法是在容器启动之前设置它 运行,我可以部分使用图像支持的环境变量。
遗憾的是,并非所有配置选项都受环境变量支持。例如,我想在 rabbitmq.config
中设置 {auth_mechanisms, ['PLAIN', 'AMQPLAIN', 'EXTERNAL']}
。
然后我找到了 RABBITMQ_CONFIG_FILE
环境变量,它应该允许我指向我想用作我的配置文件的文件。但是,我尝试了以下方法但没有成功:
docker service create --name rabbitmq --network rabbitnet \
-e RABBITMQ_ERLANG_COOKIE='mycookie' --hostname = "{{Service.Name}}{{.Task.Slot}}" \
--mount type=bind,source=/root/mounted,destination=/root \
-e RABBITMQ_CONFIG_FILE=/root/rabbitmq.config rabbitmq
默认 rabbitmq.config
文件包含:
[ { rabbit, [ { loopback_users, [ ] } ] } ]
启动后容器中的内容
在 Docker 容器中配置 rabbitmq.config
的最佳方法是什么?
配置文件位于 /etc/rabbitmq/rabbitmq.config
中,所以如果您使用类似这样的东西挂载自己的配置文件(我在这里使用 docker-compose 来设置图像)
volumes:
- ./conf/myrabbit.conf:/etc/rabbitmq/rabbitmq.config
应该这样做
我可以 运行 RabbitMQ 使用以下 bash
脚本安装配置:
#RabbitMQ props
env=dev
rabbitmq_name=dev_rabbitmq
rabbitmq_port=5672
#RabbitMQ container
if [ "$(docker ps -aq -f name=${rabbitmq_name})" ]; then
echo Cleanup the existed ${rabbitmq_name} container
docker stop ${rabbitmq_name} && docker rm ${rabbitmq_name}
echo Create and start new ${rabbitmq_name} container
docker run --name ${rabbitmq_name} -d -p ${rabbitmq_port}:15672 -v $PWD/rabbitmq/${env}/data:/var/lib/rabbitmq:rw -v $PWD/rabbitmq/${env}/definitions.json:/opt/definitions.json:ro -v $PWD/rabbitmq/${env}/rabbitmq.config:/etc/rabbitmq/rabbitmq.config:ro rabbitmq:3-management
else
echo Create and start new ${rabbitmq_name} container
docker run --name ${rabbitmq_name} -d -p ${rabbitmq_port}:15672 -v $PWD/rabbitmq/${env}/data:/var/lib/rabbitmq:rw -v $PWD/rabbitmq/${env}/definitions.json:/opt/definitions.json:ro -v $PWD/rabbitmq/${env}/rabbitmq.config:/etc/rabbitmq/rabbitmq.config:ro rabbitmq:3-management
fi
我的 rabbitmq/dev
目录中还有以下 config
个文件
definitions.json
{
"rabbit_version": "3.7.3",
"users": [{
"name": "welib",
"password_hash": "su55YoHBYdenGuMVUvMERIyUAqJoBKeknxYsGcixXf/C4rMp",
"hashing_algorithm": "rabbit_password_hashing_sha256",
"tags": ""
}, {
"name": "admin",
"password_hash": "x5RW/n1lq35QfY7jbJaUI+lgJsZp2Ioh6P8CGkPgW3sM2/86",
"hashing_algorithm": "rabbit_password_hashing_sha256",
"tags": "administrator"
}],
"vhosts": [{
"name": "/"
}, {
"name": "dev"
}],
"permissions": [{
"user": "welib",
"vhost": "dev",
"configure": ".*",
"write": ".*",
"read": ".*"
}, {
"user": "admin",
"vhost": "/",
"configure": ".*",
"write": ".*",
"read": ".*"
}],
"topic_permissions": [],
"parameters": [],
"global_parameters": [{
"name": "cluster_name",
"value": "rabbit@98c821300e49"
}],
"policies": [],
"queues": [],
"exchanges": [],
"bindings": []
}
rabbitmq.config
[
{rabbit, [
{loopback_users, []},
{vm_memory_high_watermark, 0.7},
{vm_memory_high_watermark_paging_ratio, 0.8},
{log_levels, [{channel, warning}, {connection, warning}, {federation, warning}, {mirroring, info}]},
{heartbeat, 10}
]},
{rabbitmq_management, [
{load_definitions, "/opt/definitions.json"}
]}
].