Omroth
Omroth

Reputation: 1129

How to add a RabbitMQ user while RabbitMQ is not live

I run a RabbitMQ service and Celery in a Docker container for my server. Workers are celery instances which connect to the server via RabbitMQ.

I set up and run RabbitMQ like this:

sudo service rabbitmq-server start
rabbitmqctl add_user bunny password
rabbitmqctl add_vhost bunny_host
rabbitmqctl set_permissions -p bunny_host bunny ".*" ".*" ".*"

This has a problem: if a worker tries to connect between the service being started and the bunny user being created and given permissions, then the worker's celery instance will terminate.

I tried adding this to the Dockerfile for my server to add the user before "live" startup:

RUN sudo service rabbitmq-server start && \
    rabbitmqctl add_user bunny password && \
    rabbitmqctl add_vhost bunny_host && \
    rabbitmqctl set_permissions -p bunny_host bunny ".*" ".*" ".*" && \
    sudo service rabbitmq-server stop

But when I restarted the rabbitmq-server service within the container, the user bunny did not exist.

(If I try to use rabbitmqctl to add a user when the service is not running, it errors out.)

Any help would be much appreciated.

Upvotes: 3

Views: 965

Answers (1)

Al-waleed Shihadeh
Al-waleed Shihadeh

Reputation: 2855

You can not run rabbitmqctl during the build time. Instead, you can achieve the same by using the bootstrap files. you need to update your files like the below snippets

Dockerfile

FROM rabbitmq:3.6.11-management-alpine
ADD rabbitmq.config /etc/rabbitmq/
ADD definitions.json /etc/rabbitmq/
RUN chmod 666 /etc/rabbitmq/*

rabbitmq.config

[
  {
    rabbit,
  [
    { loopback_users, [] }
  ]
},
  {
    rabbitmq_management,
  [
    { load_definitions, "/etc/rabbitmq/definitions.json" }
  ]
}
].

definitions.json

{
  "rabbit_version": "3.6.14",
  "users": [
    {
      "name": "user",
      "password_hash": "0xZBvBD2JOGWrVO84nZ62EJuQIRehcILEiPVFB9mD4zhFcAo",
      "hashing_algorithm": "rabbit_password_hashing_sha256",
      "tags": "administrator"
    }
  ],
  "vhosts": [
    {
      "name": "/"
    }
  ],
  "permissions": [
    {
      "user": "community",
      "vhost": "/",
      "configure": ".*",
      "write": ".*",
      "read": ".*"
    }
  ],
  "parameters": [],
  "global_parameters": [
    {
      "name": "cluster_name",
      "value": "rabbit@rabbitmq"
    }
  ],
  "policies": [],
  "queues": [],
  "exchanges": [],
  "bindings": []
}

Upvotes: 2

Related Questions