Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

Use open source Vagrant to deploy containers and VMs

With a few scripts, you can create virtual instances using the open source tool Vagrant, which is a cross between a container orchestrator and a VM orchestrator.

Vagrant, an open source product from HashiCorp, is something of a cross between a container orchestrator and a...

VM orchestrator. Vagrant makes it easier to deploy containers and VMs than Docker, and it takes less time to create a VM with Vagrant than Docker.

Vagrant enables the user to package his own development, test or production application and send that app to testers, users and developers, as well as migrate it through environments; the only requirements are that the target environment have Vagrant installed, a supported hypervisor and the BOX file that contains the Vagrant application.

Similar to how you would deploy containers with Docker, Vagrant allows you to package code, build completely installed systems and send them to or share them with others. Vagrant also provides automatic access to a shared file system, which requires an additional step in Docker.

Vagrant runs on most OSes, including Ubuntu, and uses the Oracle VirtualBox hypervisor.

Vagrant runs on most OSes, including Ubuntu, and uses the Oracle VirtualBox hypervisor. Vagrant also works with VMware, Hyper-V, Docker and Amazon Web Services, but be aware that VirtualBox doesn't always work on top of other hypervisors. If you try to install VirtualBox in a VM and receive the following error message, it means that the OS isn't able to access the virtualization capabilities of the Intel or Advanced Micro Devices chip: VBoxManage: error: VT-x is not available.

Vagrant package

Vagrant can automatically download packaged containers you've created, or if you prefer, you can download packages created by other users from the Vagrant public repository.

To get started, install Vagrant and Oracle VirtualBox, and then enter the following command:

vagrant init hashicorp/precise64

This downloads hashicorp/precise64, a small Linux OS, and creates a file called VagrantFile in the directory where you run Vagrant. The VagrantFile is where you do additional configuration. Next, write a file called bootstrap.sh to send instructions to the Vagrant container to execute specific tasks, such as installing software or configuring network port forwarding.

The Vagrant file should look like this:

               config.vm.box = "hashicorp/precise64"
     config.vm.provision :shell, path: "bootstrap.sh"

After you've downloaded the image and created a bootstrap.sh, deploy containers with the vagrant up command.

Vagrant creates public keys for whoever's currently logged in, so there's no need for a password. To log into the Vagrant container, run the vagrant ssh command, and use CTRL-D to log out.

Use the vagrant destroy command to shut down and free any resources.

Sample boostrap.sh

You can use a simple bootstrap.sh in Vagrant to install Nginx on the target container:

               #!/usr/bin/env bash

     sudo apt-get update
     sudo apt-get install nginx

You can't just copy HTML pages from your host computer to the target in bootstrap.sh -- instead, you have to use the following command:

               config.vm.provision "file" ...

The vagrant box add command allows you to deploy containers and send them to other people so that they can use your environments and apps. You can also expose the container with the vagrant share command. This command creates a file, Secure Shell or URL from which a user can access the container. To create a URL, you have to install Ngrok; Ngrok.io is where your URL is hosted.

Synced folders

When you launch Vagrant, it shares your project directory with the /vagrant folder on the target system. Vagrant then automatically syncs any files you create or edit to and from your local host and the guest machine, which allows you to debug and edit using your local machine with familiar tools. The synced folders feature also allows you to pick other folders to share and sync.

Next Steps

Follow this DevOps lab Vagrant tutorial

Learn the difference between containers and VMs

Navigate container and VM integration

This was last published in July 2017

Dig Deeper on Open source virtualization



Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

What else is important to know when deploying containers with Vagrant?