Packer is a build tool that creates machine images for a variety of target virtual platforms e.g. VmWare, VirtualBox, Docker etc
The 'packer' has only a few commands, the primary one being 'build'. The '*.packer' template file is a json script that contains instructions on how to build one or more build artifacts.
Template functions and variables
Global template variables and functions can be used to insert information at runtime e.g. {{timestamp}}, {{uuid}}, {{pwd}} etc.
Localized variables are identified with a dot before their name e.g. {{.Name}}
User variables can also be declared and used by prepending the variable name with 'user' e.g. {{user `my_variable`}}
Environment variables are prepended with 'env' e.g. {{env `variable_name`}}
User variables can be declared using:
In a variables section
On the command line
Reading environment variables
The packer template file supports a number of sections.
Builders
The builders section is the only mandatory section. It defines one or more machine images that the template will create e.g. it could create three separate machine images for vmWare, Docker and VirtualBox. The builder declares a starting image along with options needed to deploy the base image. The Provisioners are then run against this instance. The builder then makes a new image that the 'post-processors' will then operate on.
Provisioners
Once the Builders base image has been deployed, packer will run the Provisioners section. Provisioners modify the base Builder machine. A number of Provisioners are available such as 'shell', 'ansible' and many others. Builder specific switches are possible for each Provisioner instance; using the keywords 'only' and 'except' that specify an array of builder names.
packer build base-centos7.packer
{
"type": "shell",
"script": "script-to-run-on-docker-container.sh",
"only": ["builder-for-docker"]
}
Post-Processors
Post processors are executed after the builder has created the new image. It can do things like compressing images and copying them to a repository.