Skip to content

Commit

Permalink
refactor(flagd-proxy): update build.Dockerfile with buildkit caching (#…
Browse files Browse the repository at this point in the history
…725)

<!-- Please use this template for your pull request. -->
<!-- Please use the sections that you need and delete other sections -->

## This PR
<!-- add the description of the PR here -->

updates `flagd-proxy/build.Dockerfile` with changes:

- use less layers (from 7 to 2)
- leverage buildkit features: cache (go mod, go build) and bind

### Related Issues
<!-- add here the GitHub issue that this PR resolves if applicable -->


### Notes
<!-- any additional notes for this PR -->

- This change is motivated from the new command of docker: `docker init`
(reference:
https://docs.docker.com/engine/reference/commandline/init/#example-of-selecting-go)

### Follow-up Tasks
<!-- anything that is related to this PR but not done here should be
noted under this section -->
<!-- if there is a need for a new issue, please link it here -->

### How to test
<!-- if applicable, add testing instructions under this section -->

```
$ docker build . -f ./flagd-proxy/build.Dockerfile -t flagd-proxy
[+] Building 19.3s (13/13) FINISHED                                                                                                               docker:orbstack
 => [internal] load build definition from build.Dockerfile                                                                                                   0.0s
 => => transferring dockerfile: 1.99kB                                                                                                                       0.0s
 => [internal] load .dockerignore                                                                                                                            0.0s
 => => transferring context: 2B                                                                                                                              0.0s
 => [internal] load metadata for gcr.io/distroless/static:nonroot                                                                                            0.7s
 => [internal] load metadata for docker.io/library/golang:1.20-alpine                                                                                        2.1s
 => [auth] library/golang:pull token for registry-1.docker.io                                                                                                0.0s
 => [builder 1/4] FROM docker.io/library/golang:1.20-alpine@sha256:fd9d9d7194ec40a9a6ae89fcaef3e47c47de7746dd5848ab5343695dbbd09f8c                          0.0s
 => CACHED [stage-1 1/3] FROM gcr.io/distroless/static:nonroot@sha256:9ecc53c269509f63c69a266168e4a687c7eb8c0cfd753bd8bfcaa4f58a90876f                       0.0s
 => [internal] load build context                                                                                                                            0.0s
 => => transferring context: 298.78kB                                                                                                                        0.0s
 => CACHED [builder 2/4] WORKDIR /src                                                                                                                        0.0s
 => [builder 3/4] RUN --mount=type=cache,target=/go/pkg/mod/     --mount=type=bind,source=./core/go.mod,target=./core/go.mod     --mount=type=bind,source=.  0.7s
 => [builder 4/4] RUN --mount=type=cache,target=/go/pkg/mod/     --mount=type=cache,target=/root/.cache/go-build     --mount=type=bind,source=./core,targe  16.2s
 => [stage-1 2/3] COPY --from=builder /bin/flagd-build .                                                                                                     0.0s
 => exporting to image                                                                                                                                       0.1s
 => => exporting layers                                                                                                                                      0.1s
 => => writing image sha256:671b02ae96c27e2a1c3dc336c2a25fd2b1897d7737cb805cb5e0d72c562965b5                                                                 0.0s
 => => naming to docker.io/library/flagd-proxy                                                                                                               0.0s

$ docker run flagd-proxy        

                 ______   __       ________   _______    ______      
                /_____/\ /_/\     /_______/\ /______/\  /_____/\     
                \::::_\/_\:\ \    \::: _  \ \\::::__\/__\:::_ \ \    
                 \:\/___/\\:\ \    \::(_)  \ \\:\ /____/\\:\  \ \ \   
                  \:::._\/ \:\ \____\:: __  \ \\:\\_  _\/ \:\ \ \ \  
                   \:\ \    \:\/___/\\:.\ \  \ \\:\_\ \ \  \:\/.:| | 
                    \_\/     \_____\/ \__\/\__\/ \_____\/   \____/_/ 
                                                   Kubernetes Proxy  

flagd-proxy allows flagd to subscribe to CRD changes without the required permissions.

Usage:
  flagd [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  start       Start flagd-proxy

Flags:
      --config string   config file (default is $HOME/.agent.yaml)
  -x, --debug           verbose logging
  -h, --help            help for flagd

Use "flagd [command] --help" for more information about a command.
```

Signed-off-by: Lam Tran <lam.tran@spenmo.com>
Co-authored-by: Lam Tran <lam.tran@spenmo.com>
Co-authored-by: Todd Baert <toddbaert@gmail.com>
  • Loading branch information
3 people authored Jul 5, 2023
1 parent 7b109c7 commit 06f3d2e
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions flagd-proxy/build.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,43 @@
# 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-proxy/ flagd-proxy
COPY core/ core
COPY flagd/ flagd

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

# Go get dependencies
RUN cd flagd-proxy && 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 \
--mount=type=bind,source=./flagd-proxy/go.mod,target=./flagd-proxy/go.mod \
--mount=type=bind,source=./flagd-proxy/go.sum,target=./flagd-proxy/go.sum \
go work init ./core ./flagd ./flagd-proxy && go mod download

# # Build
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-proxy/main.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 \
--mount=type=bind,source=./flagd-proxy,target=./flagd-proxy \
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-proxy/main.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"]

1 comment on commit 06f3d2e

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Go Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: 06f3d2e Previous: 7b109c7 Ratio
BenchmarkResolveBooleanValue/test_staticBoolFlag 2624 ns/op 304 B/op 7 allocs/op 1916 ns/op 304 B/op 7 allocs/op 1.37

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.