Vagrant provides Boxes with various operating system such as Ubuntu, Debian, or CentOS. Using the vagrant utility, we can provision development environments in minutes. In our development environment, we come across situations where we have to modify the vagrant boxes with various customizations. In this blog, we would go through steps to build custom vagrant box to use for environment provisioning.
Prerequisites
Vagrant binary should be installed in the local environment.
Access to vagrant public box catalog to fetch base os box to build vagrant environment.
Need to have one or more providers configured in the local environment. We have various options to choose as a provider – VirtualBox, Hyper-V, Docker and VMware. I would be using “virtualbox” as the provider.
Build local vagrant box
We would build a local vagrant environment and then package it as a custom box. The vagrant binary provides various options to manage vagrant box operations.
Add the vagrant box of choice in the local environment. I have chosen the ubuntu/focal64 vagrant box. We can notice that this box is pulled for “virtualbox” as provider by default since this vagrant box is available with “virtualbox” as provider only.
Create a folder on local environment and initialize the vagrant environment. This step would primarily produce the default Vagrantfile, which can be configured with further resource, network and os customizations.
We would modify the Vagrantfile to put some of the basic customizations.
Private Network
Server – cpu and memory
Update the server during the launch.
Launch the server virtual machine with vagrant command option.
SSH inside the server, add and enable the docker.service and activate docker for the default user “vagrant” in the server machine.
With this basic set of customization, we can build our own vagrant box now. We have created another folder “myboxes” to store all the custom-boxes. From the local environment, we can run vagrant package command to initiate the box build process.
As a result, the customized vagrant box would be created and placed at /user1/vagrantzone/myboxes.
Versioning custom vagrant box
At this stage, it can be added at vagrant local repo with vagrant box add command. But, we would observe that the version would show as “v0”. For various business reasons, we would need to have the box versioned in a better way.
Based on the local setup, we can have seperate folders for each customized server and use the folder to fetch the box for environment build purpose. To add various metadata information (incl. box version), we can create a json based config in folder forcustomized server i.e :/user1/vagrantzone/myboxes.
In above vagrant config file, we have generated the sha256 key using the shasum tool
Now, we can add the customized vagrant box to local vagrant repo.
The customized vagrant box is ready to be used for environment provisioning purpose.
Push vagrant box to vagrant cloud
Above process shows steps to customize the box and use in local environment.
If we want to push to a global repository and share the customized vagrant image, we need to have access and account at Vagrant Cloud. All the users get access to upload and share their vagrant boxes. Customized vagrant boxes can be pushed to vagrant Cloud and it can be shared in same way as vagrant gold boxes. For the free account, we can create boxes in “public” mode, and for private access need to purchase the subscription.