Building Custom Images using Shell Provisioner and Packer

Suman Srivastava
3 min readMay 9, 2021

One of the tedious task for a Cloud Engineer is to Manage and Build Dependencies for application deployment.These can be solved by creating a custom images for your projects. Building custom images can help you in many ways, such as setting up some paths, tools, CLI, builders and imposing securities. In this demo we are going to make use of Hasicorp’s Packer and Google’s Cloud platform to build a custom image to setup Docker CE.

Table of Contents:

  1. Setting up your Environment.
  2. Scoping your Service Account.
  3. Preparing your Shell Script to setup Docker.
  4. Writing your packer file.
  5. Conclusion.

1. Setting up your Environment

I am going to make use of ubuntu 20.4 in this example and making most of the configuration based on the Linux Distribution. Windows user also follow along.

Installing Packer

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install packer -y

Windows Download

https://www.packer.io/downloads

2. Scoping service account

For this demo we are going to create and instance (packer will do this to execute your provisioners), ssh into it and then run command as root. We need our packer service account to be Compute Instance Admin and Service Account User.

Lets login to the console and navigate to service account.

Now create Service Account Scope then, download and save the json keys.

3. Preparing your Shell Script to setup Docker

I have created a Sample script to setup and install Docker CE.

Now save this file in a directory.

4. Writing your Packer File

This file has 2 blocks Builders and Provisioners. Builders require few parameters like the type, account_file, source_image_family, name of the packer user “ssh_user”, source_image_name etc.

Provisioners require the execution block and type, there are some other parameters which can be used here in provisioner block such as, Inline, Scripts (list of all scripts), Remote_File ad So on..

Now, we are Ready to launch our script let begin !!

packer validate <path_of_packer.json>

This will validate the file and will give you an error if you have not set the script correctly or key.json file path is not correct.

Lets Run the execution !!

Packer build <path_of_packer.json>

It will take sometime to provision the compute instance. Later it will ssh into VM to run Shell Script to setup Docker.

Success Message after execution

Once the Script execution is complete, Packer is going to tear down the VM and os disk. So that you won’t be charged for the Instance.

5. Conclusion

In this demo we have learned how to use packer to build golden image for our dependencies and tool setup. Which will help other engineers to call those images and save efforts.

Thanks for Reading !

--

--

Suman Srivastava

A DevOps professional experienced in setting up secure, highly scalable, available and scalable infrastructure on cloud