sample docker-ubuntu.pkr.hcl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| packer { required_plugins { docker = { version = ">= 0.0.7" source = "github.com/hashicorp/docker" } } }
source "docker" "ubuntu" { image = var.docker_image commit = true }
build { name = "learn-packer" sources = [ "source.docker.ubuntu" ]
provisioner "shell" { environment_vars = [ "FOO=hello world", ] inline = [ "echo Adding file to Docker Container", "echo \"FOO is $FOO\" > example.txt", ] }
provisioner "shell" { inline = ["echo Running ${var.docker_image} Docker image."] } }
variable "docker_image" { type = string default = "ubuntu:xenial" }
|
Packer will automatically load any variable file that matches the name *.auto.pkrvars.hcl, without the need to pass the file via the command line.
Example variable file example.auto.pkvars.hcl
1
| docker_image = "ubuntu:bionic"
|
Build
Run through the following Pack commands to build the image(s):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Initialize your Packer configuration.
$ packer init .
Format your template.
$ packer fmt .
Validate your template.
$ packer validate .
Build the image.
$ packer .
|
Verify
List all the Docker images to confirm that Packer successfully built your Docker image.