Skip to main content

HashiCorp Packer Provisioning

TST, Hong Kong

So far we created a Docker image but did not do much with it. To set up our image we can use a range of so called provisioners that will execute scripts inside our image.

Shell Provisioner

"provisioners": [
    "type": "shell",
    "script": ""

The shell provisioner can use a shell script file "script: "" or an "inline": "command". We can use the init script to install a small dockerized Node.js / Express.js app inside our Ubuntu image:

#! /bin/bash

curl -fsSL | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli
sudo usermod -aG docker ubuntu
curl -sL -o
sudo apt install nodejs
git clone /home/ubuntu/app
sudo docker build -t application /home/ubuntu/app

Virtualbox Builder

The VirtualBox Packer builder is able to create VirtualBox virtual machines and export them in the OVA or OVF format.

Packer actually comes with multiple builders able to create VirtualBox machines, depending on the strategy you want to use to build the image. Packer supports the following VirtualBox builders:

  • virtualbox-iso - Starts from an ISO file, creates a brand new VirtualBox VM, installs an OS, provisions software within the OS, then exports that machine to create an image. This is best for people who want to start from scratch.

  • virtualbox-ovf - This builder imports an existing OVF/OVA file, runs provisioners on top of that VM, and exports that machine to create an image. This is best if you have an existing VirtualBox VM export you want to use as the source. As an additional benefit, you can feed the artifact of this builder back into itself to iterate on a machine.

  • virtualbox-vm - This builder uses an existing VM to run defined provisioners on top of that VM, and optionally creates a snapshot to save the changes applied from the provisioners. In addition the builder is able to export that machine to create an image. The builder is able to attach to a defined snapshot as a starting point, which could be defined statically or dynamically via a variable.

Create your Source Image

First you need to download the base image you want to start from - I will choose Ubuntu Server 20.10 ISO. Create an Ubuntu virtual machine in VirtualBox and install Ubuntu server from the ISO image. Once installed restart the server and and login with your created user. Once verified that everything is set up correctly shutdown the VM. Open the VirtualBox Tools menu and export the VM as an .OVA file.

Alternatively, you can also use the ISO file directly