Skip to content

Commit

Permalink
Optimize docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
socheatsok78 committed Jun 23, 2024
1 parent 5070ea0 commit bad5d3f
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
ARG GO_VERSION
ARG ALPINE_VERSION

# buildkit
ARG TARGETOS="linux"
ARG TARGETARCH="amd64"
ARG BUILDPLATFORM="linux/amd64"
ARG GO_SOCKADDR_VERSION="master"

FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS builder
FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS builder
RUN apk add --no-cache git make
ARG GO_SOCKADDR_VERSION
ADD https://github.com/hashicorp/go-discover.git#${GO_SOCKADDR_VERSION} /hashicorp/go-discover
RUN <<EOT
ARG GO_SOCKADDR_VERSION="master"
ADD https://github.com/hashicorp/go-discover.git#${GO_SOCKADDR_VERSION} /tmp/source
RUN --mount=type=cache,target=/go/pkg/mod \
<<EOT
set -ex
cd /hashicorp/go-discover/cmd/discover
go build -o /discover
cd /tmp/source/cmd/discover
export CGO_ENABLED=0
export GOOS=linux
for GOARCH in amd64 arm64; do
export GOARCH
go build -o /discover-$GOOS-$GOARCH
done
EOT

FROM scratch
COPY --from=builder /hashicorp/go-discover/cmd/discover /discover
ARG TARGETOS
ARG TARGETARCH
COPY --from=builder /discover-$TARGETOS-$TARGETARCH /discover

0 comments on commit bad5d3f

Please sign in to comment.