If you are build a saas, using VMs and management tools. You will find vagrant is useful for additional features.
But Virtual machines take too much time to load. Now there is a new trending called using docker. Docker is written in go, if you haven’t heard of, you should probably go to check it out. In this article I am going to run a docker container in vagrant virtual machine
What is vagrant
Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases development/production parity, and makes the “works on my machine” excuse a relic of the past.
you can also think it as a VM without the GUI. At its core, Vagrant is a simple wrapper around Virtualbox/VMware.
A few interesting features:
- Boatloads of existing images, just check Vagrantbox.es for example.
- Snapshot and package your current machine to a Vagrant box file (and, consequently, share it back).
- Ability to fine tune settings of the VM, including things like RAM, CPU, APIC…
- Vagrantfiles. This allows you to setup your box on init: installing packages, modifying configuration, moving code around…
- Integration with CM tools like Puppet, Chef and Ansible.
Check the docs
Docker is a Linux container, based on lxc (self-described as “chroot on steroids”) and AUFS. Instead of providing a full VM, like you get with Vagrant, Docker provides you lightweight containers, that share the same kernel and allow to safely execute independent processes.
Docker is attractive for many reasons:
- Lightweight; images are much lighter than full VMs, and spinning off a new instance is lightning fast (in the range of seconds instead of minutes).
- Version control of the images, which makes it much more convenient to handle builds.
- Lots of images (again), just have a look at the docker public index of images.
Now Let’s get started.
- You should have virtualbox and vagrant ready.
- download an image
- you are all done
- There’s a 4 if you want to access your (soon to be) deployed app; you will need to dig around the Vagrant documentation to perform port forwarding, proper networking and update manually your
in virtualbox now
- Install Docker. as explainer on the official website
- verity it worked by trying to build your first container:
$ sudo docker run -i -t ubuntu /bin/bash
- let’s create a
Dockerfileto build a new image
- get ssh-key
- Now build it, more info about ssh into docker can be found here
sudo docker build -t eg_sshd .
Then run it. You can then use docker port to find out what host port the container’s port 22 is mapped to:
And now you can ssh to port
49153 on the Docker daemon’s host IP address (ip address or ifconfig can tell you that):
$ ssh email@example.com -p 49153
Now you are in a docker container. Yeah! Finally, clean up after your test by stopping and removing the container, and then removing the image.
Let’s wrap it up
So we just saw (roughly) how these tools can be used, and how they can be complementary:
Vagrant will provide you with a full VM, including the OS. It’s great at providing you a Linux environment for example when you’re on MacOS. Docker is a lightweight VM of some sort. It will allow you to build contained architectures faster and cheaper than with Vagrant.
It takes a bit of reading to get more familiar with these tools, this kind of technology allows you to automate and commoditize huge parts of your development and ops workflows. I strongly encourage you to make that investment. It has helped me tremendously increase the pace and quality of my throughput.