Skip to content

Commit

Permalink
optimize Dockerfile, and stage cache
Browse files Browse the repository at this point in the history
  • Loading branch information
iutx committed Jun 11, 2024
1 parent d58a8f2 commit b979768
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions tools/dockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
FROM --platform=$TARGETPLATFORM golang:1.22-bullseye as build
ARG GO_VERSION=1.22
ARG BASE_IMAGE="golang:${GO_VERSION}-bullseye"

FROM --platform=$TARGETPLATFORM ${BASE_IMAGE} as build

ENV PROJ_ROOT="/go/src/github.com/erda-project/erda-infra"
COPY . "${PROJ_ROOT}"
WORKDIR "${PROJ_ROOT}"

ARG GOPROXY=https://goproxy.cn,direct
RUN go env -w GO111MODULE=on && go env -w GOPROXY="${GOPROXY}"
ENV GOPROXY=https://goproxy.cn,direct \
GO111MODULE=on \
CGO_ENABLED=0

# Copy go.mod and go.sum to leverage Docker cache
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download

# Copy all project files
COPY . .

# Install goimports and gohub
RUN go install golang.org/x/tools/cmd/goimports@latest

RUN --mount=type=cache,target=/root/.cache/go-build\
# Build gohub with cache mechanism
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
cd ./tools && \
go install ./gohub && \
go install ./tools/gohub && \
gohub tools install --verbose --local


FROM --platform=$TARGETPLATFORM golang:1.22-bullseye
FROM --platform=$TARGETPLATFORM ${BASE_IMAGE}

RUN go install golang.org/x/tools/cmd/goimports@latest

# Copy built tools from build stage
COPY --from=build /root/.gohub /root/.gohub
COPY --from=build /go/bin/* /go/bin/

WORKDIR /go

CMD gohub
CMD ["gohub"]

0 comments on commit b979768

Please sign in to comment.