I spy with my little eye: another way to build docker images.
Except not really, packer just shells out to a local instance of docker to do the docker building, and it doesn't even have a way to replicate multistage builds (no way to pull data out of an image). Oh, and no layer caching (or any other caching for that matter).
This means it's basically only good for adding some stuff to a base image.
If you still think this is a good idea:
1source "docker" "base" {
2 image = "golang:alpine"
3 export_path = "single.tar"
4}
5
6build {
7 name = "singlestage"
8 sources = [
9 "source.docker.base",
10 ]
11
12 # alternatively have everything in a subdirectory
13 # and provision the entire dir instead
14 provisioner "shell" {
15 inline = [
16 "mkdir /workspace",
17 ]
18 }
19
20 provisioner "file" {
21 sources = [
22 "go.mod",
23 "main.go",
24 ]
25 destination = "/workspace/"
26 }
27
28 provisioner "shell" {
29 inline = [
30 "cd /workspace",
31 "CGO_ENABLED=0 go build -o /usr/local/bin/app",
32 ]
33 }
34
35 post-processor "docker-import" {
36 repository = "seankhliao/packer-test01"
37 tag = "t0"
38 changes = [
39 "ENTRYPOINT [\"/usr/local/bin/app\"]",
40 ]
41 }
42}