From d51e3ba62e5a4ec46785014a8f9fa880cd44a527 Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Wed, 11 Feb 2015 10:11:27 -0700 Subject: [PATCH] Add files for multi-container Docker environments with Vagrant Add support files to allow users to specify a list of containers in a YAML file and then use Vagrant to turn up all the containers. --- vagrant-docker-yaml/README.md | 23 +++++++++++++ vagrant-docker-yaml/Vagrantfile | 49 ++++++++++++++++++++++++++++ vagrant-docker-yaml/containers.yml | 7 ++++ vagrant-docker-yaml/host/Vagrantfile | 27 +++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 vagrant-docker-yaml/README.md create mode 100644 vagrant-docker-yaml/Vagrantfile create mode 100644 vagrant-docker-yaml/containers.yml create mode 100644 vagrant-docker-yaml/host/Vagrantfile diff --git a/vagrant-docker-yaml/README.md b/vagrant-docker-yaml/README.md new file mode 100644 index 0000000..fd1c600 --- /dev/null +++ b/vagrant-docker-yaml/README.md @@ -0,0 +1,23 @@ +# 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. + +## Contents + +* **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). + +* **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. + +## 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. + +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). + +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. + +Enjoy! \ No newline at end of file diff --git a/vagrant-docker-yaml/Vagrantfile b/vagrant-docker-yaml/Vagrantfile new file mode 100644 index 0000000..b9250a0 --- /dev/null +++ b/vagrant-docker-yaml/Vagrantfile @@ -0,0 +1,49 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Specify Vagrant version and Vagrant API version +Vagrant.require_version ">= 1.6.0" +VAGRANTFILE_API_VERSION = "2" +ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker' + +# Require 'yaml' module +require 'yaml' + +# Read details of containers to be created from YAML file +# Be sure to edit 'containers.yml' to provide container details +containers = YAML.load_file('containers.yml') + +# Create and configure the Docker container(s) +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" + end + + # Iterate through the entries in the YAML file + containers.each do |container| + 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 + + # Configure the Docker provider for Vagrant + cntnr.vm.provider "docker" do |docker| + + # Specify the Docker image to use, pull value from YAML file + docker.image = container["image"] + + # Specify port mappings, pull value from YAML file + # If omitted, no ports are mapped! + docker.ports = container["ports"] + + # Specify a friendly name for the Docker container, pull from YAML file + docker.name = container["name"] + end + end + end +end diff --git a/vagrant-docker-yaml/containers.yml b/vagrant-docker-yaml/containers.yml new file mode 100644 index 0000000..5816ab6 --- /dev/null +++ b/vagrant-docker-yaml/containers.yml @@ -0,0 +1,7 @@ +--- +- name: nginx-01 + image: nginx + ports: ['80:80', '443:443'] +- name: redis-01 + image: redis + ports: ['6379:6379'] diff --git a/vagrant-docker-yaml/host/Vagrantfile b/vagrant-docker-yaml/host/Vagrantfile new file mode 100644 index 0000000..967b9bb --- /dev/null +++ b/vagrant-docker-yaml/host/Vagrantfile @@ -0,0 +1,27 @@ +# -*- 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