沙滩星空的博客沙滩星空的博客

vagrantfile解析

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "catmes-centos-7"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # 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
   config.vm.network "forwarded_port", guest: 3306, host: 33060
   
   
   
  # 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. 如果宿主机的主机IP为192.168.1.xxx字段,那就不要再 这个区段,可以使用2.xxx
  # 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"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
   config.vm.synced_folder "D:/Users/admin/www", "/mnt/www", create:true, owner:"www",group:"docker"
   config.vm.synced_folder "D:/vagrant/vhost", "/mnt/vhost"
   config.vm.synced_folder "D:/vagrant/docker_compose", "/mnt/docker_compose"
  # the path can not use "\" must use "/" 

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
   config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  # vagrant up启动时,是否自动打开virtual box的窗口,缺省为false
  #   # Customize the amount of memory on the VM:
     vb.memory = "2048"
   end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", path: "start-docker.sh"
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end


Vagrantfile

config.vm.box = "centos/8"
config.vm.box_url = "http://cloud.centos.org/centos/8/x86_64/images/CentOS-8-Vagrant-8.1.1911-20200113.3.x86_64.vagrant-virtualbox.box"

网络配置
vagrant支持以下三种网络配置:

Forwarded port(端口映射)
是指将宿主计算机的端口映射到虚拟机上的某个端口上,访问宿主计算机的该端口时,请求实际会被转发到虚拟机上指定的端口,配置文件设置语法为:

config.vm.network :forwarded_port, guest: 80, host: 8889

优点:简单、容易理解、容易实现外网访问虚拟机。
缺点:需映射很多端口时较麻烦、不支持在宿主机器上使用小于1024的端口来转发(如:不能使用SSL的443端口来进行https连接)。

Private network(私有网络)
这种网络配置下,只有主机可以访问虚拟机,如果多个虚拟机设置定在同一个网段也可以相互访问,当然虚拟机也是可以访问外部网络的。配置语法如下:

config.vm.network "private_network", ip: "192.168.50.4" # 固定IP

还可以设置动态IP,配置语法如下:

config.vm.network "private_network", type: "dhcp"

优点:安全,只能自己访问。
缺点:因私有原有,所以其他团队成员不能和你协作。

Public network(公有网络)
这种配置下,虚拟机享受实体机一样的待遇,一样的网络配置,vagrant 1.3版本以后这种配置也支持设定固定IP,配置语法如下:

config.vm.network "public_network", ip: "192.168.50.4"

还可以设置桥接网卡,配置语法如下:

config.vm.network "public_network", bridge: "en1: Wi-Fi (AirPort)"

优点:方便团队协作,别人可以访问你的虚拟机。
缺点:需要有网络,有路由器分配IP


征服诱人的Vagrant https://www.cnblogs.com/hafiz/p/9175484.html
配置vagrant使用三种网络 https://my.oschina.net/u/3683692/blog/3025611
Vagrantfile 配置技巧 https://www.hi-linux.com/posts/24777.html
未经允许不得转载:沙滩星空的博客 » vagrantfile解析

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址