如何使用Vagrant創建一臺虛擬機

版本說明

Virtual Box版本:5.2.10
Vagrant版本:2.0.4
電腦系統:windows10 64位
虛擬機:ubuntu 14.0

尋找Box

官網有提供一些Box的下載可以使用,網址是:
https://app.vagrantup.com/boxes/search
截圖如下所示,其中紅色方法內的就是我們要獲取到的信息:
這裏寫圖片描述

初始化

  1. 首先需要創建一個目錄用於存放Vagrantfile文件以及Vagrant在工作中的數據。

    mkdir vagrant-workspace/
    cd vagrant-workspace
    mkdir ubuntu1404
    cd ubuntu1404
  2. 開始初始化Vagrant工程

    vagrant init ubuntu/trusty64

    這裏的ubuntu/trusty64就是在官網的提供的頁面獲取的Box的名稱。Vagrant發現box的名字的格式爲“用戶名/box名”,則會使用“https://atlas.hashicorp.com/用戶名/box名”來下載該box。對於非官網提供的box,可以通過以下命令創建:

    vagrant init {Box的名稱} {Box的url}

    回車之後,命令行窗口輸出信息,它告訴我們在文件夾下創建一個文件叫Vagrantfile,可以準備進行運行vagrant up命令了。

    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.
    t.

    Vagrantfile文件裏面的註釋忽略的話,可以看到有效的內容如下:

    Vagrant.configure("2") do |config|
      config.vm.box = "ubuntu/trusty64"
    end
    d
  3. 啓動虛擬機,如果是第一次運行,則會先去下載box文件,如果先前下載過了則跳過

    vagrant up

    經過漫長的等待之後,終於下載完了,輸出信息

    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Box 'ubuntu/trusty64' could not be found. Attempting to find and install...
        default: Box Provider: virtualbox
        default: Box Version: >= 0
    ==> default: Loading metadata for box 'ubuntu/trusty64'
        default: URL: https://vagrantcloud.com/ubuntu/trusty64
    ==> default: Adding box 'ubuntu/trusty64' (v20180419.0.0) for provider: virtualbox
        default: Downloading: https://vagrantcloud.com/ubuntu/boxes/trusty64/versions/20180419.0.0/providers/virtualbox.box
        default: Download redirected to host: cloud-images.ubuntu.com
        default: 
    ==> default: Successfully added box 'ubuntu/trusty64' (v20180419.0.0) for 'virtualbox'!
    ==> default: Importing base box 'ubuntu/trusty64'...
    ==> default: Matching MAC address for NAT networking...
    ==> default: Checking if box 'ubuntu/trusty64' is up to date...
    ==> default: Setting the name of the VM: ubuntu1404_default_1525060576422_98609
    ==> default: Clearing any previously set forwarded ports...
    Vagrant is currently configured to create VirtualBox synced folders with
    the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
    guest is not trusted, you may want to disable this option. For more
    information on this option, please refer to the VirtualBox manual:
    
      https://www.virtualbox.org/manual/ch04.html#sharedfolders
    
    This option can be disabled globally with an environment variable:
    
      VAGRANT_DISABLE_VBOXSYMLINKCREATE=1
    
    or on a per folder basis within the Vagrantfile:
    
      config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
    ==> default: Clearing any previously set network interfaces...
    ==> default: Preparing network interfaces based on configuration...
        default: Adapter 1: nat
    ==> default: Forwarding ports...
        default: 22 (guest) => 2222 (host) (adapter 1)
    ==> default: Booting VM...
    ==> default: Waiting for machine to boot. This may take a few minutes...
        default: SSH address: 127.0.0.1:2222
        default: SSH username: vagrant
        default: SSH auth method: private key
        default: Warning: Connection aborted. Retrying...
        default: Warning: Remote connection disconnect. Retrying...
        default: Warning: Connection aborted. Retrying...
        default: Warning: Connection reset. Retrying...
        default: Warning: Connection aborted. Retrying...
        default: 
        default: Vagrant insecure key detected. Vagrant will automatically replace
        default: this with a newly generated keypair for better security.
        default: 
        default: Inserting generated public key within guest...
        default: Removing insecure key from the guest if it's present...
        default: Key inserted! Disconnecting and reconnecting using new SSH key...
    ==> default: Machine booted and ready!
    ==> default: Checking for guest additions in VM...
        default: The guest additions on this VM do not match the installed version of
        default: VirtualBox! In most cases this is fine, but in rare cases it can
        default: prevent things such as shared folders from working properly. If you see
        default: shared folder errors, please make sure the guest additions within the
        default: virtual machine match the version of VirtualBox you have installed on
        default: your host and reload your VM.
        default: 
        default: Guest Additions Version: 4.3.36
        default: VirtualBox Version: 5.2
    ==> default: Mounting shared folders...
        default: /vagrant => G:/vagrant-workspace/ubuntu1404
  4. 登錄虛擬機

    vagrant ssh

關閉虛擬機

使用命令vagrant halt關閉虛擬機

刪除虛擬機

使用命令vagrant destroy刪除虛擬機。請注意,vagrant destroy只會刪除虛擬機本身,也即你在Virtualbox將看不到該虛擬機,但是不會刪除該虛擬機所使用的box。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章