Post-processors
In the last tutorial, you updated your template to build multiple images in parallel.
While provisioners are run against an instance while it is running, post-processors run only after Packer saves the instance as an image. Post-processors are extremely varied in their function; they can compress your artifact, upload your artifact into a cloud, or create a file that describes the artifact and build.
In this tutorial, you will add the docker-tag post-processor to add a tag to the Docker image.
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 nameddocker-ubuntu.pkr.hcl
.Initialize the Packer template.
Once you have successfully initialized the template, you can continue with the rest of this tutorial.
Add post-processor to template
Add the following post-processor blocks inside the build block but after the provisioner step. These post-processor blocks will tag each image with the appropriate tags.
Your build block should look like the following.
Build and tag images
Build the images.
List the images to verify Packer successfully tagged each image.
Notice how there are only three images. This is because the ubuntu-focal
post-processing step overrides the image created by ubuntu-jammy
. You can verify this by reading through the Packer build logs.
Sequential post-processing steps
You may add as many post-processors as you want using the post-processor
syntax, but each one will start from the original artifact output by the builder, not the
artifact created by a previously declared post-processor.
Use the post-processors
(note the pluralization) block to create post-processing pipelines where the output of one post-processor becomes the input to another post-processor.
For example, the following configuration will tag your image then push it to Docker Hub. You do not need to add this to your configuration for this tutorial, since it requires a Docker account and will push your image to Docker Hub.
You can add as many post-processors blocks as you want, or mix-and-match the one-off post-processors with the post-processors
blocks to create multiple pathways for post-processing.
Next steps
In this tutorial, you added post-processors to your Packer template to tag Docker images.
This is the end of the getting started tutorials for Packer. You should now be comfortable with basic Packer usage, should understand templates, defining builds, provisioners, post-processors, and more. At this point, you are ready to begin playing with and using Packer in real scenarios.
If you would like to learn how to use Packer to build Amazon images, check out our Get Started - Amazon tutorials.
The most important reference in your Packer journey will be the documentation. The documentation contains reference material for Packer's features and configuration options.
As you use Packer more, please voice your comments and concerns on the community forum. Additionally, Packer is open source so please contribute if you'd like to. Contributions are very welcome.