Virtual box, Vagrant and docker to setup & up machines instantly

Its always a pain point to test application on different operating systems with all the dependency software installed that your application might require. It would be nice if we get a pre-configured box where we add all the required softwares (databases, web servers etc.) and just use one command to bring it up and later hand over the same box to ops/admins who can just bring it up with one command without messing around with dependency software and versions.

Opensource solution Virtual box allows you to create & run virtual machines (Linux, Mac, Windows), Vagrant allows you to create and configure environments, Docker is a container that allows you to package and ship softwares.

Following we will create a Ubuntu Linux box with mongo db installed on it.

Download and install
1. Virtual box
2. Vagrant

To create a virtual machine create a directory vagrant_getting_started and do

$vagrant init ubuntu/trusty64

--Output
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

A Vagrant file is created that holds all the configurations for our virtual box. To install and run mongo db add following configuration

#Port forwarding mongodb can be accessed from your machine using port 27017
config.vm.network "forwarded_port", guest: 27017, host: 27017

config.vm.provision "docker" do |d|
d.run "mongoDB",image:"mongo:2",args:" -p 27017:27017"
end

Start up the virtual box

$vagrant up

-- Can take a while, Hint! Cachier plugin can be installed and that will speed up things later
https://github.com/fgrehm/vagrant-cachier

--Output (machine is started and can be seen running in the Oracle Virtual Box Manager, Docker is installed on the machine and mongodb is started)
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
default: /vagrant => C:/Users/muhammed01/test
==> default: Running provisioner: docker...
default: Installing Docker (latest) onto machine...
default: Configuring Docker to autostart containers...
==> default: Starting Docker containers...
==> default: -- Container: mongoDB

Now ssh to this machine (vagrant/vagrant) is the username/password the host and port is listed in the output

==> default: Setting the name of the VM: test_default_1430814667041_78006
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 27017 => 27017 (adapter 1)
default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant

After login if we grep for mongo db we can see the process is running

vagrant@vagrant-ubuntu-trusty-64:~$ ps -ef|grep mongo
999 14147 13903 0 08:33 ? 00:00:02 mongod
vagrant 14267 14248 0 08:40 pts/2 00:00:00 grep --color=auto mongo

That’s it we got a Linux box running with dependency in few minutes, now you can give the VagrantFile to any one and he can have the same instance up and running with one command.

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