Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Packer: build reproducible machine images in HCL

  • 20 min

By updated

Improve this page

Logo Packer

Packer is the HashiCorp tool that builds identical machine images from a single description file: a Docker image, an AWS AMI, an Azure, GCP or Proxmox image. This course takes you from your first template to a structured build chain, ready to plug into CI. The builder is the component that starts a temporary machine to produce the image: the examples use the Docker builder, with no cloud account and no cost.

It is aimed at developers, DevOps engineers, SREs and platform teams. It has three prerequisites, which the course plan declares: Terraform (you have already written HCL there, the language of Packer templates), Docker (the builder used in every example) and Ansible (for the ansible provisioner of the Provisioners lesson). The lessons do not teach these tools again: they use them.

What will you be able to do at the end of the course?

At the end of the seven lessons, you will be able to:

  • Explain what Packer builds and when to prefer it to a Dockerfile or to a configuration management tool;
  • Install Packer on Linux and verify the downloaded binary;
  • Write an HCL2 template and validate it with packer fmt, packer validate and packer inspect;
  • Parameterize that template with variables, locals, functions and data sources;
  • Configure the image with the shell, file, shell-local and ansible provisioners, then chain post-processors;
  • Structure a project, protect its secrets and debug a build.

How is this course organized?

The modules follow one another in order. The second assumes that a first Docker build has succeeded, and the third reuses the parameterized template of the second. The table repeats the titles and durations of the course plan shown in the left-hand column.

ModuleLessonsDurationWhat you will be able to do
Discover Packer and run a first build21 h 05"I know why Packer exists, it is installed and I have built a first Docker image"
Write a parameterized HCL2 template21 h"I write a clean HCL2 template and parameterize it with variables, locals and functions"
Configure the image and industrialize the build chain32 h"I configure my images with shell, file or Ansible, I chain post-processors and I can plug my templates into CI"

Each lesson ends with a 6-question quiz, drawn from a bank of 12 questions per lesson. There is no lab: the quizzes are the only validation mechanism of the course.

Discover Packer and run a first build

The first module lays the foundations and ends with an image that really exists. The Concepts and architecture lesson (25 min) explains why Packer exists, what a golden image is (a reference image, built once and reused as is to create identical machines), and how the builders, provisioners and post-processors chain together in a build. It also says when to prefer Packer to a Dockerfile or to Ansible.

The Install Packer and run a first build lesson (40 min) installs Packer on Linux, through the APT repository, the DNF repository, the zip archive whose checksum you verify with sha256sum -c, or mise. For macOS and Windows, it points to the official documentation. It checks the version, then builds a first Docker image from end to end.

mise is useful when your projects need different versions of Packer: a .mise.toml file pins the version of each project. Once mise is installed (the lesson points to its guide), installing Packer takes a single command:

Terminal window
mise use -g packer@1.16.2
mise ✓ packer@1.16.2 5.3s packer_1.16.2_linux_amd64.zip
mise ████████████████ 1/1 · installed 1 tool in 5.3s
mise ~/.config/mise/config.toml tools: packer@1.16.2

The output confirms that mise downloaded the Linux archive of Packer 1.16.2 and recorded it in its global configuration.

How do you write a Packer HCL2 template?

The heart of Packer is the HCL2 templates, files named .pkr.hcl. The Packer HCL2 templates lesson (25 min) details their structure: the packer, source and build blocks, splitting a template into several files, and the fmt, validate and inspect commands that catch errors before the build. The Packer variables, locals and functions lesson (35 min) then makes the template flexible: input variables, locals, .pkrvars.hcl files, data sources and built-in functions, so that you stop copying a template for each environment.

The template below is the one from the Concepts and Install lessons, without their shell provisioner. The packer block requires Packer 1.16.0 or later and declares the docker plugin. The source starts from an Alpine image pinned by its digest, and commit = true asks the builder to commit the container as an image at the end. The build block references the source; the lessons add provisioners and post-processors to it.

packer {
required_version = ">= 1.16.0"
required_plugins {
docker = {
version = "~> 1.1"
source = "github.com/hashicorp/docker"
}
}
}
source "docker" "alpine" {
image = "alpine:3.24@sha256:f59abe656b692d901e184be3445e084786602108599d2a51d229c0f5a48c71bf"
commit = true
}
build {
name = "hello-packer"
sources = ["source.docker.alpine"]
}

The @sha256:… digest pins the exact content of the starting image: without it, the same alpine:3.24 can point to a different image from one month to the next.

How do you configure the image and industrialize the build?

The last module installs the system in the image, transforms the artifact that was produced, then prepares the move to production. The Packer provisioners lesson (40 min) shows how to configure the temporary machine that the builder starts: the shell, file, shell-local and ansible provisioners run on it, before the builder freezes it into an image.

The Packer post-processors lesson (35 min) acts after the build, on the resulting artifact. The table gives the idea of each post-processor; the lesson details their options and chains them.

Post-processorResult
compressArchive of the image, in the format given by the extension (.tar.gz, .zip, .lz4...)
manifestJSON file with the build metadata
checksumChecksums, chosen by checksum_types (md5, sha256, etc.)
docker-importImport of the artifact into Docker Engine
docker-tagAdditional tag on the Docker image
docker-pushPush to a registry, with authentication; the lesson pushes two tags (the version and latest) to a local private registry

The Packer best practices lesson (45 min) industrializes the templates: project layout, Makefile, secrets management (through PKR_VAR_ environment variables, never in the template), debugging and CI integration. For debugging, PACKER_LOG turns on Packer's trace, and -debug runs the build step by step: the pause requires an interactive terminal.

Where should you start?

Follow the lessons in order, starting with the concepts, and replay the Docker examples of each module.

First lesson: Packer concepts and architecture

FAQ: frequently asked questions

The questions most often asked about Packer: difference with a Dockerfile or with Terraform, license, installation, provisioners and CI/CD integration. Packer has been distributed under the BSL license (Business Source License, identifier BUSL-1.1) since version 1.10.0.

Additional resources

Keep these addresses at hand during the course. The official documentation and the HCL2 reference answer most syntax questions, the integrations page lists the plugins available for each platform, and the HashiCorp FAQ covers troubleshooting cases.