Index Keywords Recent

↶ Return Home

How to Provision a Docker Image with Packer

Last Updated May 3rd 2022, 12:41:16 am

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
packer {
required_plugins {
docker = {
version = ">= 0.0.7"
source = "github.com/hashicorp/docker"
}
}
}

source "docker" "ubuntu" {
image = "ubuntu:xenial"
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" {
script = "scripts/script.sh"
}

provisioner "shell" {
inline = ["echo This provisioner runs last"]
}
}

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