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 inrabbitmq.confand 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.