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

Vagrant Virtual Development Environment Cookbook

Chad Thompson

35 solutions to help you utilize virtualization with Vagrant more effectively – learn how to develop and manage Vagrant in the cloud to improve collaboration
eBook
$10.00
RRP $26.99
Save 62%
Print + eBook
$44.99
RRP $44.99
What do I get with a Mapt subscription?
  • Unlimited access to all Packt’s 6,000+ eBooks and Videos
  • 100+ new titles a month, learning paths, assessments & code files
  • 1 Free eBook or Video to download and keep every month after trial
What do I get with an eBook?
  • Download this book in EPUB, PDF, MOBI formats
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the subscription reader
What do I get with Print & eBook?
  • Get a paperback copy of the book delivered to you
  • Download this book in EPUB, PDF, MOBI formats
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the subscription reader
What do I get with a Video?
  • Download this Video course in MP4 format
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the subscription reader
$10.00
$44.99
RRP $26.99
RRP $44.99
eBook
Print + eBook

Frequently bought together


Vagrant Virtual Development Environment Cookbook Book Cover
Vagrant Virtual Development Environment Cookbook
$ 26.99
$ 10.00
Creating Development Environments with Vagrant - Second Edition Book Cover
Creating Development Environments with Vagrant - Second Edition
$ 26.99
$ 10.00
Buy 2 for $20.00
Save $33.98
Add to Cart

Book Details

ISBN 139781784393748
Paperback250 pages

Book Description

Vagrant allows you to use virtualization and cloud technologies to power faster, efficient, and sharable development environments. It duplicates the development environment to allow users to easily share and combine data on different machines and also takes care of security concerns.

Each recipe of Vagrant Virtual Development Environment Cookbook provides practical information on using Vagrant to solve specific problems and additional resources to help you learn more about the techniques demonstrated.

With recipes ranging from getting new users acquainted with Vagrant, to setting up multimachine environments, you will be able to develop common project types and solutions with the help of this practical guide.

 

 

Read an Extract from the book

Using existing virtual machines with Vagrant

We can repackage existing environments to use with Vagrant, replacing the shared disk with a new box file. While box files are still essentially virtual machines, boxes can be published (and updated) and even have additional configuration applied after booting. These box files can make managing virtual machines and different versions of these virtual machines vastly simpler, especially, if you don't want to build environments from base boxes every time.

Getting ready

In this example, we'll assume that we have an existing virtual machine environment built with Oracle VirtualBox.

Warning!

This example will use a VirtualBox-only feature to set up box packaging, as Vagrant has a built-in shortcut to package existing VirtualBox environments into box files. Creating Vagrant boxes for other platforms will be covered in later chapters.

In this example, I'll choose an existing environment based on the CentOS operating system that has been created as a VirtualBox machine. In this case, the virtual machine has a few properties we'll want to note:

  • In this case, the virtual machine was created from an ISO installation of CentOS 6.5.
  • There is a user account present on the machine that we want to reuse. The credentials are:

    Username

    uaccount

    Password

    passw0rd

  • The machine is used as a development web server; we typically access the machine through terminal sessions (SSH).

    WARNING!

    We'll want to make sure that any machine that we will access using the vagrant ssh method has the SSH server daemon active and set to start on machine start. We'll also want to make one adjustment to the sshd configuration before packaging.

    With the machine created and active on a development workstation, the listing appears in the VirtualBox Manager console:

    If this machine boots locally and allows us to SSH into the machine normally, we can proceed to convert the virtual machine into a Vagrant box.

How to do it...

If we use an existing virtual machine with VirtualBox, we can use a few simple commands to export a new Vagrant box.

Packaging the VirtualBox machine

Before we can use the virtual machine with Vagrant, we need to package the machine into an appropriate box format.

  1. Note the name that VirtualBox assigns to the machine. In this case, our virtual machine is named CentOS as displayed in the left-hand menu of the VM VirtualBox Manager console:
  2. Create a temporary workspace to package the box. As with all Vagrant commands, you will do this on the command line. If you are working on a Unix machine (Linux or OS X), you can create a working directory with the mkdir ~/box-workspace command. This will create a folder in your home directory called box-workspace. Change directories to this workspace with cd ~/box-workspace.
  3. Execute the packaging command. (Warning! This is for VirtualBox only.) This command is:Vagrant Virtual Development Environment Cookbook
  4. We'll discuss a bit more about this in the following section. For now, Vagrant will return some text:==> CentOS: Exporting VM...This command might take some time to execute; Vagrant is copying the existing VirtualBox machine into a box file along with some metadata that allows Vagrant to recognize the box file itself. When this command is finished, you'll end up with a single file called centos64.box in the working directory.
  5. Import the box file into your environment. In this case, we will directly add the box for use to our local Vagrant environment so that we can proceed to test the new box with a Vagrantfile. It is also possible at this stage to simply publish the box to a web server for use by others, but it is highly recommended to attempt to boot the box and access it with an example Vagrantfile. Your users will appreciate it. Add the box with the command:vagrant box add centos64.box --name=centos64This command will copy the box to your local Vagrant cache, so you are now ready to directly use the box!

Configuring a Vagrant environment

Now that the box is added to our local cache, we'll need to configure a new Vagrant environment to use the box.

Initialize a new Vagrant environment with our new box. Do this by executing the vagrant init centos64 command. This will create a basic Vagrantfile that
uses our new centos64 box.

  1. Configure the Vagrantfile to use the correct user to SSH into the machine. We'll use the supplied username and password given in the preceding table. Edit the Vagrantfile created in the previous step to include two new lines that specify parameters for the config.ssh parameters:Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # All Vagrant configuration is done here. The most common configuration # options are documented and commented below. For a complete reference, # please see the online documentation at vagrantup.com. # Every Vagrant virtual environment requires a box to build off of. config.vm.box = "centos64" config.ssh.username="uaccount" config.ssh.password="passw0rd"
  2. By default, Vagrant relies on a common public key that is used by most box publishers that allows access to an account called vagrant. In this case, our environment will not have this key installed, we can instead configure the Vagrantfile to use a username and password. After the first login, Vagrant will place a key in the appropriate account; so, if desired, the password can be removed from the Vagrantfile after the first boot.
  3. Boot the environment. You might need to specify the provider along with the vagrant up command:vagrant up --provider=virtualboxIn this case, you will note quite a bit of output; the typical Vagrant boot messages along with some information about logging in with the password, replacing the password with the key, and so on. You might also (depending on how the box was packaged) see some information about Guest Additions. While Vagrant can use a virtual machine that has the guest additions disabled, some features (shared folders, port forwarding, and so on) rely on the VirtualBox guest additions to be installed. It's likely that your virtual machine has these already installed, especially if it has been used previously in a VirtualBox environment. Newly packaged boxes, however, will need to have the guest additions installed prior to packaging. (See the VirtualBox manual on the installation of guest additions at https://www.virtualbox.org/manual/ch04.html.)

Once the environment is booted, you can interact with the virtual machine, as you did previously, either through SSH or other services available on the machine.

How it works...

Using Vagrant with virtual machines is entirely dependent on the Vagrant box format. In this example, we used a built-in feature of Vagrant to export an existing VirtualBox environment into Vagrant. It's also possible to package box files for other environments, a topic we'll revisit later in the book. In this case, the package command generated a box file automatically.

The Vagrant box file is a file in a Unix Tape ARchive (TAR) format. If we untar the box file with the tar xvf centos64.box command, we can look at the contents of the box to see how it works. The following are the contents of the untarred file:

-rw------- 0 cothomps staff 1960775680 Jul 24 20:42 ./box-disk1.vmdk -rw------- 0 cothomps staff 12368 Jul 24 20:38 ./box.ovf -rw-r--r-- 0 cothomps staff 505 Jul 24 20:42 ./Vagrantfile

So, the box file contains two files required to operate a VirtualBox virtual machine (the vmdk file that defines the virtual hard drive, and the ovf file that defines the properties of the virtual machine used by VirtualBox). The third file is a custom Vagrantfile that contains (primarily) the MAC address of the packaged virtual machine. There might also be custom files added to packaged boxes (such as metadata), describing the box or custom files required to operate the environment.

 

Table of Contents

What You Will Learn

  • Define single and multiple virtual machine Vagrant environments
  • Provision Vagrant environments in a consistent and repeatable manner with various configuration management tools
  • Control powerful cloud resources from a desktop development environment
  • Use Vagrant to publish and share development environments
  • Start and expand your Vagrant environment with community resources
  • Share resources on a development machine with a virtual Vagrant environment

Authors

Table of Contents

Book Details

ISBN 139781784393748
Paperback250 pages
Read More

Read More Reviews

These popular $10 titles might interest you

Creating Development Environments with Vagrant - Second Edition Book Cover
Creating Development Environments with Vagrant - Second Edition
$ 26.99
$ 10.00
Learning Ansible 2 - Second Edition Book Cover
Learning Ansible 2 - Second Edition
$ 35.99
$ 10.00
Learning Ansible 2 - Second Edition Book Cover
Learning Ansible 2 - Second Edition
$ 35.99
$ 10.00
KVM Virtualization Cookbook Book Cover
KVM Virtualization Cookbook
$ 35.99
$ 10.00
Building RESTful Web services with Go Book Cover
Building RESTful Web services with Go
$ 35.99
$ 10.00
SQL Server on Linux Book Cover
SQL Server on Linux
$ 31.99
$ 10.00