|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Vagrant kick start tips" |
| 4 | +date: 2017-04-24 09:40:49 -0500 |
| 5 | +categories: vagrant |
| 6 | +tags: general |
| 7 | +--- |
| 8 | +# Kick start with vagrant |
| 9 | + |
| 10 | +[Vagrant](https://www.vagrantup.com/intro/index.html) is popular tool used for helping local development and testing. Here are some tips that has been very useful for me when setting up and using Vagrant |
| 11 | + |
| 12 | +## Use box file |
| 13 | + |
| 14 | +A common practice when using vagrant is to use shell script to provision the vm, for example: |
| 15 | + |
| 16 | +in Vagrantfile |
| 17 | + |
| 18 | +{% highlight ruby %} |
| 19 | +vm4.vm.provision "shell", path: "VagrantProvision/provision_centos7.sh" |
| 20 | +{% endhighlight %} |
| 21 | + |
| 22 | +in provision_centos7.sh |
| 23 | + |
| 24 | +{% highlight Bash shell scripts %} |
| 25 | + |
| 26 | +#!/bin/bash |
| 27 | +set -x |
| 28 | + |
| 29 | +sudo rpm -iUvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm |
| 30 | +sudo yum update -y && sudo yum install -y net-tools wget ansible python-docker-py python-prettytable |
| 31 | + |
| 32 | +# More setup ... |
| 33 | + |
| 34 | +{% endhighlight %} |
| 35 | + |
| 36 | +While This is helpful to make sure the changes made to the base image are versioned and can be share within teams, it can be slow when a base box needs to get build every time. |
| 37 | + |
| 38 | +A better way would be to build the box file which just need to build once which will speed up the vm start up time significantly. |
| 39 | + |
| 40 | +First, let's define our base image in Vagrantfile: |
| 41 | + |
| 42 | +Here we use centos 7 as an example: |
| 43 | + |
| 44 | +{% highlight ruby %} |
| 45 | + |
| 46 | +config.vm.define "base" do |base| |
| 47 | + config.vm.provider :virtualbox do |vb| |
| 48 | + vb.name = "base" |
| 49 | + end |
| 50 | + base.vm.box = "centos/7" |
| 51 | + # config local network |
| 52 | + base.vm.network "private_network", ip: "192.168.3.9" |
| 53 | + base.vm.provision "shell", path: "VagrantProvision/provision_centos7.sh" |
| 54 | +end |
| 55 | + |
| 56 | +{% endhighlight %} |
| 57 | + |
| 58 | +Sample script for building box file : |
| 59 | + |
| 60 | +{% highlight Bash shell scripts %} |
| 61 | + |
| 62 | +echo "creating dir to hold box file ..." |
| 63 | +mkdir Boxes |
| 64 | + |
| 65 | +echo "making sure base vm is down ..." |
| 66 | +vagrant destroy -f base |
| 67 | + |
| 68 | +echo "starting base vm ..." |
| 69 | +vagrant up base |
| 70 | + |
| 71 | +echo "starting cleaning up old box file ..." |
| 72 | +rm -f Boxes/centos7.box |
| 73 | +vagrant box remove --force file://Boxes/centos7.box |
| 74 | + |
| 75 | +echo "packing the new image into Boxes/centos7.box" |
| 76 | +vagrant package --base base -o Boxes/centos7.box |
| 77 | + |
| 78 | +echo "cleaning up base vm ..." |
| 79 | +vagrant destroy -f base |
| 80 | + |
| 81 | +{% endhighlight %} |
| 82 | + |
| 83 | +After the box file is build, change the vagrant file to refer to the local box file: |
| 84 | + |
| 85 | +{% highlight ruby %} |
| 86 | +vm1.vm.box = "file://Boxes/centos7.box" |
| 87 | +{% endhighlight %} |
| 88 | + |
| 89 | +Here we can skip the provisioning step when start the vm since all the changes needed as pre-build in the box file |
| 90 | + |
| 91 | + |
| 92 | +## Set up http proxy for vm |
| 93 | + |
| 94 | +## Separate vm definition from Vagrant code |
| 95 | + |
| 96 | +## Use vagrant cacher to speed up provision time |
0 commit comments