Index Keywords

↶ Return

How to Build Multiple Images in Parallel with Packer

Example:

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
40
41
42
43
44
45
packer {
required_plugins {
docker = {
version = ">= 0.0.7"
source = "github.com/hashicorp/docker"
}
}
}

variable "docker_image" {
type = string
default = "ubuntu:xenial"
}

source "docker" "ubuntu" {
image = var.docker_image
commit = true
}

source "docker" "ubuntu-bionic" {
image = "ubuntu:bionic"
commit = true
}

build {
name = "learn-packer"
sources = [
"source.docker.ubuntu",
"source.docker.ubuntu-bionic",
]

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."]
}
}

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.

1
$ docker images