Learn How Vagrant Boxes Are Packaged And Shared

Vagrant Boxes

Vagrant Boxes

In Vagrant, you can create a box by repackaging an environment you have created or you can create a box using minimum components needed for the box to function. Boxes that just have minimum components are referred to as base boxes. An example of a base box is precise64 which consists of a minimum ubuntu install instead of a repackaged environment. Base boxes provide a foundation for building environments. Creating base boxes is a resource intensive process, therefore new users are advised to find a base box they can build on.

A base box only contains minimum software. For example, a Linux box may include a package manager, SSH and a SSH user, and a configuration manager such as Chef. Other providers such as Hyper-V, Vmware, and Docker may need additional software. Guidelines for developing provider specific boxes are available, but we will not discuss them in this tutorial.

When building a base box, you need to consider the following factors listed below:

• Ensure there is adequate disk space. This will differ for each provider. For example, on a Virtualbox, you can create a dynamic drive and assign maximum size.
• Allocate an appropriate amount of memory. Avoid very high default values because the user can always override them in the Vagrantfile.
• Any unnecessary hardware such USB controllers need to be disabled as the user can always add them through the Vagrantfile

Vagrant is a highly customizable environment. For boxes that will be distributed publicly include default values needed for the box to work. For boxes that will be used privately, do not include defaults because they are known security risks. Default values that can be included are listed below:

• Vagrant needs a vagrant user for SSH connection. The default user is set up using an insecure key pair but commonly a password vagrant is assigned to default user to enable password login. The insecure key pair is implemented by placing the public key in ~/.ssh/authorized_keys directory of vagrant user. The ~/.ssh directory requires 0700 permissions while while the authorized keys need 0600 permissions.
• For boxes that will be distributed publicly a root password vagrant can be included which eases customization of the machine
• To enable network configuration, software installation and other requirements, it is important to ensure the default SSH user has a passwordless sudo configuration. Some operating systems lack sudo by default so ensure it is installed. Once sudo has been installed include this entry vagrant ALL=(ALL) NOPASSWD: ALL in your configuration file to enable passwordless sudo. If there is any entry that has requiretty, remove it
• To ensure your SSH is fast in your SSH server configuration, set UseDNS entry to no

Instead of building your base boxes manually, the recommended approach is using Packer for build reproducibility and Atlas for build automation. In this article, we will limit ourselves to demonstrating how Packer and Atlas can be used on Ubuntu.

To install Packer, navigate to this link https://www.packer.io/downloads.html download, unzip it into your preferred folder, enter the folder and add the folder to your path. To verify the installation was complete, the packer command should return available commands as shown in the output below.

install packer

To specify your build, you use template files written in JSON. Broadly build involves using a chosen ISO to create an operating system installation. The build specifics are ISO mounting, booting and OS UI initialization.

Consider the sample template provided below. At the beginning of the template, we have specified user variables. By using variables we improve template portability by keeping out sensitive and platform specific data. The key/value pair is the variable name and a default variable.

Before building an image from a template, you can check the validity of syntax and configuration values by passing the template name as shown below.


After your template is validated, you build it using the command shown below.

build packer

After the build process is complete, you can launch your image. Packer does not perform any image management.

Instead of creating a base box an easier approach is selecting an existing box, customizing it and repackaging. You begin by selecting a box that meets your needs. For this article, we will use precise64 from Hashicorp. Initialize, start and SSH into your box using the commands below:

init box

After entering your server, you can install all the software you need. In this article, we will demonstrate a minimal set up of a LAMP stack. The actual set up of LAMP is more involving. The commands below will install required software.

Use the command below to ensure your box has minimum size by cleaning up the disk space.

The last step is clearing your CLI and exiting the VM using the commands shown below.

To repackage the environment we created, we passed the name we would like our box assigned as shown below.

In this article, we introduced a special type of box known as a base box. We discussed considerations when creating a base box. We discussed some of the default values that can be included in a base box. We demonstrated how Packer can be used to build base boxes. Finally, we demonstrated how to package an existing environment into a box.


Please enter your comment!
Please enter your name here