Vagrant Getting Started Create and Share Virtual Environments
Reading Time:
Reading Time:
The official Vagrant docs states it as being a, “Easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.”
Vagrant uses the Base Image and applies the configuration options present in the Vagrantfile and creates a new Virtual Machine which can then be provisioned to our need.
Here VirtualBox is used with Vagrant since it is the best free VM available.
First get Vagrant and Virtual Box
Once you download and install the above packages you will have a vagrant
command in your system which will be added to your system path. Check the version of Vagrant by typing vagrant -v
.
Once a vagrant machine is created, provisioning tools such as shell scripts and Chef can be used to install and configure software packages into the machine.
When everything is in place, run vagrant init
in a directory where you need to initialize the vagrant. This will create a Vagrantfile
inside that directory. This is the config file that vagrant uses to configure the Base Image.
Vagrant uses Base Images to clone and create a new Virtual Machine. These Base Images are called as Boxes. Boxes are available at HashiCorp’s Atlas Box Catelogue. When found the right box required use the following command to add it.
vagrant box add [options] <name, url, or path>
vagrant box add ubuntu/trusty64
Here we are using the ubuntu/trusty64 for example.
Once it has finished downloading the Base Image, modify the Vagrantfile to inform Vagrant on which base image to use.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
end
To initiate a vagrant machine run the vagrant up
command. This will setup up a new Virtual Machine with the configuration options applied from the Vagrantfile and will be running.
Once the VM is up and running, you can access it using SSH by running vagrant ssh
command. This will SSH you into the VM that is running.
With Sync Folders it is easy to access files to and from the Host and Guest machines. By default the Guest machine shares the folder /vagrant
to the host machine, which in host machine is the folder where we created the Vagrant i.e where the Vagrantfile is present. This folder is in sync with the /vagrant
in the guest machine.
Create a directory name www
in the directory where the Vagrantfile file is present as shown below.
When viewed from inside the guest machine, the folder that we created will be available inside the guest’s /vagrant
folder.
You can also configure the synced_folder as shown below.
Note: There are also options that can passed to modify the behaviour of synced_folder like create option, which is a boolean that creates the host path if it does not exists.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.synced_folder "src/", "/srv/website", create: true
end
We have setup an environment with Ubuntu and have a folder synced to the host machine. But what good does it do if there are not other software packages. Even if we install the software packages by SSHing into the VM, it won’t do any good if we wanted to share it with others as they would have to do the same process and must have the same version that we used to develop.
This is where provisioning comes into play. You can specify a way for Vagrant to provision the list of software packages to be installed while running vagrant up
and vagrant reload --provision
commands.
Create a new setup.sh
shell script inside vagrant/
and add the following script.
#!/usr/bin/env bash
apt-get update
# Install apache, php - Redirect output and error into setup.log
apt-get install -y php5 apache2 libapache2-mod-php5 2>&1> setup.log
if ! [ -L /var/www ]; then
rm -rf /var/www/
# Created symbolic link.
ln -fs /vagrant/www /var/www/html
fi
Then modify the Vagrantfile by adding the following config option.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.synced_folder "src/", "/srv/website", create: true
config.vm.provision :shell, path: "setup.sh"
end
This config tells Vagrant to run this setup provision when it loads the VM or it can be forced to just run the provision.
Now run vagrant reload --provision
if you have you VM running already or vagrant up
to provision the softwares and start the machine. The above script will install php5 and apache2 inside the guest machine.
Create a new index.php
file inside a folder www
within vagrant/
.
Run the following code which will output Hello world from the index.php.
wget -qO- 127.0.0.1 index.php
So we have a web server running inside the guest machine, but it’s now use until we see it in a browser right. Let’s get it accessible in the Host machine’s browser using the Networking feature in Vagrant.
To access the guest machines server(apache2) which is running in a specific port(80) inside the guest, we use port forwarding. Which is basically saying, forward all the request to a port(3333) in the Host machine to the port(80) on the guest machine.
So a request to http://127.0.0.1:3333
is forwarded to the guest machines port 80 where apache is running.
Add the following line into the Vagrantfile.
config.vm.network :forwarded_port, guest: 80, host: 3333
You get the idea right. Let’s run vagrant up
if the machine is not running and vagrant reload
if it’s not running.
In a browser in the Host machine visit the url http://127.0.0.1:3333
and you will see the following output.
It’s was really easy to setup port forwarding and access the server running inside the guest machine. What if someone else wants to access the VM Guest machine. It can be done using Vagrant share. The first step to achieve this is to create an account at HashiCorp’s Atlas.
After you have signed-up login into Hashicorp using vagrant login
and providing your credentials.
Now run vagrant share
which provides you an URL which you can use in any system connected to the internet to access the apache server running inside the guest machine.
$ vagrant share
...
==> default: Your Vagrant Share is running!
==> default: URL: http://ubuntu-trusty-0345.vagrantshare.com
Of Course apart from just accessing the http server, you can connect to the guest using ssh.
$ vagrant share --ssh
The above command gives you an Name which can be used in connecting to that machine using ssh by using that name vagrant share --ssh Name
.
There are 3 ways you can clean up the vagrant that is running,
When you decide as to run which command use it with vagrant
.
vagrant [,suspend, halt, destroy]
Using vagrant is a lot of ease in development and could be used in a lot of situations to make the development process better. To know more view the Vagrant Documentation