Sunday, 13 August 2017

Quick RabbitMq using Docker

I needed a quick RabbitMq installation so I could play with MassTransit, the free open-source .Net message bus framework. Docker to the rescue. I was using the Docker Toolbox on Windows 10 Home.

First things first - RabbitMq is available on the Docker Store as a Docker image called rabbitmq. The documentation is reasonable and I decided I wanted RabbitMq installed with the management plugin. For this test I decided to leave the default RabbitMq username (guest) and password in place. I also elected to expose the default ports for the management plugin (15672) and the standard RabbitMq port (5672) to the host.

Having scanned the documentation the following Docker run command would seem to be in order:

docker run -d --hostname my-rabbit --name some-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management

Note the two –p command line arguments exposing the ports to the host from the Docker container. So, time to crack open the Docker Quickstart Terminal and run the command.


With the Docker container up-and-running I can now go to my local machine and access the RabbitMq management UI running on port 15672.


Cool! Looks like we’ve got RabbitMq running in a container.

A quick test application

I decided to run a quick test using a console application to check everything was working. Firstly, I set up a virtual host for the test in the management UI remembering to add the guest user to the virtual host.


Then, using the RabbitMq .Net client I created the ‘Hello World’ application.

using System;
using System.Text;
using RabbitMQ.Client;

namespace RabbitMqTest
    class Program
        static void Main(string[] args)
                ConnectionFactory factory = new ConnectionFactory();
                factory.Uri = new Uri("amqp://guest:guest@");

                IConnection conn = factory.CreateConnection();

                IModel model = conn.CreateModel();

                var exchangeName = "console-test-exchange";
                var queueName = "console-test-queue";
                var consoleTestRoutingKey = "console-test-routing-key";

                model.ExchangeDeclare(exchangeName, ExchangeType.Direct);
                model.QueueDeclare(queueName, false, false, false, null);
                model.QueueBind(queueName, exchangeName, consoleTestRoutingKey, null);

                byte[] messageBodyBytes = Encoding.UTF8.GetBytes("Hello, world!");
                model.BasicPublish(exchangeName, consoleTestRoutingKey, null, messageBodyBytes);
            catch (Exception e)

I ran the application and headed back into the RabbitMq management UI to check the results. Firstly, was the exchange created?


It was. And the queue?


Success again. You can see there’s one message ready too. By drilling in to the queue you can get messages in the management UI. Let’s see what we got.


Bingo! So, all is well with RabbitMq.

The Sample-ShoppingWeb application

MassTransit has a sample application called Sample-ShoppingWeb which you can get from GitHub.

Firstly, I created a new virtual host in RabbitMq and added the guest user to it. I then made simple changes to the configuration to change the RabbitMqHost setting in the App.config and Web.config files of the TrackingService and Shopping.Web projects respectively.


I ran the example and added a few items to the cart using the Shopping.Web MVC application. Watched as the TrackingService picked up the items via RabbitMq. An examination of the RabbitMq management UI showed a bunch of new exchanges and two new queues.