On Github ohskylab / hmtl-vagrant-talk
From the Vagrant site:
"Create and configure lightweight, reproducible, and portable development environments."
Helps you run an app in a virtual machine and manage it with ease
Host: your actual, physical machine
Guest: a virtual machine running on your host
NB: VMware also supported (more stable, not free)
Just a config file. Have a basic one created for you:
vagrant init precise64 http://files.vagrantup.com/precise64.box
Should specify:
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, host: 4567, guest: 4567 config.vm.provision :shell, :path => "provision.sh" end
Works with:
Let's use shell scripts
#!/usr/bin/env bash # Update system package lists sudo apt-get update # Install necessary system packages sudo apt-get install -y \ # automatic yes to prompts with -y build-essential \ nodejs \ # Install Bundler (no docs) gem install --no-rdoc --no-ri \ bundler \ # Bundle all the gems in the gemfile cd /vagrant/www bundle install
All done.
Now:
$ vagrant up
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... [default] Importing base box 'precise64'... [default] Matching MAC address for NAT networking... [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] Forwarding ports... [default] -- 22 => 2222 (adapter 1) [default] -- 4567 => 4567 (adapter 1) [default] Booting VM... [default] Waiting for VM to boot. This can take a few minutes. [default] VM booted and ready for use! [default] Configuring and enabling network interfaces... [default] Mounting shared folders... [default] -- /vagrant [default] Running provisioner: shell... [default] Running: /var/folders/2j/lkj6zqjx3hd6wzwsz2v55bmw0000gn/T/vagrant-shell20130612-1787-14r3iia # a few minutes + lots of install logging
Common commands:
First use also runs provisioning and starts the VM
After that, it just starts the VM
$ vagrant ssh Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64) * Documentation: https://help.ubuntu.com/ Welcome to your Vagrant-built virtual machine. Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2 vagrant@precise64:/vagrant$ pwd /vagrant vagrant@precise64:/vagrant$ exit logout Connection to 127.0.0.1 closed.
$ vagrant suspend [default] Saving VM state and suspending execution...
$ vagrant halt [default] Discarding saved state of VM...
Doesn't affect your project files or the base box.
$ vagrant destroy Are you sure you want to destroy the 'default' VM? [y/N] y [default] Forcing shutdown of VM... [default] Destroying VM and associated drives...
Like vagrant up, but runs the provision script again.
The same as running vagrant up for the first time.
If you can run an app on your actual, physical machine, why bother running them in a Vagrant VM?
Put the Vagrantfile and provision script under version control.
All team members can run the same stack on the same (virtual) OS.
No more:
If you are:
$ git clone https://github.com/user/project-name.git project-name $ cd project-name $ cat README.md $ vagrant up
Done (pretty much)
Documentation is good; code is better.
Written instructions on how to get an app running can get out of date quickly.
You're not forced into using Vagrant: you can still install the dependencies on your host machine and run the app there, if you like.
Fanks
Presented at HMTL, 12th June 2013
Built with Reveal.js