前几天看了Vagrant的介绍,今天正好有空,打算安装下试试。由于自己用的是Windows的开发环境,所以就在window下试着搭建了下。遇到一些问题,记录下。
关于Vagrant,及为什么用Vagrant。大家可以去看这篇文章《为什么要使用Vagrant》,个人感觉说的比较详细。
接下来,说下我的整个安装过程。
一、准备工作Vagrant 除了virtualbox,还支持HyperV、VMWare等虚拟软件。virtualbox与vagrant是最常用的搭配,也是文档最多的,所以我也选了这对组合。
box 的镜像除了使用下载好的本地文件还可以使用在线的box镜像,添加镜像时,还是会下载。
二、安装及应用2.1 安装virtualbox和vagrant
同其他windows软件一样,“下一步”即可。
2.2 应用
1、给vagrant 添加镜像。
D:\vagrant\>vagrant box add base CentOS-6.6-x86_64.box说明:
vagrant box vagrant命令
base 添加box的名称
centos-6.6-x86_64.box- 本地box的文件名
使用vagrant box list 查看添加的box镜像列表。
也可以添加在线box,和添加本地box一样。通过命令vagrant box add Ubuntu/trusty64。ubuntu/trusty64为在线镜像名称。
在线镜像地址 vagrantCloud。(国内的网络访问可能不太通,建议下载后添加 )
这是我使用centos的镜像 centos-6.6-x86_64.box
2、初始化虚拟机。
D:\\vagrant\\>mkdir worker # 创建工作环境 D:\\vagrant\\>cd worker D:\\vagarnt\\worker\\>vagrant init [boxname] # 当添加的box的别名不为 base 时,此处需要添加 boxname3、启动虚拟机
D:\\vagrant\\worker\\>vagrant up看到如下信息,说明虚拟机启动成功:
D:\vagrant\worker>vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'base'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: worker_default_1448939529781_46842 ==> 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: Adapter 2: hostonly ==> default: Forwarding ports... default: 22 => 2222 (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 timeout. Retrying... default: Warning: Connection timeout. Retrying... default: Warning: Remote connection disconnect. 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.28 default: VirtualBox Version: 5.0 ==> default: Configuring and enabling network interfaces... ==> default: Mounting shared folders... default: /vagrant => D:/vagrant/worker