如何在Docker容器中配置rabbitmq.config?

e0bqpujr  于 2022-11-23  发布在  RabbitMQ
关注(0)|答案(2)|浏览(187)

我使用的是官方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环境变量,它应该允许我指向我想用作我的conifg文件的文件。

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的最佳方法是什么?

ztyzrc3y

ztyzrc3y1#

配置文件位于/etc/rabbitmq/rabbitmq.config中,因此如果您使用类似以下内容挂载自己的配置文件(我在这里使用docker-compose来设置映像)

volumes:
- ./conf/myrabbit.conf:/etc/rabbitmq/rabbitmq.config

这样就行了。
如果您在将配置文件创建为目录try absolute paths时遇到问题。

ozxc1zmp

ozxc1zmp2#

我可以使用下面的bash脚本在已挂载的配置下运行RabbitMQ:

#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"}
    ]}
].

相关问题