Skip to content

Commit bbf4fd8

Browse files
Updated base image to be from scratch instead of alpine (max-rocket-internet#23)
Added post-build check to guarantee statically linked binary Updated builder container to use golang:1.20.5 Changed CMD to ENTRYPOINT as this is a single executable container with no arguments Updated Makefile to setup a build multi-arch docker system if desired and TAG default to 1.9 Updated the docker buildx command to generate an sbom during build. Added .gitignore
1 parent 1f67f66 commit bbf4fd8

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Dependency directories (remove the comment below to include it)
18+
vendor/
19+
20+
# Go workspace file
21+
go.work

Dockerfile

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
FROM --platform=${BUILDPLATFORM} golang:1.18.3 as builder
1+
FROM --platform=${BUILDPLATFORM} golang:1.20.5 as builder
22
ARG TARGETARCH
33
ARG TARGETOS
44
WORKDIR /go/src/github.com/max-rocket-internet/k8s-event-logger
55
COPY . .
6-
RUN go get
7-
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o k8s-event-logger
8-
RUN adduser --disabled-login --no-create-home --disabled-password --system --uid 101 non-root
6+
RUN go mod vendor
7+
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o k8s-event-logger &&\
8+
if ldd 'k8s-event-logger'; then exit 1; fi; # Ensure binary is statically-linked
9+
RUN echo "k8s-event-logger:x:10001:10001::/:/bin/false" > /etc_passwd_to_copy
910

10-
FROM --platform=${TARGETPLATFORM} alpine:3.18.0
11-
RUN addgroup -S non-root && adduser -S -G non-root non-root
12-
USER 101
13-
ENV USER non-root
14-
COPY --from=builder /go/src/github.com/max-rocket-internet/k8s-event-logger/k8s-event-logger k8s-event-logger
15-
CMD ["/k8s-event-logger"]
11+
FROM --platform=${TARGETPLATFORM} scratch
12+
COPY --from=builder /etc_passwd_to_copy /go/src/github.com/max-rocket-internet/k8s-event-logger/k8s-event-logger /
13+
ENV USER=k8s-event-logger
14+
USER 10001
15+
ENTRYPOINT ["/k8s-event-logger"]

Makefile

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
IMG ?= maxrocketinternet/k8s-event-logger
2-
TAG ?= 1.6
2+
TAG ?= 1.9
33
PLATFORMS ?= linux/amd64,linux/arm64
4+
BUILDXDRIVER ?= docker-container
5+
WITHSBOM ?= true
46

5-
.PHONY: image
7+
.DEFAULT_GOAL := image
8+
9+
.PHONY: all
10+
all: binfmt buildxbuilder image
11+
12+
.PHONY: binfmt
13+
binfmt:
14+
docker run --privileged --rm tonistiigi/binfmt --install all
615

16+
.PHONY: buildxbuilder
17+
buildxbuilder:
18+
docker buildx create --name k8s-event-logger-builder --driver $(BUILDXDRIVER) --platform $(PLATFORMS) --bootstrap
19+
20+
.PHONY: image
721
image:
8-
docker buildx build --platform $(PLATFORMS) --push -t $(IMG):$(TAG) .
22+
docker buildx build --builder k8s-event-logger-builder --platform $(PLATFORMS) --sbom=$(WITHSBOM) --push -t $(IMG):$(TAG) .
23+
24+
.PHONY: clean
25+
clean:
26+
-docker rmi $(IMG):$(TAG)
27+
-docker buildx rm k8s-event-logger-builder

0 commit comments

Comments
 (0)