Hashicorp’s Packer – build & manage machine images by couple of configuration

Packer is a tool for building identical machine images for multiple platforms from a single source configuration.

Packer is lightweight, runs on every major operating system, and is highly performant, creating machine images for multiple platforms in parallel

Packer can build images for AMIs (Amazon Machine Image), Digital Ocean, Docker, Vmware, VirtualBox, Vagrant… (Full list found here)

Inside this article, we’ll choose VirtualBox & Vagrant to demonstrate, since it’s free, convenient and widely adopted.

I. Preparation

– Install Packer

– Install VirtualBox

II. Make configuration

– As we stated recently, building a machine image by Packer is as easy as simple configuration. Packer takes advantage of JSON and it’s simple syntax.

Here’s an example:

packer_config_simple_example

Just a simple syntax to tell Packer:

– Use virtualbox as builder

– Base the machine on Ubuntu 14.04

– Setup default SSH username & password

– Provisioning: setup web stack packages with nginx, php-fpm..

– Some trivial configs, such as Shutdown message…

Actually, in real world we need to write a couple of configs to have a full-feature images. The code can be found here. In that config, we intend to install webstack with nginx & php & php-fpm packages.

III. Build

Let’s run:

$ packer build example.json

build_progress

Below are the output from our full configs:

$ packer build my_ubuntu14.json

build_progress_2

build_progress_3

packer_virtualbox

packer_virtualbox_done

IV. Use the image

– We have image now, it’s usable, even production-ready :)!

By this example, we have an image that can be used by your team with Vagrant (remember to install Vagrant if you haven’t)

$ vagrant box add ubuntu14_nginx builds/ubuntu-14.04.virtualbox.box

packer_vagrant_add

We now have a virtual machine with web-stack: nginx + php + php-fpm

vagrant_grep_nginx_php

For other builders, such as Amazon AMI, digitalocean… you can ‘push’ that image to have cloud running machine which ready for your usage!

I really love the idea of building images, modifying images, deploying images.. with just a couple of configuration changes. Packer and other Hashicorp opensource products do a great job like that. They explained their ecosystem here

If you have anything feedback or in the need of further discussion, please feel free to drop me an email at tuan_nh@septeni-technology.jp

Add a Comment

Scroll Up