# vagrant --version
Vagrant 1.8.1
# vagrant box list
CentOS/7 (virtualbox, 1603.01)
Ubuntu/trusty64 (virtualbox, 20160406.0.0)
# vagrant box add ADDRESS
Vagrant可以從這裡https://atlas.hashicorp.com/boxes/search 下載各種Vagrant映像文件。
# vagrant box add ubuntu/trusty64
# vagrant box add https://atlas.hashicorp.com/ubuntu/boxes/trusty64
# vagrant box add CentOS7.1 file:///D:/Work/VagrantBoxes/CentOS-7.1.1503-x86_64-netboot.box
# vagrant init ubuntu/trustry64
此命令會在當前目錄創建一個名為Vagrantfile的配置文件,內容大致如下:
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
end
當在此目錄啟動Vagrant後,Vagrant會從互聯網下載“ubuntu/trusty64”這個box到本地,並使用它作為VM的映像。
要搜索可用的box,查看這裡: https://atlas.hashicorp.com/boxes
# vagrant up
如果我們想啟動任意VM,首先進入有Vagrantfile配置文件的目錄,然後執行上面的命令。控制台的輸出通常如下:
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'ubuntu/trusty64-juju' could not be found. Attempting to find a
nd install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'ubuntu/trusty64-juju'
default: URL: https://atlas.hashicorp.com/ubuntu/trusty64-juju
==> default: Adding box 'ubuntu/trusty64-juju' (v20160707.0.1) for provider: vir
tualbox
default: Downloading: https://atlas.hashicorp.com/ubuntu/boxes/trusty64-juju
/versions/20160707.0.1/providers/virtualbox.box
==> default: Waiting for cleanup before exiting...
default: Progress: 0% (Rate: 0/s, Estimated time remaining: --:--:--):--)
進入Vagrantfile配置文件所在的目錄,執行以下命令:
# vagrant ssh
要注意,本機上必須先安裝SSH客戶端。
進入Vagrantfile配置文件所在的目錄,執行以下命令:
# vagrant halt
# vagrant destory [name|id]
比如:
vagrant destroy ubuntu/trusty64
此命令會停止VM的運行,並銷毀所有創建的資源。