This example uses packer to build a docker image based on the ubuntu:xenial docker image.
The docker-ubuntu.pkr.hcl file is the packer file that is used by packer to build the image.
The packer.required_plugins section outlines what plugins to use to build the image.
In this example the docker plugin version 0.0.7 and greater will be used to build the image.
packer {
required_plugins {
docker = {
version = ">= 0.0.7"
source = "github.com/hashicorp/docker"
}
}
}
The source section is used to configure the build plugin's configuration. In this example docker is the builder plugin that is being used.
The code below creates a new build source named docker.ubuntu that configures the docker build plugin to use the ubuntu:xenial image as a base for the new image.
The commit=true statements tells the docker plugin to create containter locally on the user's machine.
source "docker" "ubuntu" {
image = "ubuntu:xenial"
commit = true
}
The source "docker" "ubuntu" statement tells packer to use the docker build type and label the source as docker.ubuntu.
The build section defines what should be done with the container after it is launch.
Normally this is when steps like provisioning the container happen but in this example there is no provisioner setup.
build {
name = "learn-packer"
sources = [
"source.docker.ubuntu"
]
}
Run the commands below to build the image:
packer init .- This command will download all the plugins listed in the required_plugins sections
packer build docker-ubuntu.pkr.hcl- This command will build the image
docker images- Verify the image was created by using the
docker imagescommand to list the images and verify the newly created image is in the list
- Verify the image was created by using the
Note: Packer doesn't the manage the images so you will have to remove them manually using the docker rmi IMAGE_ID command.
packer fmt .- Formats the .hcl file
packer validate .- Checks the .hcl file for any invalid configs