How to add initial users when starting a RabbitMQ Docker container?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
For a fresh RabbitMQ container, the simplest way to create an initial user is through the default user environment variables. The important caveat is that these values are only applied when RabbitMQ is booting with a blank, uninitialized database, so reused volumes often make it look like the settings were ignored.
The Simple Case: One Initial User On First Boot
For development or CI, the official image supports:
This creates:
- the initial user
- the initial password
- the initial virtual host
But only if the node is starting from scratch.
That "first boot only" behavior is the most important thing to remember.
Why It Sometimes Seems Not To Work
If you mount a persistent volume and restart the container later, RabbitMQ already has a database. At that point, RABBITMQ_DEFAULT_USER, RABBITMQ_DEFAULT_PASS, and RABBITMQ_DEFAULT_VHOST do not recreate or overwrite users.
That is why this common pattern can confuse people:
If rabbitmq_data already contains initialized state, changing the environment variables later will not change the existing users.
For More Than One User, Use Definitions Import
If you need several users, permissions, vhosts, exchanges, or policies at boot time, definitions import is the cleaner approach.
Example definitions.json:
Then mount it and point RabbitMQ at it:
And in rabbitmq.conf:
This is a much better fit when you want reproducible multi-user bootstrapping.
Avoid Shell-Scripting User Creation Inside The Container
A common but fragile pattern is to wrap rabbitmq-server in a shell script, sleep for startup, then run rabbitmqctl add_user .... That can work, but it is harder to reason about, less declarative, and more error-prone than using the official boot-time options.
If you only need one default user, use the environment variables. If you need a full preconfigured broker, use definitions import.
That split keeps the setup maintainable.
It also makes local debugging easier because you know whether the source of truth is a few environment variables or a full declarative broker definition.
That clarity saves time when containers are recreated frequently.
It helps during CI too.
Common Pitfalls
One common mistake is expecting RABBITMQ_DEFAULT_USER and related variables to change an already-initialized volume.
Another issue is using these defaults in production as if they were a full identity-management strategy. They are mainly convenient for development, CI, and controlled bootstrap scenarios.
A third problem is trying to add many users through ad hoc container startup scripts when a definitions file would be clearer and more reproducible.
Finally, people often forget that users also need permissions on the relevant virtual hosts, not just an account record.
Summary
- '
RABBITMQ_DEFAULT_USER,RABBITMQ_DEFAULT_PASS, andRABBITMQ_DEFAULT_VHOSTwork only on first boot of a blank node.' - Persistent volumes are the usual reason those variables appear to stop working.
- For multiple users and richer setup, use definitions import instead of shell scripting.
- Mount
definitions.jsonand pointmanagement.load_definitionsto it for reproducible bootstrapping. - Always think about permissions and vhosts, not just usernames and passwords.
- Decide early whether startup convenience or reproducible provisioning matters more.
Related reading
- How to add JVM parameters to Apache Kafka?
- How to add plugin to RabbitMQ docker image?
- How to alter the TTL for a particular topic in Kafka
- How to always consume from latest offset in kafka-streams
- How to add users to Docker container?
- How to allow a Kubernetes Job access to a file on host
- How to apply an SMT to a single topic in Kafka connect?
- How to ask RabbitMQ to retry when business Exception occurs in Spring Asynchronous MessageListener use case

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.