How to configure RabbitMQ to run as Docker container in Vagrant

Vagrant is one of those technologies that hit you like a truck and you don’t understand anymore how you lived without it before.

If you haven’t heard about Vagrant yet – it is a simple mechanism to automatically build a complete development environment which is sandboxed in a VM. By default it uses VirtualBox but could be used with other providers, including VMware, AWS, Google Compute Engine, etc. It seamlessly integrates with things like Chef, Puppet and Docker.

From practical perspective it works like this: you add to your git repo a Vagrantfile which is a configuration of VM image and software that you want to have installed for your project. When you clone this repo to your local machine you need to run a single command: “vagrant up” and Vagrant will setup a VM and install all the dependencies. Depending on the complexity of configuration this might take few mins but when Vagrant is done, you will have completely isolated dev environment which you can start, halt, destroy, etc. The main idea is that you have exactly the same development setup as you use in production so entire class of bugs (“… but it worked on my laptop!”) is eliminated and Vagrant takes away all the pain usually associated with repeatable env setup.

Vagrant is platform independent so you can continue using your favorite OS (MacOs, Linux or Windows) to run Ubuntu, CentOS or any other VM image, you of course can continue using your text editor and Vagrant will take care of file synch to VM.

There are plenty of resources on Vagrant but you probably should start with talk by Vagrant developer Mitchell Hashimoto and with Vagrant book.

For one of my recent projects I wanted to use RabbitMQ Docker container and here are the steps how Vagrant, RabbitMQ and Docker could be used together.

First you need RabbitMQ Docker image, you can build one yourself but there are quite a few of them already available at Docker index, I picked up the most recent one built by
Mikael Gueck

2. Now you need to install VirtualBox and Vagrant at your machine.
Install virtualbox from https://www.virtualbox.org/ (or use your package manager)
Install vagrant from http://www.vagrantup.com/ (or use your package manager)

3. Next step you need Vagrantfile which describes VM configuration:


# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "precise64"

  # The url from where the 'config.vm.box' box will be fetched if it
  # doesn't already exist on the user's system.
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  #config.vm.network :forwarded_port, guest: 80, host: 8080
  config.vm.network :forwarded_port, guest: 15672, host: 15672, auto_correct: true 
  config.vm.network :forwarded_port, guest: 4369, host: 4369,   auto_correct: true
  config.vm.network :forwarded_port, guest: 5672, host: 5672,   auto_correct: true

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network :private_network, ip: "192.168.33.10"

  # Load and start rabbitmq image
  config.vm.provision "docker" do |d|
    d.pull_images "mikaelhg/docker-rabbitmq"
    d.run "mikaelhg/docker-rabbitmq",
      args: "-h rabbithost -p :5672 -p :15672"
  end
end

this config will build Ubuntu based VM, will install RabbitMQ Docker container and start it with ports 5672 and 15672. The docker image will also have RabbitMQ management console plugin installed. The image will add NAT port mappings and also expose IP “192.168.33.10” for this VM.

4. Now you can run : “vagrant up” command and Vagrant will build and start the VM.

with “vagrant ssh” you can log into the VM console and with “docker ps -a” you can inspect running Docker containers.

here is the screenshot with the steps:
Screen Shot 2014-01-12 at 11.20.15

5. the final step: run http://192.168.33.10:32768/ from your browser (RabbitMQ login / password is “guest” / “guest”)

if everything has started correctly you should get RabbitMQ Management Console in your browser.

In conclusion: both Vagrant and Docker are absolutely amazing technologies and you should be aware of their usage.

Here is the gitbub repo with already preset project.

run:
git clone https://github.com/RomanShestakov/docker-rabbitmq.git
cd docker-rabbitmq
vagrant up
go to http://192.168.33.10:32768/

Hopefully this post would be helpful to somebody.

Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

3 Responses to How to configure RabbitMQ to run as Docker container in Vagrant

  1. Pingback: Docker, Puppet and taking care of chaos | R.Shestakov

  2. Lee says:

    Wow, thanks for the write up. I was looking at creating a test enviroment for a RabbitMQ test enviroment and you have just introduced me to some useful technologies on how to do this.

  3. Hello! Thanks for nice post!
    I have the problem on step of installing docker:
    ==> default: Running provisioner: docker…
    default: Installing Docker (latest) onto machine…
    The following SSH command responded with a non-zero exit status.
    Vagrant assumes that this means the command failed!

    curl http://get.docker.io/gpg | apt-key add –
    Stdout from the command:

    Stderr from the command:

    stdin: is not a tty
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    0 0 0 0 0 0 0 0 –:–:– –:–:– –:–:– 0
    gpg: no valid OpenPGP data found.

    What could be reason?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s