Skip to content

Commit

Permalink
refactor(flagd): update profile.Dockerfile with buildkit caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Lam Tran committed Jun 29, 2023
1 parent 75223e2 commit 5df353e
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions flagd/profile.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,40 @@
# Build the manager binary
FROM --platform=$BUILDPLATFORM golang:1.20-alpine AS builder

WORKDIR /workspace
WORKDIR /src

ARG TARGETOS
ARG TARGETARCH
ARG VERSION
ARG COMMIT
ARG DATE

# Copy source code
COPY flagd/ flagd
COPY core/ core

# Setup go workspace
RUN go work init
RUN go work use ./flagd
RUN go work use ./core

# Go get dependencies
RUN cd flagd && go mod download
# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds.
# Leverage bind mounts to go.sum and go.mod to avoid having to copy them into
# the container.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,source=./core/go.mod,target=./core/go.mod \
--mount=type=bind,source=./core/go.sum,target=./core/go.sum \
--mount=type=bind,source=./flagd/go.mod,target=./flagd/go.mod \
--mount=type=bind,source=./flagd/go.sum,target=./flagd/go.sum \
go work init ./core ./flagd && go mod download

# Build with profiler
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -ldflags "-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.date=${DATE}" -o flagd-build flagd/main.go flagd/profiler.go
# Build the application.
# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds.
# Leverage a bind mount to the current directory to avoid having to copy the
# source code into the container.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=bind,source=./core,target=./core \
--mount=type=bind,source=./flagd,target=./flagd \
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -ldflags "-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.date=${DATE}" -o /bin/flagd-build ./flagd/main.go ./flagd/profiler.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/flagd-build .
COPY --from=builder /bin/flagd-build .
USER 65532:65532

ENTRYPOINT ["/flagd-build"]

0 comments on commit 5df353e

Please sign in to comment.