Skip to content

Commit ee82b3e

Browse files
authored
Add docker-push-w-buildx make target (kubernetes-sigs#3135)
* Add docker-push-w-buildx make target * Update BASE_IMAGE Signed-off-by: Ivy Ostosh <[email protected]> --------- Signed-off-by: Ivy Ostosh <[email protected]>
1 parent d18e523 commit ee82b3e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Dockerfile

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# syntax=docker/dockerfile:experimental
2+
ARG BASE_IMAGE
3+
ARG BUILD_IMAGE
4+
5+
FROM --platform=${TARGETPLATFORM} $BUILD_IMAGE AS base
6+
WORKDIR /workspace
7+
# Copy the Go Modules manifests
8+
COPY go.mod go.mod
9+
COPY go.sum go.sum
10+
# cache deps before building and copying source so that we don't need to re-download as much
11+
# and so that source changes don't invalidate our downloaded layer
12+
RUN --mount=type=bind,target=. \
13+
GOPROXY=direct go mod download
14+
15+
FROM base AS build
16+
ARG TARGETOS
17+
ARG TARGETARCH
18+
ENV VERSION_PKG=sigs.k8s.io/aws-load-balancer-controller/pkg/version
19+
RUN --mount=type=bind,target=. \
20+
--mount=type=cache,target=/root/.cache/go-build \
21+
GIT_VERSION=$(git describe --tags --dirty --always) && \
22+
GIT_COMMIT=$(git rev-parse HEAD) && \
23+
BUILD_DATE=$(date +%Y-%m-%dT%H:%M:%S%z) && \
24+
GOOS=${TARGETOS} GOARCH=${TARGETARCH} GO111MODULE=on \
25+
CGO_CPPFLAGS="-D_FORTIFY_SOURCE=2" \
26+
CGO_LDFLAGS="-Wl,-z,relro,-z,now" \
27+
go build -buildmode=pie -tags 'osusergo,netgo,static_build' -ldflags="-s -w -linkmode=external -extldflags '-static-pie' -X ${VERSION_PKG}.GitVersion=${GIT_VERSION} -X ${VERSION_PKG}.GitCommit=${GIT_COMMIT} -X ${VERSION_PKG}.BuildDate=${BUILD_DATE}" -mod=readonly -a -o /out/controller main.go
28+
29+
FROM $BASE_IMAGE as bin-unix
30+
31+
COPY --from=build /out/controller /controller
32+
ENTRYPOINT ["/controller"]
33+
34+
FROM bin-unix AS bin-linux
35+
FROM bin-unix AS bin-darwin
36+
37+
FROM bin-${TARGETOS} as bin

Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ MAKEFILE_PATH = $(dir $(realpath -s $(firstword $(MAKEFILE_LIST))))
33

44
# Image URL to use all building/pushing image targets
55
IMG ?= public.ecr.aws/eks/aws-load-balancer-controller:v2.4.6
6+
# Image URL to use for builder stage in Docker build
7+
BUILD_IMAGE ?= public.ecr.aws/docker/library/golang:1.19.3
8+
# Image URL to use for base layer in Docker build
9+
BASE_IMAGE ?= public.ecr.aws/eks-distro-build-tooling/eks-distro-minimal-base-nonroot:2023-02-22-1677092456.2
610
IMG_PLATFORM ?= linux/amd64,linux/arm64
711
# ECR doesn't appear to support SPDX SBOM
812
IMG_SBOM ?= none
@@ -91,6 +95,15 @@ aws-load-balancer-controller-push: ko
9195
BUILD_DATE=$(shell date +%Y-%m-%dT%H:%M:%S%z) \
9296
ko build --tags $(word 2,$(subst :, ,${IMG})) --platform=${IMG_PLATFORM} --bare --sbom ${IMG_SBOM} .
9397

98+
# Push the docker image using docker buildx
99+
docker-push-w-buildx:
100+
docker buildx build . --target bin \
101+
--tag $(IMG) \
102+
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
103+
--build-arg BUILD_IMAGE=$(BUILD_IMAGE) \
104+
--push \
105+
--platform ${IMG_PLATFORM}
106+
94107
# find or download controller-gen
95108
# download controller-gen if necessary
96109
controller-gen:

0 commit comments

Comments
 (0)