Book Image

Vagrant Virtual Development Environment Cookbook

Book Image

Vagrant Virtual Development Environment Cookbook

Overview of this book

Table of Contents (17 chapters)
Vagrant Virtual Development Environment Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Mixed environments – the Docker provisioner


In addition to the Docker provider, Vagrant can help manage Docker containers and mixed environments using the Docker provisioner. The Docker provisioner can be used to build a virtual machine that hosts Docker containers, or perhaps a host that is provisioned with software with maybe one or two services managed by Docker containers. (For example, a virtual machine can be configured with a database or middleware installation managed in Docker containers, while the machine itself is configured to run a web application natively.)

The Docker provisioner will also manage the Docker runtime, which installs Docker on the virtual machine if necessary. In this example, we'll take a look at installing a MySQL database (using the Docker provisioner) and the MySQL image published on Docker Hub.

How to do it...

  1. Start with a simple Vagrantfile. This Vagrantfile defines a box (in this case, an Ubuntu image) and a Docker provisioner block:

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.box = "puppetlabs/ubuntu-14.04-64-nocm"
               config.vm.provision "shell", inline:"apt-get install -y mysql-client"
    
      config.vm.provision "docker" do |d|
        d.pull_images "library/mysql"
        d.run 'library/mysql',
              args: '-e MYSQL_ROOT_PASSWORD=password -p 3306:3306'
      end
    end

    This Vagrantfile also defines the Docker provisioner block. In this case, the block specifies the image to be pulled as well as a run command that will be executed on system startup.

    The image being pulled is the mysql image from the Docker Hub repository, the run command is similar to using Docker's command-line tools. In this case, the run command specifies an environment variable (MYSQL_ROOT_PASSWORD) and defines a port mapping from the container to the host.

  2. Start the Vagrant machine with a vagrant up command. When the Docker provisioner starts, the provisioner will install the latest version of the Docker runtime and start containers listed with the Docker run command.

  3. Verify that the Docker command works by accessing the virtual machine with the vagrant ssh command and executing docker ps. The process listing will show a running MySQL container:

    While we have an open terminal, obtain the IP address of the virtual machine (if this is not a fixed IP machine) with the ipconfig command.

  4. From the host operating system, access the MySQL database with the mysql client command line, making sure that you have a mysql client installed on your host operating system:

This is a simple example of using the Docker provisioner to install and start software services. The provisioner is designed in a way that allows Vagrant users to bootstrap an entire Vagrant environment that can support Docker and other services effectively, mimicking how a Docker host machine will operate. The provisioner also isolates Docker containers in the virtual machine itself as the provider does not rely on shared services or boot2docker to operate. As such, the use of the Docker provisioner is useful when simulating an entire software stack, where the Docker provider is focused on the container development and interaction.

See also