We have crossed halfway through this series and by this time you might have a good understanding of what Vagrant does and how to use provisioners in vagrant.
Till now you are using prebuilt vagrant boxes downloaded from the vagrant cloud site. Some boxes are preconfigured to serve different purposes like the scotch box which comes with a LAMP stack, trusty64cdh which comes with a single node CDH Hadoop distribution. You can also create your box like the one mentioned and share it with the community or fellow geeks working with you on the same project.
Here there are two ways you can create a vagrant box. You can choose any hypervisor and manually install guest os and harden the os then package it to box format. Alternatively, you can download boxes already available in the vagrant cloud, customize them according to your requirement, and repack it to box format. This is quite easy compared to building the box from scratch.
Creating a New Vagrant Box
1. Choose the box which will be used. In my case, I am downloading the ubuntu/focal64 box. Create a new directory and run the vagrant init command.
$ mkdir ubuntu $ cd rebuild $ vagrant init -m "ubuntu/focal64"
2. Bring up the virtual machine by running the vagrant init command.
$ vagrant init
3. Connect to the guest virtual machine using:
$ vagrant ssh
Enable password-based authentication if needed, by default it is disabled.
$ sudo sed -i "/^[^#]*PasswordAuthentication[[:space:]]no/c\PasswordAuthentication yes" /etc/ssh/sshd_config $ sudo service sshd restart
4. I want to install PostgreSQL on this virtual machine and rebox it.
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' $ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - $ sudo apt-get update $ sudo apt-get -y install postgresql
Switch as a Postgres user and type psql
to connect to the shell. Now if everything goes well installation is successful.
$ sudo su - postgres $ psql
5. Before repacking the box we have to zero out the underlying drive to achieve better compression.
$ sudo dd if=/dev/zero of=/EMPTY bs=1M $ sudo rm -f /EMPTY
Create a New Vagrant Box using Existing Vagrant Box
6. Create a new box from the existing virtual machine by running the following command.
$ vagrant package --output ubuntu_repacked.box
7. Take a look at the above image where you can see a new box is created “ubuntu_repacked.box”. Now, this box needs to be added to the vagrant to import.
$ vagrant box add repacked ubuntu_repacked.box
8. Now I can use this box and spin up a new guest virtual machine. Create a new project directory and do a vagrant init.
$ mkdir repacked $ cd repacked $ vagrant init -m "repacked" $ vagrant up
From the above image, you can see it is importing the repacked box we created. Connect to the virtual machine and run the psql
command to work with PostgreSQL.
$ vagrant ssh $ sudo su - postgres $ psql
We have come to the end of this article. I have shown you how to create a box from the existing vagrant box. In this example, I have shown you how to install PostgreSQL in the vagrant box. The real-time use case can be anything and this process will be very handy.