✦ For everyone, free.

Practical knowledge for real and everyday life

Home

9.1.2.3 Compose Message Brokers

A focused guide to Compose Message Brokers, connecting core concepts with practical Docker and container operations.

Compose message brokers are messaging or queueing services — such as RabbitMQ or Kafka — defined within a Compose file, providing a consistent local instance of this kind of infrastructure for developing and testing applications that rely on asynchronous messaging between components.

Defining a Message Broker Service

A message broker service is configured much like any other infrastructure dependency, specifying the image and any necessary ports or credentials.

services:
  broker:
    image: rabbitmq:3-management
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      - RABBITMQ_DEFAULT_USER=devuser
      - RABBITMQ_DEFAULT_PASS=devpassword

The second exposed port here provides access to RabbitMQ's management web interface, useful for inspecting queues and messages during development.

Connecting Application Services to the Broker

Other services reference the broker by its Compose service name, exactly as they would any other service.

services:
  producer:
    environment:
      - BROKER_URL=amqp://devuser:devpassword@broker:5672
  consumer:
    environment:
      - BROKER_URL=amqp://devuser:devpassword@broker:5672
  broker:
    image: rabbitmq:3-management

Both producer and consumer reach the same broker instance using its Compose service name, with Compose's automatic networking handling resolution.

Ensuring the Broker Starts Before Dependent Services

A depends_on relationship can help sequence startup, though for many brokers, an application's own retry logic for the initial connection attempt remains the more robust safeguard, since being started doesn't guarantee being immediately ready to accept connections.

services:
  consumer:
    depends_on:
      - broker
  broker:
    image: rabbitmq:3-management
Why Compose Message Brokers Matter

Running a message broker through Compose provides developers with a consistent, easily resettable local environment for building and testing applications built around asynchronous messaging, without needing to set up and maintain this infrastructure outside of the project's own development workflow.