diff --git a/Dockerfile b/Dockerfile index d8c7db8b..1d63e85c 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 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