From 6b6b80df37cb5c555d783d676358bc4bf45213c9 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Wed, 8 Jun 2022 17:42:03 +0700 Subject: [PATCH] chore: update 1.18 and use trick multi Go install --- ...file-go.md => 2022-06-08-dockerfile-go.md} | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) rename posts/{2021-12-18-dockerfile-go.md => 2022-06-08-dockerfile-go.md} (78%) diff --git a/posts/2021-12-18-dockerfile-go.md b/posts/2022-06-08-dockerfile-go.md similarity index 78% rename from posts/2021-12-18-dockerfile-go.md rename to posts/2022-06-08-dockerfile-go.md index 6453c2d..cf13805 100644 --- a/posts/2021-12-18-dockerfile-go.md +++ b/posts/2022-06-08-dockerfile-go.md @@ -6,7 +6,10 @@ Like set up `.gitignore`, CI configs, Dockerfile, ... So I decide to have a baseline Dockerfile like this: ```Dockerfile -FROM golang:1.18beta1-bullseye as builder +FROM golang:1.18-bullseye as builder + +RUN go install golang.org/dl/go1.18@latest \ + && go1.18 download WORKDIR /build @@ -36,10 +39,19 @@ So I stick with it for a while. Also, remember to match Distroless Debian version with Go official image Debian version. ```Dockerfile -FROM golang:1.18beta1-bullseye as builder +FROM golang:1.18-bullseye as builder ``` This is Go image I use as a build stage. +This can be official Go image or custom image is required in some companies. + +```Dockerfile +RUN go install golang.org/dl/go1.18@latest \ + && go1.18 download +``` + +This is optional. +In my case, my company is slow to update Go image so I use this trick to install latest Go version. ```Dockerfile WORKDIR /build @@ -54,10 +66,10 @@ I use `/build` to emphasize that I am building something in that directory. The 4 `COPY` lines are familiar if you use Go enough. First is `go.mod` and `go.sum` because it defines Go modules. -The second is `vendor` because I use it a lot, this is not necessary but I use it because I don't want each time I build Dockerfile, I need to redownload Go modules. +The second is `vendor`, this is optional but I use it because I don't want each time I build Dockerfile, I need to redownload Go modules. ```Dockerfile -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app main.go +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app . ``` This is where I build Go program.