Skip to content

Commit

Permalink
wip: Fix docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Mar 3, 2022
1 parent d20ad71 commit c24ac00
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
# Build the manager binary
FROM golang:1.17 as builder

ARG TARGETPLATFORM
FROM --platform=$BUILDPLATFORM golang:1.17 as builder

WORKDIR /workspace

ENV GO111MODULE=on \
CGO_ENABLED=0

# # Copy the Go Modules manifests
# COPY go.mod go.sum ./
# Copy the Go Modules manifests
COPY go.mod go.sum ./

# # cache deps before building and copying source so that we don't need to re-download as much
# # and so that source changes don't invalidate our downloaded layer
# RUN --mount=type=cache,target=/go/pkg/mod go mod download
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer.
#
# Also, we need to do this before setting TARGETPLATFORM/TARGETOS/TARGETARCH/TARGETVARIANT
# so that go mod cache is shared across platforms.
RUN go mod download

# Copy the go source
# COPY . .

ARG TARGETOS
ARG TARGETARCH
# Usage:
# docker buildx build --tag repo/img:tag -f ./Dockerfile . --platform linux/amd64,linux/arm64,linux/arm/v7
#
# With the above commmand,
# TARGETOS can be "linux", TARGETARCH can be "amd64", "arm64", and "arm", TARGETVARIANT can be "v7".

ARG TARGETPLATFORM TARGETOS TARGETARCH TARGETVARIANT

# We intentionally avoid `--mount=type=cache,mode=0777,target=/go/pkg/mod` in the `go mod download` and the `go build` runs
# to avoid https://github.com/moby/buildkit/issues/2334
# We can use docker layer cache so the build is fast enogh anyway

# Build
RUN --mount=target=. \
--mount=type=cache,mode=0777,target=/root/.cache/go-build \
--mount=type=cache,mode=0777,target=/go/pkg/mod\
GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
GOARM=$(echo ${TARGETPLATFORM} | cut -d / -f3 | cut -c2-) \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} GOARM=${TARGETVARIANT#v} \
go build -o /out/manager main.go && go build -o /out/github-webhook-server ./cmd/githubwebhookserver

# Use distroless as minimal base image to package the manager binary
Expand Down

0 comments on commit c24ac00

Please sign in to comment.