{"id":35693,"date":"2016-12-15T14:23:16","date_gmt":"2016-12-15T08:53:16","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=35693"},"modified":"2016-12-15T14:36:57","modified_gmt":"2016-12-15T09:06:57","slug":"how-to-automate-docker-on-vagrant","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-automate-docker-on-vagrant\/","title":{"rendered":"How to Automate Docker on Vagrant?"},"content":{"rendered":"<p>The <a title=\"What is Docker and Why You Must Use It?\" href=\"http:\/\/www.tothenew.com\/blog\/what-is-docker-and-why-use-it\/\">Docker<\/a>\u00a0provides 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.<\/p>\n<p>Think about the developer who wants to\u00a0run multiple containers to run a <a title=\"Docker DevOps\" href=\"http:\/\/www.tothenew.com\/devops-chef-puppet-docker\">dockerized application<\/a> 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 <a title=\"Vagrant \u2013 for Creating Lightweight Virtual Environments\" href=\"http:\/\/www.tothenew.com\/blog\/why-should-you-use-vagrant\/\">Vagrant<\/a>\u00a0and <a title=\"Docker Swarm Multi-Manager Setup\" href=\"http:\/\/www.tothenew.com\/blog\/docker-swarm-multi-manager-setup\/\">Docker Compose<\/a> while scripting all the steps in a Vagrant file.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-41723\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/10\/docker.png\" alt=\"docker\" width=\"543\" height=\"369\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/10\/docker.png 543w, \/blog\/wp-ttn-blog\/uploads\/2016\/10\/docker-300x203.png 300w\" sizes=\"(max-width: 543px) 100vw, 543px\" \/><\/p>\n<p>I request readers to <a href=\"https:\/\/www.vagrantup.com\/docs\/getting-started\/\">install Vagrant<\/a>\u00a0 on their system and assume that the readers have the basic knowledge\u00a0of Docker.<\/p>\n<p>After going through this blog you should be able to:<br \/>\n1. Install all Vagrant plugin dependencies<br \/>\n2. Install Docker on guest OS<br \/>\n3. Login into Docker hub to pull private images<br \/>\n4. Execute Docker-compose to run all container on startup<\/p>\n<p>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:<\/p>\n<p>1. <strong>vagrant-gatling-rsync<\/strong>: It is only required to sync the mounted resources from host to guest machine.<br \/>\n2. <strong>vagrant-docker-compose<\/strong>: It installs <a title=\"Running Multi-Container Nodejs Application using Docker Compose\" href=\"http:\/\/www.tothenew.com\/blog\/running-multi-container-nodejs-application-using-docker-compose\/\">Docker Compose<\/a> in guest machine and runs container defined in docker-compose.yml<br \/>\n3. <strong>vagrant-vbguest<\/strong>: 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.<br \/>\n4. <strong>vagrant-docker-login<\/strong>: Provide capability to login into Docker repository.<\/p>\n<p><span style=\"color: #000000;\"><strong>Define Vagrant Plugins<\/strong><\/span><br \/>\nLet&#8217;s first <a title=\"Using Vagrant to deploy AWS EC2 Instances\" href=\"http:\/\/www.tothenew.com\/blog\/using-vagrant-to-deploy-aws-ec2-instances\/\">understand Vagrant file configuration<\/a>. The below block should be on the top of the Vagrant configuration file. We can put all plugins name in a variable named &#8220;plugins_dependencies&#8221; and it will install missing plugins. If a plugin is installed it will restart the Vagrant process and try to provision your box again.<\/p>\n<p>[js]<br \/>\n# List plugins dependencies<br \/>\nplugins_dependencies = %w( vagrant-gatling-rsync vagrant-docker-compose vagrant-vbguest vagrant-docker-login )<br \/>\nplugin_status = false<br \/>\nplugins_dependencies.each do |plugin_name|<br \/>\n  unless Vagrant.has_plugin? plugin_name<br \/>\n    system(&quot;vagrant plugin install #{plugin_name}&quot;)<br \/>\n    plugin_status = true<br \/>\n    puts &quot; #{plugin_name}  Dependencies installed&quot;<br \/>\n  end<br \/>\nend<\/p>\n<p># Restart Vagrant if any new plugin installed<br \/>\nif plugin_status === true<br \/>\n  exec &quot;vagrant #{ARGV.join&#8217; &#8216;}&quot;<br \/>\nelse<br \/>\n  puts &quot;All Plugin Dependencies already installed&quot;<br \/>\nend<br \/>\n[\/js]<\/p>\n<p><span style=\"color: #000000;\"><strong>Setup Guest Machine<\/strong><\/span><\/p>\n<p>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 &#8220;192.168.1.100&#8221;. 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.<\/p>\n<p>[js]<br \/>\nVagrant.configure(&quot;2&quot;) do |config|<br \/>\n  config.vm.hostname = &quot;docker&quot;<br \/>\n  config.vm.box = &quot;bento\/ubuntu-14.04&quot;<br \/>\n  config.vm.network &quot;private_network&quot;, ip: &quot;192.168.1.100&quot;<\/p>\n<p>  config.vm.provider &quot;virtualbox&quot; do |v|<br \/>\n    v.name = &quot;docker&quot;<br \/>\n    v.memory = 4096<br \/>\n    v.cpus = 2<br \/>\n  end<br \/>\n[\/js]<\/p>\n<p><span style=\"color: #000000;\"><strong>Define Docker Provisioner<\/strong><\/span><br \/>\nIn this step, you need to define provisioner and the \u00a0file 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.<\/p>\n<p>[js]<br \/>\n  config.vm.provision :shell, inline: &quot;apt-get update&quot;<br \/>\n  config.vm.provision :docker<br \/>\n  config.vm.provision :docker_login, username: &quot;&lt;username&gt;&quot;, email: &quot;&lt;em&gt;&quot;,   password: &quot;&lt;password&gt;&quot;<br \/>\n  config.vm.provision :docker_compose, yml: [&quot;\/vagrant\/docker-compose.yml&quot;]<br \/>\nend<br \/>\n[\/js]<\/p>\n<p><span style=\"color: #000000;\"><strong>Start Docker VM<\/strong><\/span><br \/>\nOnce you finish with configuration its time to execute &#8220;vagrant up&#8221; command. You can download the Vagrant configuration file which I have used <a href=\"https:\/\/bitbucket.org\/rajdeeps\/blog\/src\/f82080b688f62f61b3d0b6e52a39aea6521e2849\/docker-compose-Vagrantfile\" target=\"_blank\">from here<\/a>. 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.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-43158\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/Screen-Shot-2016-12-12-at-12.47.20-AM.png\" alt=\"Screen Shot 2016-12-12 at 12.47.20 AM\" width=\"627\" height=\"170\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/Screen-Shot-2016-12-12-at-12.47.20-AM.png 627w, \/blog\/wp-ttn-blog\/uploads\/2016\/12\/Screen-Shot-2016-12-12-at-12.47.20-AM-300x81.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/12\/Screen-Shot-2016-12-12-at-12.47.20-AM-624x169.png 624w\" sizes=\"(max-width: 627px) 100vw, 627px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Docker\u00a0provides 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 [&hellip;]<\/p>\n","protected":false},"author":165,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[1174,2348,1],"tags":[1892,1883,3282,4283,4288,4287,4285,2590,4286,3651,4284],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/35693"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/165"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=35693"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/35693\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=35693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=35693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=35693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}