Parallel builds
In the previous tutorials, you used Packer to automatically build an image and provision it from a template. Then, you parameterized the template allowing you to build different images using the same template. While this is already quite powerful, Packer can create multiple images in parallel, all configured from a single template.
Parellel build is a very useful and important feature of Packer. For example, Packer can build an Amazon AMI and a VMware virtual machine in parallel provisioned with the same scripts, resulting in near-identical images. The AMI can be used for production, the VMware machine can be used for development. Or, another example, if you're using Packer to build software appliances, then you can build the appliance for every supported platform all in parallel and configured from a single template.
In this tutorial, you will build a second Docker image in parallel.While the second image is a Docker image for the sake of this tutorial, the second source could just as easily come from another hypervisor or a cloud platform.
Prerequisites
This tutorial assumes that you are continuing from the previous tutorials. If not, follow the steps below before continuing.
Install Packer
Create a directory named
packer_tutorial
and paste the following configuration into a file namedaws-ubuntu.pkr.hcl
.Initialize the Packer template.
Once you have successfully initialized the template, you can continue with the rest of this tutorial.
Add parallel builds to template
To use parallel builds, create a source then add the source to the sources
array in the build block. Your sources do not need to be the same type. This tells Packer to build multiple images when that build is run.
Add the following source block to your aws-ubuntu.pkr.hcl
file. This is similar to the existing source block but has -focal
in the AMI name and uses ubuntu-focal-20.04
as the base AMI.
Then update your build block to use the new source.
Build images
Build the images. Packer will display color-coded output for both builds. You can tell which build source an output line is associated with using either the line color or prefix. In this example, there are two builds: amazon-ebs.ubuntu
and amazon-ebs.ubuntu-focal
.
Next steps
In this tutorial, you updated your template to build multiple images in parallel. Continue to the next tutorial to add a post-processing step to your Packer template.
Refer to the following resources for additional details on the concepts covered in this tutorial:
- Learn how to restrict provisioners to certain build sources with the
only
andexcept
attributes.