From ea95d7ba421ee777a566b643b9b6f89258349046 Mon Sep 17 00:00:00 2001 From: oraz Date: Tue, 12 Mar 2024 08:28:35 +0200 Subject: [PATCH 1/2] Set Golang version in Dockerfile Directly install a desired Go version instead of first installing what the builder image provides, and then using Golang CLI to get the desired version. It stems from an error we have seen in building the contianer at post-submit job --- Dockerfile | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index d8c7db8b..a4942c06 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,28 @@ # Build the manager binary FROM quay.io/centos/centos:stream8 AS builder -RUN dnf install -y golang git \ +RUN dnf install -y golang jq git \ && dnf clean all -y WORKDIR /workspace -# Copy the Go Modules manifests +# Copy the Go Modules manifests for detecting Go version COPY go.mod go.mod COPY go.sum go.sum -# Ensure correct Go version -RUN export GO_VERSION=$(grep -E "go [[:digit:]]\.[[:digit:]][[:digit:]]" go.mod | awk '{print $2}') && \ - go install golang.org/dl/go${GO_VERSION}@latest && \ - ~/go/bin/go${GO_VERSION} download && \ - /bin/cp -f ~/go/bin/go${GO_VERSION} /usr/bin/go && \ - go version +RUN \ + # get Go version from mod file + export GO_VERSION=$(grep -E "go [[:digit:]]\.[[:digit:]][[:digit:]]" go.mod | awk '{print $2}') && \ + echo ${GO_VERSION} && \ + # find filename for latest z version from Go download page + export GO_FILENAME=$(curl -sL 'https://go.dev/dl/?mode=json&include=all' | jq -r "[.[] | select(.version | startswith(\"go${GO_VERSION}\"))][0].files[] | select(.os == \"linux\" and .arch == \"amd64\") | .filename") && \ + echo ${GO_FILENAME} && \ + # download and unpack + curl -sL -o go.tar.gz "https://golang.org/dl/${GO_FILENAME}" && \ + tar -C /usr/local -xzf go.tar.gz && \ + rm go.tar.gz + +# add Go directory to PATH +ENV PATH="${PATH}:/usr/local/go/bin" +RUN go version # Copy the go source COPY main.go main.go From 60997441e4cbb12ea1c2dc48d9bc1c62e8f1de76 Mon Sep 17 00:00:00 2001 From: oraz Date: Fri, 15 Mar 2024 16:46:49 +0200 Subject: [PATCH 2/2] Don't install Golang from base image Redundant step, and Golang desired version will be installed afterward from official page --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a4942c06..1d63e85c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ # Build the manager binary FROM quay.io/centos/centos:stream8 AS builder -RUN dnf install -y golang jq git \ +RUN dnf install -y jq git \ && dnf clean all -y WORKDIR /workspace