How to create a queue in RabbitMQ upon startup

You can predefine queues and exchanges without creating own rabbit-mq docker image.

Your docker-compose should look like this:

rabbit:
container_name: rabbitmq-preload-conf
image: rabbitmq:3-management
volumes:
  - ./init/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf:ro
  - ./init/definitions.json:/etc/rabbitmq/definitions.json:ro
ports:
  - "5672:5672"
  - "15672:15672"

In this case rabbitmq.conf and definitions.json files should be in init folder in the same parent folder as docker-compose file

rabbitmq.conf file

management.load_definitions = /etc/rabbitmq/definitions.json

definitions.json file

{
"queues": [
    {
        "name": "externally_configured_queue",
        "vhost": "/",
        "durable": true,
        "auto_delete": false,
        "arguments": {
            "x-queue-type": "classic"
        }
    }
],
"exchanges": [
    {
        "name": "externally_configured_exchange",
        "vhost": "/",
        "type": "direct",
        "durable": true,
        "auto_delete": false,
        "internal": false,
        "arguments": {}
    }
],
"bindings": [
    {
        "source": "externally_configured_exchange",
        "vhost": "/",
        "destination": "externally_configured_queue",
        "destination_type": "queue",
        "routing_key": "externally_configured_queue",
        "arguments": {}
    }
  ]
 }

NOTE: After update in rabbit images, additional configuration may be required. If rabbit container failed to start with the configuration mentioned above (error message contains “Exception during startup:
exit:{error,{no_such_vhost,<<“/”>>}}”
) following configuration should be added into definitions file:

 "users": [
    {
      "name": "guest",
      "password_hash": "BMfxN8drrYcIqXZMr+pWTpDT0nMcOagMduLX0bjr4jwud/pN",
      "hashing_algorithm": "rabbit_password_hashing_sha256",
      "tags": [
        "administrator"
      ],
      "limits": {}
    }
  ],
  "vhosts": [
    {
      "name": "/"
    }
  ],
  "permissions": [
    {
      "user": "guest",
      "vhost": "/",
      "configure": ".*",
      "write": ".*",
      "read": ".*"
    }
  ]

Using this additional configuration, default user will be guest with password guest.

Apart from queues, exchanges and bindings, definitions.json file can contain additional configuration

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)