forked from eraser-dev/eraser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dockerfile
76 lines (63 loc) · 2.61 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# syntax=docker/dockerfile:1.6
ARG BUILDERIMAGE="golang:1.20-bullseye"
ARG TRIVY_BINARY_IMG="ghcr.io/aquasecurity/trivy:0.45.1"
ARG STATICBASEIMAGE="gcr.io/distroless/static:latest"
ARG STATICNONROOTBASEIMAGE="gcr.io/distroless/static:nonroot"
ARG BUILDKIT_SBOM_SCAN_STAGE=builder,manager-build,collector-build,remover-build,trivy-scanner-build
FROM --platform=$TARGETPLATFORM $TRIVY_BINARY_IMG AS trivy-binary
# Build the manager binary
FROM --platform=$BUILDPLATFORM $BUILDERIMAGE AS builder
WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum 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
ENV GOCACHE=/root/gocache
ENV CGO_ENABLED=0
RUN \
--mount=type=cache,target=${GOCACHE} \
--mount=type=cache,target=/go/pkg/mod \
go mod download
COPY . .
ARG LDFLAGS
ARG TARGETOS
ARG TARGETARCH
FROM builder AS manager-build
RUN \
--mount=type=cache,target=${GOCACHE} \
--mount=type=cache,target=/go/pkg/mod \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build ${LDFLAGS:+-ldflags "$LDFLAGS"} -o out/manager main.go
FROM builder AS collector-build
RUN \
--mount=type=cache,target=${GOCACHE} \
--mount=type=cache,target=/go/pkg/mod \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build ${LDFLAGS:+-ldflags "$LDFLAGS"} -o out/collector ./pkg/collector
FROM builder AS remover-build
RUN \
--mount=type=cache,target=${GOCACHE} \
--mount=type=cache,target=/go/pkg/mod \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build ${LDFLAGS:+-ldflags "$LDFLAGS"} -o out/remover ./pkg/remover
FROM builder AS trivy-scanner-build
RUN \
--mount=type=cache,target=${GOCACHE} \
--mount=type=cache,target=/go/pkg/mod \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build ${LDFLAGS:+-ldflags "$LDFLAGS"} -o out/trivy-scanner ./pkg/scanners/trivy
FROM --platform=$TARGETPLATFORM $STATICNONROOTBASEIMAGE AS manager
WORKDIR /
COPY --from=manager-build /workspace/out/manager .
USER 65532:65532
ENTRYPOINT ["/manager"]
FROM --platform=$TARGETPLATFORM $STATICBASEIMAGE as collector
COPY --from=collector-build /workspace/out/collector /
ENTRYPOINT ["/collector"]
FROM --platform=$TARGETPLATFORM $STATICBASEIMAGE as remover
COPY --from=remover-build /workspace/out/remover /
ENTRYPOINT ["/remover"]
FROM --platform=$TARGETPLATFORM $STATICBASEIMAGE as trivy-scanner
COPY --from=trivy-scanner-build /workspace/out/trivy-scanner /
COPY --from=trivy-binary /usr/local/bin/trivy /
WORKDIR /var/lib/trivy
ENTRYPOINT ["/trivy-scanner"]
FROM $STATICNONROOTBASEIMAGE as non-vulnerable
COPY --from=builder /tmp /tmp