Let’s create a basic configuration of our Vagrantfile, instead of only use
Vagrant.require_version ">= 1.3.5", "< 1.4.0"
This will help toavoid compatibility issues that may arise from using different version of vagrant.
We can specify the version that the Vagrantfile will only run if we use greater than equal version
1.3.5 and below
config.vm.provision "shell", inline: "apt-get update && apt-get install -y docker.io "
config.vm.provision "shell", inline: <<-SHELL apt-get update apt-get install -y docker.io SHELL
We can automatically run a code to install and configure something when the virtual machine is created.
In this case, we update the package list and install docker.
config.vm.define "ubuntu1804" do |ubuntu1804| # ... end
We can define a name for our VM, instead of use
default if we don’t define it.
config.vm.box = "ubuntu/bionic64"
We can specify our machine we want to run. We can find available linux machine here.
config.vm.box_check_update = false
The default value is
true, so every time we run the VM, it will check for updates.
# enable public access config.vm.network "forwarded_port", guest: 80, host: 8080 # disable public access config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
We can configure the networks in the machine. If we use
config.vm.network we have to specify two ports, first one is for
guessand the second one is
host port that the guest port can be accessed by.
By default, it will automatically enable the public access. If we want to disable public access, we need to specify the
host_ip that only allow access via
config.vm.network "private_network", ip: "192.168.33.10"
We can also specify the private IP for the machine that allow us to access the VM using a static IP.
config.vm.network :public_network, :bridge => "en0: Wi-Fi (Wireless)"
The default network connection for vagrant is
NAT. We can update into
bridge which is allow the machine to connect to other vm and the host, also we can use
host only which only allow communication between the machine and the host.
config.vm.synced_folder "HOST_FOLDER_PATH", "VM_FOLDER_PATH"
We can create a sync folder that allow us to copy pasting a file between the machine and the host. We need to specify the
host folder path and
vm folder path , then we can easily transfer file between that machines.
config.vm.provider "virtualbox" do |vb| vb.memory = "2048" vb.cpus = "1" end
If we need more memory and cpu, we can configure the
cpus we want to use in the VM.
config.disksize.size = '25GB'
We also can configure disk size that we want to use for the machine.