docker rabbitmq how to expose port and reuse container with a docker file
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
RabbitMQ is a popular open-source message broker that facilitates message-oriented middleware architectures. It is lightweight, easy to set up, and supports multiple messaging protocols. Docker offers an efficient way to deploy and manage RabbitMQ instances. This article will guide you on how to expose ports and reuse RabbitMQ containers using a Dockerfile, highlighted with technical explanations and examples.
Understanding RabbitMQ Docker Containers
To run RabbitMQ in Docker, you can use the official RabbitMQ image from Docker Hub. This image is preconfigured with RabbitMQ and all its dependencies. When setting up RabbitMQ in a Docker container, you often need to configure network settings, including port mappings, to allow for communication between the RabbitMQ service inside the container and the outside world.
Dockerfile for RabbitMQ
A Dockerfile is a text document that contains the commands a user could call on the command line to assemble an image. Here’s a basic example of a Dockerfile for RabbitMQ:
Exposing Ports in Docker
In the above Dockerfile, the EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. 15672 is the default port for the management interface, and 5672 is used by the AMQP protocol. Exposing these ports makes them available to linked services, but not to the host. To bind these ports to the host, you need to use the -p option in the docker run command like so:
This command tells Docker to run the container in detached mode, bind port 5672 inside the container to port 5672 on the host, and bind port 15672 inside the container to port 15672 on the host.
Reusing RabbitMQ Containers
When developing applications, you might stop and start the RabbitMQ container multiple times. To reuse a RabbitMQ container:
- Start the RabbitMQ Docker container if it's not already running:
- When you are done, you can stop the container:
- If you need to shell into the running container, use:
By starting and stopping the same container, you maintain the state of the RabbitMQ service across development cycles, including queues and configurations until the container is removed.
Summary Table
To summarize the configurations and commands:
| Action | Command | Description |
| Build Image | docker build -t myrabbitmq-image . | Builds a Docker image named myrabbitmq-image using the Dockerfile in the current directory. |
| Run Container | docker run -d --name myrabbitmq -p 5672:5672 -p 15672:15672 myrabbitmq-image | Runs the container with port bindings. |
| Start Container | docker start myrabbitmq | Starts an existing container. |
| Stop Container | docker stop myrabbitmq | Stops the running container. |
| Shell into Container | docker exec -it myrabbitmq bash | Accesses the shell of the running container. |
Conclusion
Using Docker to manage RabbitMQ instances provides flexibility, portability, and a consistent environment for development and production settings. By properly configuring the Dockerfile and understanding Docker commands for managing containers, developers can efficiently integrate RabbitMQ into their workflows.

