vagrant入门之VagrantFile

前言

VagrantFile可以理解为使用ruby语法编写的配置文件里面配置同样也是多而繁琐的,我只写点我们常用的,更深入的需要各位翻阅官方文档

参考文档

  • https://www.vagrantup.com/docs/vagrantfile

正题

Vagrant.configure("2") do |config|
  # 设置主机名称
  config.vm.hostname="docker"
  # 设置使用那个box,可以在https://vagrantcloud.com/search查找
  config.vm.box = "centos-docker"
  # 设置使用的版本
  config.vm.box_version="1.0"
  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  config.vm.network "public_network"
  # 文件同步
  # config.vm.synced_folder "../data", "/vagrant_data"
    
  # virtualbox 配置
  config.vm.provider "virtualbox" do |vb|
    # 内存设置
    vb.memory = "2048"
    # cpu
    vb.cpus=2
    # virtualbox显示的名称 
    vb.name="centos-docker"
  end
  # 执行的shell脚本 
  config.vm.provision "shell", inline: <<-SHELL
    echo hello vagrant
  SHELL
end

说明

VagrantFile 大致为分为

  • config.vm https://www.vagrantup.com/docs/vagrantfile/machine_settings.html
  • config.ssh https://www.vagrantup.com/docs/vagrantfile/ssh_settings.html
  • config.winrm 针对windows用户的 https://www.vagrantup.com/docs/vagrantfile/winrm_settings.html
  • config.winssh https://www.vagrantup.com/docs/vagrantfile/winssh_settings.html
  • config.vagrant https://www.vagrantup.com/docs/vagrantfile/vagrant_settings.html

vagrant三种网络模式

这个也是比较可以复杂和简单的模块,我这里用的是公共网络

  • public_network 公共网络,同一局域网其他计算机在同一网段就可以访问
  • forwarded_port端口转发
  • private_network 内部网络

最后

如果你想了解更多的文章可以微信搜索zhaoyx92,或者扫码关注

zhaoyx92

你可能感兴趣的:(vagrant入门之VagrantFile)