Skip to content

Commit 8237adc

Browse files
author
Raghavendra K
committed
Reduce image CVEs by switching to minimal base and cleanup build chain
Signed-off-by: Raghavendra K <[email protected]>
1 parent 2868b6f commit 8237adc

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

Dockerfile

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
FROM --platform=$TARGETPLATFORM gcc:latest as utils-builder
2-
3-
RUN apt-get update && apt-get install -y flex libkeyutils-dev
1+
# === Stage 1: Build DRBD utilities ===
2+
FROM --platform=$TARGETPLATFORM gcc:13 as utils-builder
3+
4+
# Install only necessary tools in minimal environment
5+
# - flex: needed for parsing in DRBD utils build
6+
# - libkeyutils-dev: required by DRBD
7+
# - curl, tar: to fetch and extract source
8+
# - make: to compile tools
9+
RUN apt-get update && apt-get install -y \
10+
flex \
11+
libkeyutils-dev \
12+
curl \
13+
tar \
14+
make \
15+
&& apt-get clean
416

517
ARG DRBD_UTILS_VERSION=9.27.0
618
RUN curl -fsSL "https://pkg.linbit.com/downloads/drbd/utils/drbd-utils-$DRBD_UTILS_VERSION.tar.gz" | tar -xzv \
@@ -9,11 +21,11 @@ RUN curl -fsSL "https://pkg.linbit.com/downloads/drbd/utils/drbd-utils-$DRBD_UTI
921
&& make tools \
1022
&& mv user/v9/drbdsetup /drbdsetup
1123

24+
# === Stage 2: Build Go binary ===
1225
FROM --platform=$BUILDPLATFORM golang:1 as go-builder
1326

1427
WORKDIR /work
1528
COPY go.mod go.sum /work/
16-
1729
RUN go mod download -x
1830

1931
COPY main.go main.go
@@ -22,12 +34,19 @@ COPY pkg/ pkg/
2234
ARG VERSION=devel
2335
ARG TARGETARCH
2436
ARG TARGETOS
25-
RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -a -ldflags "-X github.com/piraeusdatastore/drbd-shutdown-guard/pkg/vars.Version=$VERSION" -o drbd-shutdown-guard main.go
37+
RUN --mount=type=cache,target=/root/.cache/go-build \
38+
CGO_ENABLED=0 \
39+
GOOS=$TARGETOS \
40+
GOARCH=$TARGETARCH \
41+
go build -a -ldflags "-X github.com/piraeusdatastore/drbd-shutdown-guard/pkg/vars.Version=$VERSION" -o drbd-shutdown-guard main.go
2642

27-
FROM --platform=$TARGETPLATFORM registry.access.redhat.com/ubi9/ubi:latest
43+
# === Stage 3: Final minimal runtime image ===
44+
FROM registry.access.redhat.com/ubi9/ubi-minimal:latest
2845

46+
# Copy only the built binaries from previous stages
2947
COPY --from=utils-builder /drbdsetup /usr/local/sbin/drbdsetup
3048
COPY --from=go-builder /work/drbd-shutdown-guard /usr/local/sbin/drbd-shutdown-guard
3149

50+
# Define the binary to run
3251
ENV DRBDSETUP_LOCATION=/usr/local/sbin/drbdsetup
3352
CMD ["/usr/local/sbin/drbd-shutdown-guard", "install"]

0 commit comments

Comments
 (0)