DEV Community

Zaki Arrozi Arsyad
Zaki Arrozi Arsyad

Posted on • Edited on

3

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>

Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
end
view raw Vagrantfile hosted with ❤ by GitHub

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.

Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: "apt-get update && apt-get install -y docker.io "
config.vm.define "ubuntu1804" do |ubuntu1804|
config.vm.box = "ubuntu/bionic64"
config.vm.box_check_update = false
config.vm.network :public_network, :bridge => "en0: Wi-Fi (Wireless)"
config.vm.synced_folder "/Users/zaki/Documents/sandbox/ubuntu1804", "/home/vagrant/host"
master.vm.provider "virtualbox" do |v|
v.memory = 2024
v.cpus = 2
end
end
end

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay