0
0
RabbitmqHow-ToBeginner ยท 4 min read

How to Use Environment Variables in RabbitMQ Configuration

You can use environment variables in RabbitMQ by referencing them in the configuration file or Docker setup. For example, in the rabbitmq.conf file or Docker Compose, use ${VARIABLE_NAME} syntax to inject values dynamically.
๐Ÿ“

Syntax

RabbitMQ supports environment variables mainly in its configuration files and Docker setups. Use the ${VARIABLE_NAME} syntax to reference environment variables. This allows dynamic configuration without hardcoding sensitive or environment-specific data.

For example, in rabbitmq.conf or Docker Compose, you can write:

  • RABBITMQ_DEFAULT_USER=${RABBITMQ_USER}
  • listeners.tcp.default = ${RABBITMQ_PORT}
conf
listeners.tcp.default = ${RABBITMQ_PORT}
management.listener.port = ${RABBITMQ_MANAGEMENT_PORT}

# Default user and password from environment
loopback_users.guest = false

# Use environment variables for default user
default_user = ${RABBITMQ_DEFAULT_USER}
default_pass = ${RABBITMQ_DEFAULT_PASS}
๐Ÿ’ป

Example

This example shows how to run RabbitMQ in Docker using environment variables to set the default user, password, and ports. It demonstrates flexible configuration without changing the Docker image.

yaml
version: '3'
services:
  rabbitmq:
    image: rabbitmq:3.11-management
    ports:
      - "${RABBITMQ_PORT}:5672"
      - "${RABBITMQ_MANAGEMENT_PORT}:15672"
    environment:
      RABBITMQ_DEFAULT_USER: "${RABBITMQ_USER}"
      RABBITMQ_DEFAULT_PASS: "${RABBITMQ_PASS}"
    restart: unless-stopped
Output
RabbitMQ server starts with user and password from environment variables and listens on specified ports.
โš ๏ธ

Common Pitfalls

Common mistakes when using environment variables with RabbitMQ include:

  • Not exporting environment variables before starting RabbitMQ or Docker, causing empty or default values.
  • Incorrect syntax in configuration files, such as missing ${} around variable names.
  • Using environment variables in places RabbitMQ does not support dynamic substitution (some config options require static values).
  • Forgetting to secure sensitive variables like passwords in production.
conf
## Wrong way (no variable syntax):
listeners.tcp.default = RABBITMQ_PORT

## Right way:
listeners.tcp.default = ${RABBITMQ_PORT}
๐Ÿ“Š

Quick Reference

Tips for using environment variables with RabbitMQ:

  • Use ${VARIABLE_NAME} syntax in rabbitmq.conf and Docker Compose.
  • Export environment variables in your shell or CI/CD pipeline before starting RabbitMQ.
  • Use Docker environment variables for easy container configuration.
  • Keep sensitive data like passwords out of config files; use environment variables instead.
โœ…

Key Takeaways

Use ${VARIABLE_NAME} syntax to reference environment variables in RabbitMQ config files.
Set environment variables before starting RabbitMQ or Docker to ensure correct values.
Docker Compose environment section is ideal for injecting RabbitMQ settings dynamically.
Avoid hardcoding sensitive data; use environment variables for security and flexibility.
Check RabbitMQ docs for which config options support environment variable substitution.