Creating a custom RabbitMQ container with preconfigured queues

It is pretty easy to create custom RabbitMQ containers based on the official RabbitMQ images with queues and exchanges preconfigured.

Note: This post has been updated and tested with lately with RabbitMQ version 3.6.11-alpine

FROM rabbitmq:3.6.11-alpine

MAINTAINER <devop@datenkollektiv.de>

ADD rabbitmq.config /etc/rabbitmq/
ADD definitions.json /etc/rabbitmq/

The custom configuration is done via the two files ADDed to etc/rabbitmq/. First the rabbitmq.config. Nothing fancy here.

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

Note: Kent Johnson pointed out in his comment below “that you can export definitions from the RabbitMQ management interface once you are done setting things up the way you want”. Thanks for the hint. He also mentioned that he came here via the StackOverflow question How to add initial users when starting a RabbitMQ Docker container? showing a more detailed example of a rabbitmq.config.

The exchange and queue is configured in the referenced definitions.json (For more configuration options see the rabbitmq.config.example). The most relevant portions of the configuration file are exchanges, queues and bindings. No surprise in these three sections of the configuration:

{
  "exchanges": [
    {
      "name": "jenkins",
      "vhost": "/",
      "type": "fanout",
      "durable": true,
      "auto_delete": false,
      "internal": false,
      "arguments": {}
    }
  ],
  "queues": [
    {
      "name": "jenkins",
      "vhost": "/",
      "durable": true,
      "auto_delete": false,
      "arguments": {}
    }
  ],
  "bindings": [
    {
      "source": "jenkins",
      "vhost": "/",
      "destination": "jenkins",
      "destination_type": "queue",
      "routing_key": "*",
      "arguments": {}
    }
  ]
}

To get an overall impression of the configuration - the context of the snippets above:

{
  "users": [
    {
      "name": "guest",
      "password_hash": "guestAccessHash=",
      "tags": "administrator"
    }
  ],
  "vhosts": [
    {
      "name": "/"
    }
  ],
  "permissions": [
    {
      "user": "guest",
      "vhost": "/",
      "configure": ".*",
      "write": ".*",
      "read": ".*"
    }
  ],
  "parameters": [],
  "policies": [],
...
}

Note: If you prefer to create a RabbitMQ server with the management console enabled replace the FROM line with:

FROM rabbitmq:3.6.11-management-alpine

Kick-start a local RabbitMQ listening on the internal interface, only:

docker run -d -u root --name rabbitmq -p 127.0.0.1:5672:5672 datenkollektiv/rabbitmq