FROM golang:1.18beta1-bullseye as builder

WORKDIR /build

COPY go.mod.
COPY go.sum.
COPY vendor.
COPY..

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app main.go

FROM gcr.io/distroless/base-debian11

COPY --from=builder /build/app /app

ENTRYPOINT ["/app"]

Use multi-stage build to keep my image size.
The first stage is the official Go mirror
The second stage is Distroless

Before Distroless, I used the official Alpine mirror.
There is a lot of discussion on the Internet about choosing which is the best base mirror for Go.
After reading some blogs, I found that Distroless is a small and safe base image.

So I persisted for a while

Remember to match the Distroless Debian version with the official Go image Debian version

FROM golang:1.18beta1-bullseye as builder

This is the Go image I used for the build stage

WORKDIR /build

COPY go.mod.
COPY go.sum.
COPY vendor.
COPY..

My /build used to emphasize that I was building something in that directory.

COPY If you use enough Go, these 4 lines will be familiar.
The first is go.mod and go.sum, because it defines the go to module.
The second is the vendor because I use a lot, this is not necessary, but I use it because I don’t want to re-download the Go module every time I build a Dockerfile

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app main.go

This is where I build Go programs.
CGO_ENABLED=0 because I don't want to mess up the C library.
GOOS=linux GOARCH=amd64 is easy to explain, Linux and x86-64.
GOAMD64=v3 is new since Go 1.18,
I use v3 because I read about AMD64 version in Arch Linux rfcs. TLDR’s newer computer is already x86-64-v3

FROM gcr.io/distroless/base-debian11

COPY --from=builder /build/app /app

ENTRYPOINT ["/app"]

Finally, I copied the app to the Distroless base image

Likes(0)

Comment list count 0 Comments

No Comments

WeChat Self-Service

WeChat Consult

TaoBao

support@elephdev.com

发表
评论
Go
Top