From 7e8077310d28624e89a071a782c2eb2d263abf50 Mon Sep 17 00:00:00 2001 From: "Scott S. Lowe" Date: Tue, 4 Oct 2016 09:41:29 -0600 Subject: [PATCH 1/2] Re-factor Docker+Vagrat+YAML environment Move host Vagrantfile into main directory, and refactor to use external YAML configuration file. Add multi-platform support. --- vagrant-docker-yaml/Vagrantfile | 20 ++++----- vagrant-docker-yaml/VagrantfileHost | 61 ++++++++++++++++++++++++++++ vagrant-docker-yaml/host/Vagrantfile | 27 ------------ vagrant-docker-yaml/hostvms.yml | 7 ++++ 4 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 vagrant-docker-yaml/VagrantfileHost delete mode 100644 vagrant-docker-yaml/host/Vagrantfile create mode 100644 vagrant-docker-yaml/hostvms.yml diff --git a/vagrant-docker-yaml/Vagrantfile b/vagrant-docker-yaml/Vagrantfile index 0aea644..52f5dc9 100644 --- a/vagrant-docker-yaml/Vagrantfile +++ b/vagrant-docker-yaml/Vagrantfile @@ -2,8 +2,8 @@ # vi: set ft=ruby : # Specify Vagrant version, Vagrant API version, and Vagrant clone location -Vagrant.require_version ">= 1.6.0" -VAGRANTFILE_API_VERSION = "2" +Vagrant.require_version '>= 1.6.0' +VAGRANTFILE_API_VERSION = '2' ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker' # Require 'yaml' module @@ -19,30 +19,30 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # Perform one-time configuration of Docker provider to specify # location of Vagrantfile for host VM; comment out this section # to use default boot2docker box - config.vm.provider "docker" do |docker| - docker.vagrant_vagrantfile = "host/Vagrantfile" + config.vm.provider 'docker' do |docker| + docker.vagrant_vagrantfile = 'VagrantfileHost' end # config.vm.provider # Iterate through the entries in the YAML file containers.each do |container| - config.vm.define container["name"] do |cntnr| + config.vm.define container['name'] do |cntnr| # Disable synced folders for the Docker container # (prevents an NFS error on "vagrant up") - cntnr.vm.synced_folder ".", "/vagrant", disabled: true + cntnr.vm.synced_folder '.', '/vagrant', disabled: true # Configure the Docker provider for Vagrant - cntnr.vm.provider "docker" do |docker| + cntnr.vm.provider 'docker' do |docker| # Specify the Docker image to use, pull value from YAML file - docker.image = container["image"] + docker.image = container['image'] # Specify port mappings, pull value from YAML file # If omitted, no ports are mapped! - docker.ports = container["ports"] + docker.ports = container['ports'] # Specify a friendly name for the Docker container, pull from YAML file - docker.name = container["name"] + docker.name = container['name'] end # contnr.vm.provider end # config.vm.define end # containers.each diff --git a/vagrant-docker-yaml/VagrantfileHost b/vagrant-docker-yaml/VagrantfileHost new file mode 100644 index 0000000..3ec98c6 --- /dev/null +++ b/vagrant-docker-yaml/VagrantfileHost @@ -0,0 +1,61 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Specify minimum Vagrant version and Vagrant API version +Vagrant.require_version '>= 1.6.0' +VAGRANTFILE_API_VERSION = '2' + +# Require 'yaml' module +require 'yaml' + +# Read YAML file with VM details (box, CPU, RAM, IP addresses) +# Edit host.yml to change VM configuration details +hostvms = YAML.load_file(File.join(File.dirname(__FILE__), 'hostvms.yml')) + +# Create and configure the VM(s) +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + + # Always use Vagrant's default insecure key + config.ssh.insert_key = false + + # Iterate through entries in YAML file to create VMs + hostvms.each do |host| + + # Configure the VMs per details in YAML file + config.vm.define host['name'] do |srv| + + # Don't check for box updates + srv.vm.box_check_update = false + + # Specify the hostname of the VM + srv.vm.hostname = host['name'] + + # Specify the Vagrant box to use (use VMware box by default) + srv.vm.box = host['vmw_box'] + + # Disable synced folders (prevents an NFS error on "vagrant up") + srv.vm.synced_folder '.', '/vagrant', disabled: true + + # Assign additional private network + if host['ip_addr'] != nil + srv.vm.network 'private_network', ip: host['ip_addr'] + end # if host['ip_addr'] + + # Configure CPU & RAM per settings in hostvms.yml (Fusion) + srv.vm.provider 'vmware_fusion' do |vmw| + vmw.vmx['memsize'] = host['ram'] + vmw.vmx['numvcpus'] = host['vcpu'] + end # srv.vm.provider 'vmware_fusion' + + # Configure CPU & RAM per settings in hostvms.yml (VirtualBox) + srv.vm.provider 'virtualbox' do |vb, override| + vb.memory = host['ram'] + vb.cpus = host['vcpu'] + override.vm.box = host['vb_box'] + end # srv.vm.provider 'virtualbox' + + # Provision the VM with Docker + srv.vm.provision 'docker' + end # config.vm.define + end # hostvsm.each +end # Vagrant.configure diff --git a/vagrant-docker-yaml/host/Vagrantfile b/vagrant-docker-yaml/host/Vagrantfile deleted file mode 100644 index 967b9bb..0000000 --- a/vagrant-docker-yaml/host/Vagrantfile +++ /dev/null @@ -1,27 +0,0 @@ -# -*- mode: ruby -*- -# vi: set ft=ruby : - -# Specify Vagrant version and Vagrant API version -Vagrant.require_version ">= 1.6.0" -VAGRANTFILE_API_VERSION = "2" - -# Create and configure the VM(s) -Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| - - # Assign a friendly name to this host VM - config.vm.hostname = "docker-host" - - # Skip checking for an updated Vagrant box - config.vm.box_check_update = false - - # Always use Vagrant's default insecure key - config.ssh.insert_key = false - - # Spin up a "host box" for use with the Docker provider - # and then provision it with Docker - config.vm.box = "slowe/ubuntu-trusty-x64" - config.vm.provision "docker" - - # Disable synced folders (prevents an NFS error on "vagrant up") - config.vm.synced_folder ".", "/vagrant", disabled: true -end diff --git a/vagrant-docker-yaml/hostvms.yml b/vagrant-docker-yaml/hostvms.yml new file mode 100644 index 0000000..9f67afb --- /dev/null +++ b/vagrant-docker-yaml/hostvms.yml @@ -0,0 +1,7 @@ +--- +- name: "default" + vmw_box: "slowe/ubuntu-trusty-x64" + vb_box: "ubuntu/trusty64" + ram: "1024" + vcpu: "1" + ip_addr: "192.168.100.101" From e3feb3361938529a78a95306b552daf215d866bc Mon Sep 17 00:00:00 2001 From: "Scott S. Lowe" Date: Wed, 5 Oct 2016 21:08:30 -0600 Subject: [PATCH 2/2] Update README.md Update README.md based on new architecture of the learning environment --- vagrant-docker-yaml/README.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/vagrant-docker-yaml/README.md b/vagrant-docker-yaml/README.md index fd1c600..77cad1c 100644 --- a/vagrant-docker-yaml/README.md +++ b/vagrant-docker-yaml/README.md @@ -1,23 +1,27 @@ # Running Multiple Docker Containers in Vagrant with YAML -These files were created to allow users to use Vagrant ([http://www.vagrantup.com](http://www.vagrantup.com)) to quickly and relatively easily launch multiple Docker ([http://www.docker.com](http://www.docker.com)) containers. The specifics of the Docker containers are specified in an external YAML file. The configuration was tested using Vagrant 1.7.2, VMware Fusion 6.0.5, and the Vagrant VMware plugin. +These files were created to allow users to use Vagrant ([http://www.vagrantup.com](http://www.vagrantup.com)) to quickly and relatively easily launch multiple Docker ([http://www.docker.com](http://www.docker.com)) containers. The specifics of the Docker containers are specified in an external YAML file. The configuration was tested using Vagrant 1.8.5, VMware Fusion 8.1.0 (with the Vagrant VMware plugin), and VirtualBox 5.1. ## Contents +* **containers.yml**: This YAML file contains a list of the Docker containers and their properties for use by Vagrant. You will need to edit this file to specify container friendly name, image, and exposed ports. + +* **hostvms.yml**: This YAML file contains the configuration data for the "host VM" that the Vagrant Docker provider will use. Edit this file to change the Vagrant box used by the "host VM," or the VM configuration. + * **README.md**: The file you're currently reading. -* **host/Vagrantfile**: This file is used by Vagrant to spin up a "host VM" for use with the Vagrant Docker provider. Edit this file to change the Vagrant box you'd like to use; by default, this Vagrantfile uses the "slowe/ubuntu-trusty-x64" box (built for the "vmware_desktop" provider). +* **VagrantfileHost**: This file is used by Vagrant to spin up a "host VM" for use with the Vagrant Docker provider. No edits should be necessary to this file. -* **Vagrantfile**: This file is used by Vagrant to spin up the Docker containers on the host VM created by `host/Vagrantfile`. Unless you change filenames, you should not need to edit this file. All container details are stored in a separate YAML file. - -* **containers.yml**: This YAML file contains a list of the Docker containers and their properties for use by Vagrant. You will need to edit this file to specify container friendly name, image, and exposed ports. +* **Vagrantfile**: This file is used by Vagrant to spin up the Docker containers on the host VM created by `VagrantfileHost`. Unless you change filenames, you should not need to edit this file. All container details are stored in a separate YAML file. ## Instructions -1. If you wish to use a box _other_ than my "ubuntu-trusty-x64" base box (running Ubuntu 14.04), edit the `Vagrantfile` in the host subdirectory. +1. Use `vagrant box add` to install an Ubuntu 14.04 base box. If you'd like to use a VMware Fusion-formatted box, you can use my base box ("slowe/ubuntu-trusty-x64"). -2. Edit the `containers.yml` file to specify the Docker containers that Vagrant should create on the host VM (specified by `host/Vagrantfile`). Each container should contain three properties: `name` (the friendly name to be assigned to the Docker container), `image` (the name of the Docker image to be used for this container), and `ports` (a list of ports to be exposed for the container). +2. Edit `hostvms.yml` to specify the name of the box you added in step #1. -3. From the directory where the main `Vagrantfile` is located, simply run `vagrant up` to spin up the specified host VM and specified Docker containers. Note that Internet access **will be** required to download Docker and the Docker images. +3. Edit the `containers.yml` file to specify the Docker containers that Vagrant should create on the host VM. Each container should contain three properties: `name` (the friendly name to be assigned to the Docker container), `image` (the name of the Docker image to be used for this container), and `ports` (a list of ports to be exposed for the container). + +4. From the directory where the main `Vagrantfile` is located, simply run `vagrant up` to spin up the specified host VM and specified Docker containers. Note that Internet access **will be** required to download Docker and the Docker images. Enjoy! \ No newline at end of file