Creating a Multi-Machine Vagrant Environment

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

By using a “Vagrantfile within a Vagrantfile” structure, we can create code that allows Vagrant to provision an environment comprised of multiple machines. This lets us create Vagrant environments that closely mimic production, separating out databases, services, applications, and more as needed. In this hands-on lab, we use the LXC provider to create two different machines and establish that the machines can communicate.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create Two Machines
  1. Move into the website directory and initialize a Vagrantfile:

    cd website
    vagrant init
  2. Update the Vagrantfile so two containers are created:

    vim Vagrantfile
    Vagrant.configure("2") do |config| = "fgrehm/trusty64-lxc"
      config.vm.provider "lxc"
      config.vm.define "web" do |web|
      config.vm.define "db" do |db|
Configure the Private Network

Still editing your Vagrantfile, add a private network to each guest:

Vagrant.configure("2") do |config| = "fgrehm/trusty64-lxc"
  config.vm.provider "lxc"
  config.vm.define "web" do |web| "private_network", ip: "", lxc__bridge_name: "brdg"
  config.vm.define "db" do |db| "private_network", ip: "", lxc__bridge_name: "brdg"

Save and exit the file.

Test the Environment
  1. Deploy the environment:

    vagrant up
  2. Log in to the web guest:

    vagrant ssh web
  3. View the IP of the guest:

  4. ping the db from the web guest:


Additional Resources

You have been tasked with creating a Vagrant environment that the front-end designers can use. This environment needs to include both a web and db host, and a private network needs to be established between them.

Write a Vagrantfile that provisions two LXC containers, and ensure both guests use the fgrehm/trusty64-lxc box. When configuring the network, set the IP for web to and the IP for db to Since you are using the LXC provider, you also need to provide the name of the bridge via the lxc__bridge_name option; set the bridge name to brdg.

Once finished, deploy the environment. Ensure the two guests can communicate.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?