Before we go into explanations, let's create the required configuration for Vagrant. Create a file named Vagrantfile
at the root of your project code base, and write the following code into it:
Vagrant.configure("2") do |config| # Which box we'll be using as base config.vm.box = "hashicorp/precise64" # Following is only for reference, as Vagrant knows # where to get the "precise64" box right from the start. # config.vm.box_url = "http://files.vagrantup.com/precise64.box" # What to do with the base box as initial setup config.vm.provision :shell, :path => "bootstrap/01-prepare-precise64.sh" config.vm.provision :shell, :path => "bootstrap/02-configure-app-for-precise64.sh" config.vm.provision :shell, :path => "bootstrap/03-prepare-application.sh" # How to expose the web application inside the box: # publish port 80 at the virtual machine as port 8888 at the host machine. config.vm.network "forwarded_port", guest: 80, host: 8888 end
This file is Ruby code. If you want, you can write arbitrary expressions here as long as you do Vagrant.configure
in it as well.
With this file in place, you can do the magic of vagrant up
. When you issue this command for the first time in an unprepared machine and code base, Vagrant will do the following:
box_url
. In our case, Vagrant knows already where the box named hashicorp/precise64
is.vagrant up
.config.vm.provision
setting. In our case, provisioning is split into three separate shell scripts, which must be run in exactly that order.It can do some other things as well, but we are interested only in these steps.
At the second and later invocations of vagrant up
, Vagrant will just launch the virtual machine and forward the ports. As was already said, the code base will be constantly shared between virtual and host machines.
It is obvious that Vagrant provides an enormously useful local deployment environment for developers. The most complex part is to correctly craft the provisioning scripts.
18.119.28.108