Archive for the ‘Programming’ Category

For Developers: Vagrant

Posted: January 5, 2017 in Programming

Vagrant is a tool for building complete development environments, sandboxed in a virtual machine. Vagrant lowers development environment setup time, increases development/production parity, and brings the idea of disposable compute resources down to the desktop.

With one command, Vagrant does all of the following:

  • Creates a virtual machine for you based on an operating system of your choice.
  • Modifies the physical properties of this virtual machine (e.g., RAM, number of CPUs, etc.).
  • Establishes network interfaces so that you can access your virtual machine from your own computer, another device on the same network, or even from another virtual machine.
  • Sets up shared folders so that you can continue editing files on your own machine and have those modifications mirror over to the guest machine.
  • Boots the virtual machine so that it is running.
  • Sets the hostname of the machine, since a lot of software depends on this being properly set.
  • Provisions software on the machine via a shell script or configuration management solution such as Chef, Puppet, or a custom solution.
  • Performs host and guest specific tweaking to work around known issues that may arise. For example, Ubuntu 12.04 LTS breaks VirtualBox networking defaults, so Vagrant makes minor modifications to some configuration in Ubuntu to make sure networking continues working. Vagrant does these sorts of things for many host/guest pairs.

This is all completed in about a minute, but the time it takes can greatly increase depending on the software being installed.

Once Vagrant finishes setting up the machine, you are left with a completely sandboxed, fully provisioned development environment. Due to the shared folders and networking, you continue using your own editor and your own browser to develop and test your applications, but the code itself runs on the virtual machine.

Vagrant handles the entire lifecycle of the machine for you, so in addition to setting up your development environment, Vagrant can do all of the following:

  • SSH into the machine.
  • Halt (shut down) the machine.
  • Destroy the machine, completely deleting its virtual hard drive and metadata.
  • Suspend or resume the machine.
  • Package the machine state so that you can distribute it to other developers.

Vagrant is a Swiss Army knife for development environments. It does everything you need to create and manage them, and helps enforce good practices by encouraging the use of automation and an environment that more closely resembles production.

If there is something Vagrant can’t do with your development environments, chances are that you can extend Vagrant’s behavior through a plug-in to achieve what you need. If this ends up being something that a lot of people need, then your plug-in could be merged back into Vagrant core, since Vagrant itself is open source. Thus, by using Vagrant, you’re using a tool that thousands of developers have contributed to based on their real-world needs.

Some of the Import Commands of Vagrant and its meanings:

  • box – manages boxes: installation, removal, etc.
  • cap – checks and executes capability
  • connect – connect to a remotely shared Vagrant environment
  • destroy – stops and deletes all traces of the vagrant machine
  • docker-exec – attach to an already-running docker container
  • docker-logs – outputs the logs from the Docker container
  • docker-run – run a one-off command in the context of a container
  • global-status – outputs status Vagrant environments for this user
  • halt – stops the vagrant machine
  • help – shows the help for a subcommand
  • init – initializes a new Vagrant environment by creating a Vagrantfile
  • list-commands – outputs all available Vagrant subcommands, even non-primary ones
  • login – log in to HashiCorp’s Atlas
  • package – packages a running vagrant environment into a box
  • plugin – manages plugins: install, uninstall, update, etc.
  • port – displays information about guest port mappings
  • powershell – connects to machine via powershell remoting
  • provider – show provider for this environment
  • provision – provisions the vagrant machine
  • push – deploys code in this environment to a configured destination
  • rdp – connects to machine via RDP
  • reload – restarts vagrant machine, loads new Vagrantfile configuration
  • resume – resume a suspended vagrant machine
  • rsync – syncs rsync synced folders to remote machine
  • rsync-auto – syncs rsync synced folders automatically when files change
  • share – share your Vagrant environment with anyone in the world
  • snapshot – manages snapshots: saving, restoring, etc.
  • ssh – connects to machine via SSH
  • ssh-config – outputs OpenSSH valid configuration to connect to the machine
  • status – outputs status of the vagrant machine
  • suspend – suspends the machine
  • up – starts and provisions the vagrant environment
  • version – prints current and latest Vagrant version