Vagrant - kamialie/knowledge_corner GitHub Wiki

Contents

General

Vagrant VM is called a box. Vagrant downloads images to the local cache (home directory .vagrant.d), which is specific to provider (f.e. VirtualBox) and also includes it's own Vagrantfile. Custom Vagrantfile is used to overwrite default settings.

Vagrant includes its own SSH client. While connecting to a box, account name and password are both vargrant.

By default Vagrant creates a directory /vagrant inside a box, which is synced with Vagrant environment directory on host.

Vagrant plugins

Networking

Port forwarding

The following line sets up port forwarding with the host (last argument restricts access only from host, removing it has the effect of exposing access to outside world):

config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

Private network

To get dynamic IP for Vagrant box add type: "dhcp" argument, instead of static one, defined in default configuration:

config.vm.network "private_network", type: "dhcp"

Public networks

Providers

Enable hypervisor to be used with Vagrant. VirtualBox and Hyper-V are built-in providers. Vagrant also has support for building Docker containers.

When starting a box with up command, specify different provider other than VirtualBox with --provider parameter.

Provisioners

Scripts that can be defined in Vagrantfile or in external files. By default, provisioners run only the first time box starts (that is on first up, but not reload). Use --provision parameter with vagrant up to force run provisioners. This can also be configured inside Vagrantfile with run: "always" on the provisioner.

Multibox

Define new object (box) in Vagrantfile with config.vm.define block. All statements are the same inside, f.e. provisioners, provider, etc.

Packaging

Essentially creating a new base box from an existing box.

Vagrantfile load order:

  • packaged Vagrantfile
  • Vagrantfile at ~/.vagrant.d (user-profile defaults)
  • local environment Vagrantfile

Local Vagrantfile is used to configure the future box. While packaging, another Vagrantfile can be included, which will provide default values for the box. Vagrantfile created by vagrant init will always have just the name of the box.

Package a box:

# Build the box
$ vagrant up

# Package the box
# NAME usually ends with .box
$ vagrant package --vagrantfile vagrant_file/Vagrantfile --output NAME

# Add the new box to local cache
# FILE_NAME is the output of previous command
$ vagrant box add BOX_NAME FILE_NAME

# Now change the directory and use the BOX_NAME to boot up new box
$ mkdir test && cd $_
$ vagrant init BOX_NAME
$ vagrant up

CLI

All commands regarding interaction with Vagrant box can be run locally (in Vagrant environment) or globally, by providing box ID as the last argument. Acquire ID by running global status command.

Initialize current directory as Vagrant environment; creates Vagrantfile and set the image name (from Vagrant Cloud):

$ vagrant init ORGANIZATION/IMAGE_NAME
# Downloads, if not present, the specified image and starts it:
$ vagrant up

# Check the status
$ vagrant status # in Vagrant environement
$ vagrant global-status # can be run from anywhere, lists all machines

# Connect to the box
$ vagrant ssh # in Vagrant environement

# Stop the machine (sends `SHUT_DOWN` signal, so that the machine can stop
# gracefully)
$ vagrant halt

# Completely delete associated files for the current box
$ vagrant destroy

# Restart the box - shortcut for hault and up commands, which makes the
# changes in Vagrantfile to take effect
$ vagrant reload

# List boxes installed in local cache
$ vagrant box list

Get stats while inside the box:

$ vmstat -s # memory among other thigs
$ lscpu # virtual cpu

Snapshoting is a common hypervisor capability. It is possible to take a snapshot of the box, do some tests and then roll back.

$ vagrant up

# Save a snapshot
$ vagrant snapshot save NAME

# do some changes

$ vagrant shapshot save ANOTHER_NAME

# List available snapshots
$ vagrant snapshot list

# Restore previous state
# --no-provision ensures provisioners do not run
$ vagrant snapshot restore NAME --no-provision

Suspend VM and resume (similar as sleep mode for laptop) that is RAM contents are saved to disk and VM is ready to start from the same state.

$ vagrant suspend
$ vagrant resume

References