24

I'm using the official RabbitMQ Docker image (https://hub.docker.com/_/rabbitmq/)

I've tried editing the rabbitmq.config file inside the container after running

docker exec -it <container-id> /bin/bash

However, this seems to have no effect on the rabbitmq server running in the container. Restarting the container obviously didn't help either since Docker starts a completely new instance.

So I assumed that the only way to configure rabbitmq.config for a Docker container was to set it up before the container starts running, which I was able to partly do using the image's supported environment variables.

Unfortunately, not all configuration options are supported by environment variables. For instance, I want to set {auth_mechanisms, ['PLAIN', 'AMQPLAIN', 'EXTERNAL']} in rabbitmq.config.

I then found the RABBITMQ_CONFIG_FILE environment variable, which should allow me to point to the file I want to use as my conifg file. However, I've tried the following with no luck:

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

The default rabbitmq.config file containing:

[ { rabbit, [ { loopback_users, [ ] } ] } ]

is what's in the container once it starts

What's the best way to configure rabbitmq.config inside Docker containers?

AmazingBergkamp
  • 5,708
  • 4
  • 15
  • 24

2 Answers2

33

the config file lives in /etc/rabbitmq/rabbitmq.config so if you mount your own config file with something like this (I'm using docker-compose here to setup the image)

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

that should do it.

In case you are having issues that the configuration file get's created as directory, try absolute paths.

Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
Luiz E.
  • 6,769
  • 10
  • 58
  • 98
  • 4
    I am trying to add config file to my own image with `COPY ./rabbitmq.config /etc/rabbitmq/rabbitmq.config` and then `CMD ["rabbitmq-server"]` but it seems that the file I add gets overwritten by default config file. it might be that default config file is created by "rabbit-server" command – Kirill G. Jan 28 '18 at 07:39
  • 1
    Yes, the file just gets overwritten by Rabbit, and some of the values such as default_permissions get deleted. Extremely irritating. – Jimbali Aug 29 '18 at 13:46
  • 1
    @KirillG. According to docker-entrypoint.sh of that image, it happens because you provide env vars that are starting from RABBITMQ_. If you place everything in own config initially and do not feed anything from env then it doesn't override. Though this becomes more problematic as saving password in config and sending to git looks even worse. – Michael A. Apr 19 '19 at 13:42
  • 2
    for old erland conf -> - ./conf/myrabbit.conf:/etc/rabbitmq/rabbitmq.config for newer version -> - ./conf/myrabbit.conf:/etc/rabbitmq/rabbitmq.conf – Gokberk Yar Sep 28 '21 at 18:11
  • In my case the location for the config file inside the docker container was `/etc/rabbitmq/conf.d/my_config.conf` – Niv Dec 20 '22 at 15:24
2

I'm able to run RabbitMQ with a mounted config using the following bash script:

#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

I also have the following config files in my rabbitmq/dev dir

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"}
    ]}
].
Ashraf Sarhan
  • 1,507
  • 16
  • 21