Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Vagrant Virtual Development Environment Cookbook

You're reading from   Vagrant Virtual Development Environment Cookbook 35 solutions to help you utilize virtualization with Vagrant more effectively – learn how to develop and manage Vagrant in the cloud to improve collaboration

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher
ISBN-13 9781784393748
Length 250 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Chad O Thompson Chad O Thompson
Author Profile Icon Chad O Thompson
Chad O Thompson
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Vagrant Virtual Development Environment Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Setting Up Your Environment FREE CHAPTER 2. Single Machine Environments 3. Provisioning a Vagrant Environment 4. Provisioning with Configuration Management Tools 5. Networked Vagrant Environments 6. Vagrant in the Cloud 7. Packaging Vagrant Boxes Vagrant Plugins A Puppet Development Environment Using Docker with Vagrant 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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images