Skip to content

Commit 50bb098

Browse files
committed
refactor: docker images
Signed-off-by: peefy <xpf6677@163.com>
1 parent da9e670 commit 50bb098

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
lines changed

docker/amd64/Dockerfile.init

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1-
FROM --platform=${BUILDPLATFORM} golang:1.23 AS builder
1+
# We use the Go 1.23 version unless asked to use something else.
2+
# The GitHub Actions CI job sets this argument for a consistent Go version.
3+
ARG GO_VERSION=1.23
4+
ARG BASE_IMAGE=kcllang/kcl
25

3-
# The TARGETOS and TARGETARCH args are set by docker. We set GOOS and GOARCH to
4-
# these values to ask Go to compile a binary for these architectures. If
5-
# TARGETOS and TARGETOS are different from BUILDPLATFORM, Go will cross compile
6-
# for us (e.g. compile a linux/amd64 binary on a linux/arm64 build machine).
7-
ARG TARGETOS
8-
ARG TARGETARCH
6+
# Setup the base environment. The BUILDPLATFORM is set automatically by Docker.
7+
# The --platform=${BUILDPLATFORM} flag tells Docker to build the function using
8+
# the OS and architecture of the host running the build, not the OS and
9+
# architecture that we're building the function for.
10+
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} as build
911

10-
ENV GO111MODULE=on \
11-
GOPROXY=https://goproxy.cn,direct
12+
COPY / /src
13+
WORKDIR /src
1214

13-
WORKDIR /
14-
15-
COPY . .
15+
ENV CGO_ENABLED=0
1616

1717
# We run go mod download in a separate step so that we can cache its results.
1818
# This lets us avoid re-downloading modules if we don't need to. The type=target
1919
# mount tells Docker to mount the current directory read-only in the WORKDIR.
2020
# The type=cache mount tells Docker to cache the Go modules cache across builds.
2121
RUN --mount=target=. --mount=type=cache,target=/go/pkg/mod go mod download
2222

23-
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o webhook-init cmd/wehbook-init/main.go
24-
25-
FROM kcllang/kcl
23+
# The TARGETOS and TARGETARCH args are set by docker. We set GOOS and GOARCH to
24+
# these values to ask Go to compile a binary for these architectures. If
25+
# TARGETOS and TARGETOS are different from BUILDPLATFORM, Go will cross compile
26+
# for us (e.g. compile a linux/amd64 binary on a linux/arm64 build machine).
27+
ARG TARGETOS
28+
ARG TARGETARCH
2629

27-
WORKDIR /
28-
COPY --from=builder /webhook-server .
30+
# Build the webhook init binary. The type=target mount tells Docker to mount the
31+
# current directory read-only in the WORKDIR. The type=cache mount tells Docker
32+
# to cache the Go modules cache across builds.
33+
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o webhook-init cmd/wehbook-init/main.go
2934

35+
FROM ${BASE_IMAGE} as image
36+
RUN apt-get update && apt-get install -y ca-certificates tini
37+
COPY --from=build /src/webhook-init /usr/local/bin/
3038
ENV KCL_FAST_EVAL=1
3139
ENV LANG="en_US.UTF-8"
3240

33-
ENTRYPOINT ["/webhook-init"]
41+
ENTRYPOINT ["/usr/local/bin/webhook-init"]

docker/amd64/Dockerfile.server

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
FROM --platform=${BUILDPLATFORM} golang:1.23 AS builder
1+
# We use the Go 1.23 version unless asked to use something else.
2+
# The GitHub Actions CI job sets this argument for a consistent Go version.
3+
ARG GO_VERSION=1.23
4+
ARG BASE_IMAGE=kcllang/kcl
25

3-
# The TARGETOS and TARGETARCH args are set by docker. We set GOOS and GOARCH to
4-
# these values to ask Go to compile a binary for these architectures. If
5-
# TARGETOS and TARGETOS are different from BUILDPLATFORM, Go will cross compile
6-
# for us (e.g. compile a linux/amd64 binary on a linux/arm64 build machine).
7-
ARG TARGETOS
8-
ARG TARGETARCH
9-
10-
ENV GO111MODULE=on \
11-
GOPROXY=https://goproxy.cn,direct
6+
# Setup the base environment. The BUILDPLATFORM is set automatically by Docker.
7+
# The --platform=${BUILDPLATFORM} flag tells Docker to build the function using
8+
# the OS and architecture of the host running the build, not the OS and
9+
# architecture that we're building the function for.
10+
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} as build
1211

13-
WORKDIR /
14-
15-
COPY . .
12+
COPY / /src
13+
WORKDIR /src
1614

1715
ENV CGO_ENABLED=0
1816

@@ -22,14 +20,22 @@ ENV CGO_ENABLED=0
2220
# The type=cache mount tells Docker to cache the Go modules cache across builds.
2321
RUN --mount=target=. --mount=type=cache,target=/go/pkg/mod go mod download
2422

25-
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o webhook-server cmd/wehbook-server/main.go
26-
27-
FROM kcllang/kcl
23+
# The TARGETOS and TARGETARCH args are set by docker. We set GOOS and GOARCH to
24+
# these values to ask Go to compile a binary for these architectures. If
25+
# TARGETOS and TARGETOS are different from BUILDPLATFORM, Go will cross compile
26+
# for us (e.g. compile a linux/amd64 binary on a linux/arm64 build machine).
27+
ARG TARGETOS
28+
ARG TARGETARCH
2829

29-
WORKDIR /
30-
COPY --from=builder /webhook-server .
30+
# Build the webhook server binary. The type=target mount tells Docker to mount the
31+
# current directory read-only in the WORKDIR. The type=cache mount tells Docker
32+
# to cache the Go modules cache across builds.
33+
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o webhook-server cmd/wehbook-server/main.go
3134

35+
FROM ${BASE_IMAGE} as image
36+
RUN apt-get update && apt-get install -y ca-certificates tini
37+
COPY --from=build /src/webhook-server /usr/local/bin/
3238
ENV KCL_FAST_EVAL=1
3339
ENV LANG="en_US.UTF-8"
3440

35-
ENTRYPOINT ["/webhook-server"]
41+
ENTRYPOINT ["/usr/local/bin/webhook-server"]

0 commit comments

Comments
 (0)