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.
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.
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.
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.
git clone https://github.com/RomanShestakov/docker-rabbitmq.git
go to http://192.168.33.10:32768/
Hopefully this post would be helpful to somebody.