Friday April 10, 2015 - tags:    rabbitmq, docker, MQTT, IOT

The Internet of things, Dockerising RabbitMQ and MQTT




Introduction

Ths is the first post in a 2 part series, where I explore how to setup a dockerised RabbitMQ instance with MQTT support and outline how we can use RabbitMQ and its MQTT plugin to talk to devices with MQTT and AMQP in a polyglot manner.

Part two can be found here:
- The Internet of things, with RabbitMQ, Node.js, MQTT and AMQP

Source code can be found here:
- the-internet-of-things-rabbitmq-mqtt

MQTT is a machine-to-machine connectivity protocol, used by many to support the 'Internet of Things'. It was designed as an extremely lightweight publish/subscribe messaging transport. RabbitMQ supports MQTT via plugin.

In this post

An MQTT enabled RabbitMQ docker image

I have created a docker image with MQTT enabled: andrewkeig/rabbitmq-mqtt-enabled, which is simply the official RabbitMQ docker image 3.5 as base, with MQTT and Management plugins enabled.

FROM rabbitmq:3.5.0

RUN rabbitmq-plugins enable --offline rabbitmq_management  
RUN rabbitmq-plugins enable --offline rabbitmq_mqtt

EXPOSE 15672  
EXPOSE 1883  

Enable RabbitMQ ports on a mac for docker

If you are running on a mac, run the following commands to forward ports.

$ VBoxManage modifyvm "boot2docker-vm" --natpf1 "rb1,tcp,127.0.0.1,1883,,1883"
$ VBoxManage modifyvm "boot2docker-vm" --natpf1 "rb2,tcp,127.0.0.1,15672,,15672"
$ VBoxManage modifyvm "boot2docker-vm" --natpf1 "rb3,tcp,127.0.0.1,5672,,5672"

Run RabbitMQ with docker-compose

In order to run the docker image you can use the following docker-compose file, which uses the image I created with MQTT enabled. We expose 3 ports:

rb:  
  image: andrewkeig/rabbitmq-mqtt-enabled
  volumes:
    - /var/docker/rabbit:data
  ports:
    - "15672:15672"
    - "5672:5672"
    - "1883:1883"

Now we have our docker-compose file setup we can simply run:

$ docker-compose up

RabbitMQ management console

If you would like to view the RabbitMQ management console; this can be found here:

http://localhost:15672

Docker exposed environment variables

If you would like to use this image as part of your project, Docker exposes two RabbitMQ environment variables. We have to do a bit of url hacking in order to change the protocol contained in the docker url.

var mqttUrl = process.env.RB_PORT_1883_TCP.replace('tcp', 'mqtt');  
var amqpUrl = process.env.RB_PORT_5672_TCP.replace('tcp', 'amqp');