How to Automate Docker on Vagrant?

15 / Dec / 2016 by Rajdeep Singh 0 comments

The Docker provides capabilities to ship and run containerized application on the development machine and eliminates inconsistency in the different environment. However, it needs Linux Kernel to run Docker Daemon on the machine. There are many tools to run Docker on Windows/MAC machine. In this post, we will focus on running multiple containers on a non-Linux machine with a single command.

Think about the developer who wants to run multiple containers to run a dockerized application and the images, which are in a private registry. Setting up such an environment is a time-consuming task. We will make it easy with Vagrant and Docker Compose while scripting all the steps in a Vagrant file.

docker

I request readers to install Vagrant  on their system and assume that the readers have the basic knowledge of Docker.

After going through this blog you should be able to:
1. Install all Vagrant plugin dependencies
2. Install Docker on guest OS
3. Login into Docker hub to pull private images
4. Execute Docker-compose to run all container on startup

We are building a single Vagrant file which will be performing all steps during provisioning. The Vagrant written in Ruby language will have its configuration in ruby syntax only. We have to identify all the dependent plugins as in our case:

1. vagrant-gatling-rsync: It is only required to sync the mounted resources from host to guest machine.
2. vagrant-docker-compose: It installs Docker Compose in guest machine and runs container defined in docker-compose.yml
3. vagrant-vbguest: It is required to check VirtualBox guest version on the box and if required will update it to the current version of host machine VirtualBox.
4. vagrant-docker-login: Provide capability to login into Docker repository.

Define Vagrant Plugins
Let’s first understand Vagrant file configuration. The below block should be on the top of the Vagrant configuration file. We can put all plugins name in a variable named “plugins_dependencies” and it will install missing plugins. If a plugin is installed it will restart the Vagrant process and try to provision your box again.

[js]
# List plugins dependencies
plugins_dependencies = %w( vagrant-gatling-rsync vagrant-docker-compose vagrant-vbguest vagrant-docker-login )
plugin_status = false
plugins_dependencies.each do |plugin_name|
unless Vagrant.has_plugin? plugin_name
system("vagrant plugin install #{plugin_name}")
plugin_status = true
puts " #{plugin_name} Dependencies installed"
end
end

# Restart Vagrant if any new plugin installed
if plugin_status === true
exec "vagrant #{ARGV.join’ ‘}"
else
puts "All Plugin Dependencies already installed"
end
[/js]

Setup Guest Machine

I hope you will find this section interesting. Here we just have to define some parameters of VM machine and the guest machine will have IP “192.168.1.100”. All port of this IP is now accessible on the host machine, So there is no need to expose a specific port. You can edit Vagrant configuration file the way you want.

[js]
Vagrant.configure("2") do |config|
config.vm.hostname = "docker"
config.vm.box = "bento/ubuntu-14.04"
config.vm.network "private_network", ip: "192.168.1.100"

config.vm.provider "virtualbox" do |v|
v.name = "docker"
v.memory = 4096
v.cpus = 2
end
[/js]

Define Docker Provisioner
In this step, you need to define provisioner and the  file name for Docker Compose. Make sure the compose file should exist in your current working directory. You have to replace the credential with its real value.

[js]
config.vm.provision :shell, inline: "apt-get update"
config.vm.provision :docker
config.vm.provision :docker_login, username: "<username>", email: "<em>", password: "<password>"
config.vm.provision :docker_compose, yml: ["/vagrant/docker-compose.yml"]
end
[/js]

Start Docker VM
Once you finish with configuration its time to execute “vagrant up” command. You can download the Vagrant configuration file which I have used from here. If there is no syntax error in your configuration file, then on console output you can see it successfully logged in into private Docker registry and the Docker Compose is able to run all container.

Screen Shot 2016-12-12 at 12.47.20 AM

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *