Open rainit2006 opened 4 years ago
vagrant 不能单独使用,如果你用它来管理自己的开发环境的话,必须在自己的电脑里安装了虚拟机软件,如 virtualbox。
更进一步,Vagrant 提供一个命令行工具 vagrant,通过这个命令行工具可以直接启动一个虚拟机,当然你需要提前定义一个 Vagrantfile 文件,这有点类似Dockerfile 之于 docker了。 可以通过编写一个 Vagrantfile 来控制虚拟机的启动、虚拟机网络环境的配置、虚拟机与主机间的文件共享,以及启动后自动执行一些配置脚本,比如自动执行一个 shell script 来安装一些必备的开发工具,如Mysql。 这意味着,当你需要在多台机器间同步开发进度时,只需要同步Vagrantfile,就可以保证各台机器拥有一致的开发环境。
https://www.jianshu.com/p/31b2a6ce0924
Vagrant 是通过基础镜像包来实现快速克隆创建虚拟机的。这些基础镜像包在 Vagrant 中被称为 boxes。 https://app.vagrantup.com/boxes/search
$ mkdir vagrant_getting_started
$ cd vagrant_getting_started
$ vagrant init hashicorp/bionic64
$ vagrant box add hashicorp/bionic64
Open the Vagrantfile and change the contents to the following:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/bionic64"
end
$ vagrant up
$ vagrant ssh
The SSH session can be terminated with CTRL+D.
By default, Vagrant shares your project directory (remember, that is the one with the Vagrantfile) to the /vagrant directory in your guest machine.
Vagrant has built-in support for automated provisioning. Using this feature, Vagrant will automatically install software when you vagrant up so that the guest machine can be repeatably created and ready-to-use. Ex.
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/bionic64"
config.vm.provision :shell, path: "bootstrap.sh" // bootstrap.sh里是执行apache安装的命令
end
After everything is configured, just run vagrant up
to create your machine and Vagrant will automatically provision it. You should see the output from the shell script appear in your terminal.
If the guest machine is already running from a previous step, run vagrant reload --provision
, which will quickly restart your virtual machine, skipping the initial import step.
其他参考:
https://blog.csdn.net/54powerman/article/details/50684844
从字面上来看,provision是准备,实现的功能是在原生镜像的基础上,进行一些附加的操作,以改变虚拟机的环境,比如安装应用,发布程序等。
provision任务是预先设置的一些操作指令。格式:
config.vm.provision 命令字 json格式参数
或
config.vm.provion 命令字 do |s|
s.参数名 = 参数值
end
每一个 config.vm.provision 命令字 代码段,我们称之为一个provisioner。 根据vagrantfile的层次,分为: configure级:它定义在 Vagrant.configure("2") 的下一层次,形如: config.vm.provision ... vm级:它定义在 config.vm.define "web" do |web| 的下一层次,web.vm.provision ... 执行的顺序是先执行configure级任务,再执行vm级任务,即便configure级任务在vm定义的下面才定义。
原文链接:https://blog.csdn.net/54powerman/article/details/50684844
Ex. Port Forwarding Port forwarding allows you to specify ports on the guest machine to share via a port on the host machine.
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/bionic64"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 4567
end
参考:https://www.jianshu.com/p/a1bc23bc7892 Vagrant可以指定三种网络:端口转发(Forwarded Ports),私有网络(Private Network),公有网络(Public Network)。
config.vm.network :forwarded_port, guest: 80, host: 8080
将虚拟机(被称作guest)的端口80映射为宿主机的端口8080。
端口转发隐含着一个provider的NAT(Network Address Translation,网络地址转换)网络将被首先创建。所以,如果你单独定义一条端口转发的配置语句的话,VM将会自动建立NAT网络环境。config.vm.network :private_network, ip: "192.168.1.104"
你可以从宿主机自由访问虚拟机,但LAN网络中的其他人不需要也无法访问虚拟机。
值得注意的是,ip地址“192.168.1.104”不是随便指定的。
首先你可以不指定,这表示虚机启动时会DHCP到一个可用的IP地址(例如:192.168.33.101),这是vagrant通过virtualbox私有网络的DHCP机制获得的。
如果你要自行指定明确的IP地址,要保证该地址是在恰当的网段中,例如192.168.33.71。
本质上说,这是使用provider的HostOnly模式。config.vm.network :public_network
本质上说,这是使用provider的桥接网络模式。You need the vagrant-share plugin for the rest of the tutorial to work.
Next, run vagrant share
$ vagrant suspend $ vagrant halt $ vagrant destroy
$ vagrant up
https://qiita.com/ryurock/items/91df14537512c03488ac
vmの起動後に実行してくれる設定を持たすことができます。
:shell 前処理をshellで実行出来ます。
config.vm.provision :shell, :path => "{任意の名前}.sh"
この例ですとvmの起動後カレントの{任意の名前}.shを実行するという処理になります。(起動時毎回)
また:inlineというものもあって.shでなく直接Vagrantfileに書いて実行できるようです。
Vagrant.configure("2") do |config|
# ... other configuration
config.vm.provision :shell, :inline => "echo hello"
end
共有フォルダの設定です。 デフォルトは
config.vm.synced_folder "ホスト側のディレクトリ", "ゲスト側のディレクトリ"
vagrant plugin install {gemの名称} gem的网页: https://rubygems.org/search?utf8=%E2%9C%93&query=vagrant
Vagrantfile 是一种非常灵活的配置格式。语法基于 Ruby。
(1..3).each do |i|
config.vm.define "node-#{i}" do |node|
node.vm.provision "shell",
inline: "echo hello from node #{i}"
end
end
Ruby 块就是放在 do 和 end 之间的代码,两个竖线中间的 变量 是块的参数。 config.vm.define "node-#{i}" do |node| 相当于定义 参数node 的值等于 node-#{i}。这里i的数值是来自上一句循环语句里的参数。
Tutorial
https://www.vagrantup.com/intro/getting-started/index.html