DEV Community

Zaki Arrozi Arsyad
Zaki Arrozi Arsyad

Posted on • Updated on

linux : vagrantfile configuration

Let’s create a basic configuration of our Vagrantfile, instead of only use vagrant init.

Default Vagrantfile if we run vagrant init <boxpath>

Set minimum vagrant version

Vagrant.require_version ">= 1.3.5", "< 1.4.0"
Enter fullscreen mode Exit fullscreen mode

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 1.4.0.

Configures provisioners

config.vm.provision "shell", inline: "apt-get update && apt-get install -y  docker.io "
Enter fullscreen mode Exit fullscreen mode

or

config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y  docker.io
SHELL
Enter fullscreen mode Exit fullscreen mode

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.

Define a Virtual Machine

config.vm.define "ubuntu1804" do |ubuntu1804|
    # ...
end
Enter fullscreen mode Exit fullscreen mode

We can define a name for our VM, instead of use default if we don’t define it.

Configure the machine

config.vm.box = "ubuntu/bionic64"
Enter fullscreen mode Exit fullscreen mode

We can specify our machine we want to run. We can find available linux machine here.

Set autoupdate for every vagrant up

config.vm.box_check_update = false
Enter fullscreen mode Exit fullscreen mode

The default value is true, so every time we run the VM, it will check for updates.

Create a forwarded port

# 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"
Enter fullscreen mode Exit fullscreen mode

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 127.0.0.1.

Create a private network

config.vm.network "private_network", ip: "192.168.33.10"
Enter fullscreen mode Exit fullscreen mode

We can also specify the private IP for the machine that allow us to access the VM using a static IP.

Create a bridge network

config.vm.network :public_network, :bridge => "en0: Wi-Fi (Wireless)"
Enter fullscreen mode Exit fullscreen mode

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.

Crate share a folder between VM and host

config.vm.synced_folder "HOST_FOLDER_PATH", "VM_FOLDER_PATH"
Enter fullscreen mode Exit fullscreen mode

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.

Set provider configuration

config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"
    vb.cpus = "1"
end
Enter fullscreen mode Exit fullscreen mode

If we need more memory and cpu, we can configure the memory and cpus we want to use in the VM.

Configure disk size

config.disksize.size = '25GB'
Enter fullscreen mode Exit fullscreen mode

We also can configure disk size that we want to use for the machine.

Top comments (0)