From 50bb098a56c43e8a3d2ad2a0d7f3d95d0b846766 Mon Sep 17 00:00:00 2001 From: peefy Date: Wed, 4 Sep 2024 16:17:17 +0800 Subject: [PATCH] refactor: docker images Signed-off-by: peefy --- docker/amd64/Dockerfile.init | 44 ++++++++++++++++++++-------------- docker/amd64/Dockerfile.server | 44 +++++++++++++++++++--------------- 2 files changed, 51 insertions(+), 37 deletions(-) diff --git a/docker/amd64/Dockerfile.init b/docker/amd64/Dockerfile.init index 6687182..3246cbf 100644 --- a/docker/amd64/Dockerfile.init +++ b/docker/amd64/Dockerfile.init @@ -1,18 +1,18 @@ -FROM --platform=${BUILDPLATFORM} golang:1.23 AS builder +# We use the Go 1.23 version unless asked to use something else. +# The GitHub Actions CI job sets this argument for a consistent Go version. +ARG GO_VERSION=1.23 +ARG BASE_IMAGE=kcllang/kcl -# The TARGETOS and TARGETARCH args are set by docker. We set GOOS and GOARCH to -# these values to ask Go to compile a binary for these architectures. If -# TARGETOS and TARGETOS are different from BUILDPLATFORM, Go will cross compile -# for us (e.g. compile a linux/amd64 binary on a linux/arm64 build machine). -ARG TARGETOS -ARG TARGETARCH +# Setup the base environment. The BUILDPLATFORM is set automatically by Docker. +# The --platform=${BUILDPLATFORM} flag tells Docker to build the function using +# the OS and architecture of the host running the build, not the OS and +# architecture that we're building the function for. +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} as build -ENV GO111MODULE=on \ - GOPROXY=https://goproxy.cn,direct +COPY / /src +WORKDIR /src -WORKDIR / - -COPY . . +ENV CGO_ENABLED=0 # We run go mod download in a separate step so that we can cache its results. # This lets us avoid re-downloading modules if we don't need to. The type=target @@ -20,14 +20,22 @@ COPY . . # The type=cache mount tells Docker to cache the Go modules cache across builds. RUN --mount=target=. --mount=type=cache,target=/go/pkg/mod go mod download -RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o webhook-init cmd/wehbook-init/main.go - -FROM kcllang/kcl +# The TARGETOS and TARGETARCH args are set by docker. We set GOOS and GOARCH to +# these values to ask Go to compile a binary for these architectures. If +# TARGETOS and TARGETOS are different from BUILDPLATFORM, Go will cross compile +# for us (e.g. compile a linux/amd64 binary on a linux/arm64 build machine). +ARG TARGETOS +ARG TARGETARCH -WORKDIR / -COPY --from=builder /webhook-server . +# Build the webhook init binary. The type=target mount tells Docker to mount the +# current directory read-only in the WORKDIR. The type=cache mount tells Docker +# to cache the Go modules cache across builds. +RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o webhook-init cmd/wehbook-init/main.go +FROM ${BASE_IMAGE} as image +RUN apt-get update && apt-get install -y ca-certificates tini +COPY --from=build /src/webhook-init /usr/local/bin/ ENV KCL_FAST_EVAL=1 ENV LANG="en_US.UTF-8" -ENTRYPOINT ["/webhook-init"] +ENTRYPOINT ["/usr/local/bin/webhook-init"] diff --git a/docker/amd64/Dockerfile.server b/docker/amd64/Dockerfile.server index 7d384ca..6a7b257 100644 --- a/docker/amd64/Dockerfile.server +++ b/docker/amd64/Dockerfile.server @@ -1,18 +1,16 @@ -FROM --platform=${BUILDPLATFORM} golang:1.23 AS builder +# We use the Go 1.23 version unless asked to use something else. +# The GitHub Actions CI job sets this argument for a consistent Go version. +ARG GO_VERSION=1.23 +ARG BASE_IMAGE=kcllang/kcl -# The TARGETOS and TARGETARCH args are set by docker. We set GOOS and GOARCH to -# these values to ask Go to compile a binary for these architectures. If -# TARGETOS and TARGETOS are different from BUILDPLATFORM, Go will cross compile -# for us (e.g. compile a linux/amd64 binary on a linux/arm64 build machine). -ARG TARGETOS -ARG TARGETARCH - -ENV GO111MODULE=on \ - GOPROXY=https://goproxy.cn,direct +# Setup the base environment. The BUILDPLATFORM is set automatically by Docker. +# The --platform=${BUILDPLATFORM} flag tells Docker to build the function using +# the OS and architecture of the host running the build, not the OS and +# architecture that we're building the function for. +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} as build -WORKDIR / - -COPY . . +COPY / /src +WORKDIR /src ENV CGO_ENABLED=0 @@ -22,14 +20,22 @@ ENV CGO_ENABLED=0 # The type=cache mount tells Docker to cache the Go modules cache across builds. RUN --mount=target=. --mount=type=cache,target=/go/pkg/mod go mod download -RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o webhook-server cmd/wehbook-server/main.go - -FROM kcllang/kcl +# The TARGETOS and TARGETARCH args are set by docker. We set GOOS and GOARCH to +# these values to ask Go to compile a binary for these architectures. If +# TARGETOS and TARGETOS are different from BUILDPLATFORM, Go will cross compile +# for us (e.g. compile a linux/amd64 binary on a linux/arm64 build machine). +ARG TARGETOS +ARG TARGETARCH -WORKDIR / -COPY --from=builder /webhook-server . +# Build the webhook server binary. The type=target mount tells Docker to mount the +# current directory read-only in the WORKDIR. The type=cache mount tells Docker +# to cache the Go modules cache across builds. +RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o webhook-server cmd/wehbook-server/main.go +FROM ${BASE_IMAGE} as image +RUN apt-get update && apt-get install -y ca-certificates tini +COPY --from=build /src/webhook-server /usr/local/bin/ ENV KCL_FAST_EVAL=1 ENV LANG="en_US.UTF-8" -ENTRYPOINT ["/webhook-server"] +ENTRYPOINT ["/usr/local/bin/webhook-server"]