How to Parallel Docker image builds with Packer
- 4 minsWhat is Packer?
- Packer is a tool that lets you create identical machine images for multiple platforms from a single source template. Packer can create golden images to use in image pipelines.
- Packer is kind of IaC tool, You can build images and automate them for your different environment. You can build different type of images like AWS AMIs, Docker images, VM ware images and more. You can use a template like hcl, json or Yaml. Packer includes some features and plugins for manipulate for images. You can create base images which is include softwares and additional packages. Packer is an open source tool for creating 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.
Install Packer to Debian
- HashiCorp officially maintains and signs packages for the following Linux distributions.
- Add the HashiCorp GPG key.
- Add the official HashiCorp Linux repository.
- Update and install.
- Verifying the Installation
Build an Image and Push the image to Docker Hub
- With Packer installed, it is time to build your first image. For speed, simplicity’s sake, and because it’s free to download, you will build a Docker container. You can download Docker from https://docs.docker.com/get-docker/ if you don’t have it installed.
- Write Packer template, A Packer template is a configuration file that defines the image you want to build and how to build it. Packer templates use the Hashicorp Configuration Language (HCL).
- Create a file docker-ubuntu.pkr.hcl
- Add following HCL block to it and save the file.
- Packer Block
- The packer {} block contains Packer settings, including specifying a required Packer version.
- In addition, you will find required_plugins block in the Packer block, which specifies all the plugins required by the template to build your image. Even though Packer is packaged into a single binary, it depends on plugins for much of its functionality. Some of these plugins, like the Docker Builder which you will to use, are built, maintained, and distributed by HashiCorp — but anyone can write and use plugins.
- Each plugin block contains a version and source attribute. Packer will use these attributes to download the appropriate plugin(s)..
- The source block configures a specific builder plugin, which is then invoked by a build block.
- The build block defines what Packer should do with the Docker container after it launches.
- Initialize Packer configuration, Initialize your Packer configuration.
- 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.
- Build images
- After build images succesfully We can verify that We have images
-
We used parallel images that means 2 different image with different tags and also We just push the only 1 image to repository
- If We check our Dockerhub repository We can verify that We have only 1 image.
Guneycan Sanli.