Vagrant与Docker有不少类似之处,又有很多不一样。一言蔽之,Vagrant用来管理虚拟机,Docker用来隔离应用环境。 https://www.vagrantup.com/downloads.html
vagrant -v Vagrant 2.1.1
Centos上安装php
wget https://releases.hashicorp.com/vagrant/2.1.2/vagrant_2.1.2_x86_64.rpm yum install -y vagrant_2.1.2_x86_64.rpm
vagrant box add 只会下载镜像,并不会马上建立一个虚拟机html
vagrant box list/add/remove
CLI列表mysql
https://www.vagrantup.com/doc...nginx
#会生成一个Vagrantfile文件 vagrant init centos/7 #基于Vagrantfile配置文件,建立启动centos7 vagrant up #进入启动完成的centos7系统 vagrant ssh
若是须要使用vagrant建立其余系统,能够访问 https://app.vagrantup.com/box...,查看全部的系统列表
若是追求稳定版,能够优先使用 https://app.vagrantup.com/bentosql
vagrant status Current machine states: default running (virtualbox) The VM is running. To stop this VM, you can run `vagrant halt` to shut it down forcefully, or you can run `vagrant suspend` to simply suspend the virtual machine. In either case, to restart it again, simply run `vagrant up`.
vagrant halt
vagrant reload
默认状况下会更新Vagrantfile文件中除了config.vm.provision
配置之外的配置,而后重启系统。
使用--provision
选项,会强制更新config.vm.provision
配置docker
vagrant reload
默认状况下,咱们是使用用户名为vagrant、密码为vagrant的帐号登录的。可是在执行命令前不加sudo
就经常会致使权限问题。所以能够经过以下方式,将默认帐号改成root:shell
密码方式登录centos
先使用vagrant帐号登录进系统,而后su -
切换成root用户,root用户的密码默认为vagrant
ruby
修改/etc/ssh/sshd_config
文件,修改成或添加以下配置,而后 systemctl reload sshd
重启sshd服务app
若是是密码方式登录:
PermitRootLogin yes PasswordAuthentication yes
若是是证书方式登录:
PermitRootLogin yes PubkeyAuthentication yes
最后编辑Vagrantfile文件,添加以下配置,vagrant reload
后从新vagrant ssh
便可
若是是密码方式登录:
config.ssh.username = 'root' config.ssh.password = 'vagrant'
若是是证书方式登录:
config.ssh.username= "root" config.ssh.private_key_path="~/.ssh/id_rsa"
private_key_path若是不显示声明的话,vagrant就会报错:
Authentication failure. Retrying...
PHPStorm目前并不支持Vagrant文件的语法高亮。使用IntelliJ打开项目,而后安装一个ruby插件,就能够实现语法高亮了
vagrant默认的root密码为vagrant,为了便于记忆,咱们将虚拟机中的各个软件的密码都设置为vagrant
按照以下配置,咱们便可在宿主机上经过8080端口,去访问虚拟机中80端口的内容
config.vm.network "forwarded_port", guest: 80, host: 8080
打包前须要vagrant halt
虚拟机,注意打包文件的输出目录必定不要放在vagrant数据同步的目录内,缘由你懂的。。。
vagrant package --output xxx.box vagrant package --output xxx.box --base 虚拟机名称
若是咱们给别人了1.0版本的box,而后又分发了一个2.0的box,老用户想升级的话,此时能够由发行者在Vagrantfile文件的shell部分编写升级脚本.
若是咱们经过sudo vagrant up
建立了一个vm,可是使用vagrant status
来读取vmzhuang tai
The VirtualBox VM was created with a user that doesn't match the current user running Vagrant. VirtualBox requires that the same user be used to manage the VM that was created. Please re-run Vagrant with that user. This is not a Vagrant issue. The UID used to create the VM was: 502 Your UID is: 501
别人拿到打包的.box文件,使用以下命令便可使用了
vagrant init siguoya.box vagrant up
Vagrant.configure("2") do |config| # https://docs.vagrantup.com. config.vm.box = "centos/7" config.vm.hostname = "centos" config.vm.box_check_update = false config.disksize.size = "60GB" # config.vm.network "forwarded_port", guest: 80, host: 8080 # 配置private_network的好处: # - 不须要配端口转发,突破端口转发宿主机端口设置不能小于1024的问题 # - 可使用nfs进行文件同步,避免出现文件类型不一致的问题 # 记得不要和宿主机所在的网段冲突,例如个人宿主机为192.168.88.66,而后将ip设成192.168.88.168的时候就致使虚拟机没法启动了 # 设置auto_config为true,是为了打包分发别人的时候,避免私有ip致使的问题,保障可以正常使用 config.vm.network "private_network", ip: "192.168.33.10", auto_config: true #因为VM的bug,须要将nginx的sendfile设置为false,否则可能会致使代码不能当即生效 #10.15 之前 config.vm.synced_folder "/usr/project/code", "/usr/project/code", :nfs => true config.vm.synced_folder "/System/Volumes/Data/usr/local/coding/code", "/usr/local/coding/code", type: "nfs" , nfs_version: 3, nfs_udp: false #因为虚拟机内外文件系统不一致,vagrant默认的文件夹同步会报错,所以经过以下方式进行禁止 config.vm.synced_folder ".", "/vagrant", :disabled => true config.vm.provider "virtualbox" do |vb| vb.name = "vagrant_siguoya" vb.memory = "2048" vb.cpus = "2" end end
配置了nfs同步以后,vagrant up
会要求输入密码,若是不想输入密码,能够 sudo vi /etc/sudoers
添加以下内容:
# VAGRANT Cmnd_Alias VAGRANT_EXPORTS_ADD = /usr/bin/tee -a /etc/exports Cmnd_Alias VAGRANT_NFSD = /sbin/nfsd restart Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /usr/bin/sed -E -e /*/ d -ibak /etc/exports %admin ALL=(root) NOPASSWD: VAGRANT_EXPORTS_ADD, VAGRANT_NFSD, VAGRANT_EXPORTS_REMOVE
若是/etc/exports
的文件内容以下:
# VAGRANT-BEGIN: 501 79c27c9a-9f3f-4ac7-b2b1-fb453217fb92 "/usr/project/code" 192.168.33.10 -alldirs -mapall=501:20 # VAGRANT-END: 501 79c27c9a-9f3f-4ac7-b2b1-fb453217fb92 # VAGRANT-BEGIN: 501 0b26c233-7623-4acd-931a-670b8ecbfcd4 "/usr/project/code/zy108830/docker-demo/vagrant-network/labs" 192.168.205.10 -alldirs -mapall=501:20 # VAGRANT-END: 501 0b26c233-7623-4acd-931a-670b8ecbfcd4
就会报错:
NFS is reporting that your exports file is invalid. Vagrant does this check before making any changes to the file. Please correct the issues below and execute "vagrant reload": exports:5: /usr/project/code/zy108830/docker-demo/vagrant-network/labs conflicts with existing export /usr/project/code
If you had some previous images/boxes installed in your VirtualBox installation, so there were a few invalid entries in /etc/exports
already. So you had to clean up that file and restart your Vagrant box.
sudo rm /etc/exports && sudo touch /etc/exports vagrant halt && vagrant up --provision