vagrant筆記
Vagrant Official Site
Place Where You Can Download And Upload Box

1 General

1.1 Path

下載的box預設放在~/.vagrant.d/boxes裡
guest machine的data放的地方根據VM設定而不同可開啟VirtualBox查看,VirtualBox預設是在~/VirtualBox\ VMS

1.2 Shared Folder

guest machine的/vagrant將會自動mount到host machine的放Vagrantfile的那個folder
此功能需要guest machine的Box的VB guest additions版本與VM中的一樣
若出現錯誤則需要更新box或用這個非官方的plugin: https://github.com/dotless-de/vagrant-vbguest

2 Uninstall

2.1 REMOVING THE VAGRANT PROGRAM

rm -rf /Applications/Vagrant
rm -f /usr/bin/vagrant
sudo pkgutil --forget com.vagrant.vagrant

2.2 REMOVING USER DATA

rm -rf  ~/.vagrant.d

3 Command

https://www.vagrantup.com/docs/cli/

vagrant init #create Vagrantfile in current folder
vagrant box add USER/BOX #add a box from https://atlas.hashicorp.com/boxes/search
vagrant up #creates and configures guest machines according to your Vagrantfile
vagrant ssh #SSH into a running Vagrant machine and give you access to a shell
vagrant up --provider=vmware_fusion #indicate the provider

vagrant destroy #stops the running machine Vagrant is managing and destroys all resources that were created during the machine creation process.
vagrant halt #shutdown the running machine
vagrant box remove NAME --all #remove box
vagrant halt -f #force shutdown running maching that means to turn off the power
vagrant suspend #save the current running state of the machine and stop it

vagrant reload # == halt and up, use it after Vagrantfile modification
vagrant reload --provision #provision does not run again by default, this command force run provision

3.1 halt vs destory vs box remove

vagrant halt: 將machine關機
vagrant destroy: 將machine關機,且將該machine移除,但box還留著
vagrant box remove: 移除box

4 Provision

example: 安裝apache server and 將DocumentRoot指向/vagrant when provision
bootstrap.sh

#!/usr/bin/env bash

apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
rm -rf /var/www
ln -fs /vagrant /var/www
fi

Vagrantfile

Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision :shell, path: "bootstrap.sh"
end

記得run vagrant reload –provision

5 Network

5.1 Port Forwarding

Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 4567
end

then run vagrant reload

5.2 Private Network

Vagrant.configure("2") do |config|
config.vm.network "private_network", type: "dhcp"
end
Last Updated 2017-03-10 五 10:26.
Render by hexo-renderer-org with Emacs 25.2.1 (Org mode 8.2.10)