vagrant的學習 之 打包分發
一、打包Box:
(1)關閉虛擬機。
vagrant halt
(2)打包:
vagrant package
這樣打包出來的文件叫package.box。
指定生成的包名字:
vagrant package --output ubuntu.box
二、升級Box:
老用戶,可以修改 Vagrantfile文件,
新用戶,直接使用新的box文件。
修改vagrantfile,找到這段代碼:
# config.vm.provision "shell", inline: <<-SHELL # apt-get update # apt-get install -y apache2 # SHELL end
例如本機新增了redis服務,打開注釋,增加安裝redis的命令:
config.vm.provision "shell", inline: <<-SHELL # apt-get update # apt-get install -y apache2 apt-get install -y redis-server SHELL
然后進行重啟:
vagrant reload --provision
三、使用打包的box,搭建環境:
vagrant box add ubuntu1404-2 package.box
然后會看到有兩個box:
vagrant box list ubuntu1404 (virtualbox, 0) ubuntu1404-2 (virtualbox, 0)
然后新建一個目錄,例如study2,開始創建:
vagrant init ubuntu1404-2
顯示結果:
A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
然后使用vagrant up啟動:
vagrant up
如果發生錯誤,可以打開新生成的Vagrantfile文件調試錯誤,
找到vb.gui = true,打開注釋,注意第一行和end那一行也得打開。
config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" end
然后執行,vagrant reload,注意打包時需要將ip的設置關閉,否則會報錯。
然后啟動新生成的虛擬機,查看LAMP和LNMP環境是否正常,配置好IP地址和hosts文件,就可以了,安裝完成!