軟件安裝
安裝vagrant,安裝virtualbox
安裝步驟省略
使用方法
- 新建文件夾vagrant, 進入該文件夾
mkdir vagrant
- 下載.box系統的鏡像文件
獲取鏡像文件地址:[http://www.vagrantbox.es/]
兩種方式:
1.從url直接添加
vagrant box add {name} {鏡像url}
2.下載後添加
vagrant box add {name} {下載好的本地box路徑}
例:我新建了box文件夾,用下載的方式將.box文件放到了box文件夾下.
下載速度慢可以轉到清華的源上,本次用到的.box下載路徑:
https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box
E:\vagrant\box\{name}.box
並執行添加命令:
vagrant box add {myname} {E:\vagrant\box\{name}.box}
- 列出添加後的box列表
vagrant box list
- 初始化box
vagrant init {myname}
- 設置共享目錄
打開剛生成的Vagrantfile
文件,放開註釋,並修改共享目錄位置
例:
# config.vm.synced_folder "../data", "/vagrant_data"
修改爲:
config.vm.synced_folder "E:\\vagrant\\sap\\data", "/vagrant"
- 啓動box
vagrant up --provider=virtualbox
啓動box如果報以下錯誤
E:\vagrant\box>vagrant up --provider=virtualbox
The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:
Vagrant has detected that you have a version of VirtualBox installed
that is not supported by this version of Vagrant. Please install one of
the supported versions listed below to use Vagrant:
4.0, 4.1, 4.2, 4.3, 5.0, 5.1
A Vagrant update may also be available that adds support for the version
you specified. Please check www.vagrantup.com/downloads.html to download
the latest version.
是因爲vagrant和virtualbox版本不匹配導致。
解決辦法參考次大神博客,親測有效:
https://www.cnblogs.com/chen-785/p/12881625.html
解決完成後能夠正常啓動:
E:\vagrant\sap>vagrant up --provider=virtualbox
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> 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: Running 'pre-boot' VM customizations...
==> 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: 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: 5.2.34
default: VirtualBox Version: 6.0
==> default: Mounting shared folders...
default: /vagrant => E:/vagrant/sap/data
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
其中出現這兩句表示共享目錄設置成功並啓動
==> default: Mounting shared folders...
default: /vagrant => E:/vagrant/sap/data
進入/vagrant目錄怎能看到共享文件。
打開virtualbox可以看到裏已經添加成功正在運行。
啓動連接窗口後輸入默認用戶名密碼:
用戶名 : vagrant , 登錄密碼 : vagrant