Skip to content

Commit bde7c98

Browse files
feat(codegen): implement multi-stage Docker build for security hardening and reduced image size
Signed-off-by: Mohammed Firdous <124298708+mohammedfirdouss@users.noreply.github.com>
1 parent e103623 commit bde7c98

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

tool/codegen/Dockerfile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Stage 1: Builder - Build Go-based plugins
2+
23
FROM golang:1.25.2 AS builder
34

45
# Version configuration
@@ -33,6 +34,7 @@ RUN wget -q https://github.com/envoyproxy/protoc-gen-validate/archive/refs/tags/
3334
&& rm /tmp/validate.tar.gz
3435

3536
# Stage 2: Downloader - Download pre-built binaries
37+
3638
FROM debian:bookworm-slim AS downloader
3739

3840
RUN apt-get update && apt-get install -y --no-install-recommends wget ca-certificates \
@@ -46,6 +48,7 @@ RUN wget -q https://github.com/grpc/grpc-web/releases/download/${PROTOC_GEN_GRPC
4648

4749
# Download protoc-gen-js for both architectures
4850
# This is a workaround: https://github.com/protocolbuffers/protobuf-javascript/issues/127#issuecomment-1361047597
51+
4952
ARG PROTOC_GEN_JS_VER=3.21.2
5053
RUN for target in x86_64 aarch_64; do \
5154
mkdir -p /protoc-gen-js-${target} \
@@ -56,3 +59,44 @@ RUN for target in x86_64 aarch_64; do \
5659
done \
5760
&& mv /protoc-gen-js-aarch_64 /protoc-gen-js-aarch64
5861

62+
63+
# Stage 3: Final - Minimal runtime image
64+
FROM debian:bookworm-slim AS final
65+
66+
# Install protoc, proto files, and ca-certificates
67+
# libprotobuf-dev includes standard proto files in /usr/include/google/protobuf/
68+
RUN apt-get update \
69+
&& apt-get install -y --no-install-recommends \
70+
protobuf-compiler \
71+
libprotobuf-dev \
72+
ca-certificates \
73+
git \
74+
&& rm -rf /var/lib/apt/lists/*
75+
76+
# Copy Go runtime from golang image
77+
# mockgen uses 'go list' and 'go build' internally
78+
COPY --from=golang:1.25.2 /usr/local/go /usr/local/go
79+
ENV GOROOT=/usr/local/go
80+
ENV GOPATH=/go
81+
ENV PATH=$GOPATH/bin:$GOROOT/bin:$PATH
82+
83+
# Create necessary directories
84+
RUN mkdir -p /go/bin /go/src/github.com/envoyproxy
85+
86+
# Copy built Go plugins from builder
87+
COPY --from=builder /out/protoc-gen-go /go/bin/
88+
COPY --from=builder /out/protoc-gen-go-grpc /go/bin/
89+
COPY --from=builder /out/protoc-gen-validate /go/bin/
90+
COPY --from=builder /out/mockgen /go/bin/
91+
COPY --from=builder /out/protoc-gen-auth /usr/local/bin/
92+
93+
# Copy validate proto files (needed for -I include path in codegen.sh)
94+
COPY --from=builder /out/validate-protos /go/src/github.com/envoyproxy/protoc-gen-validate
95+
96+
# Copy downloaded binaries from downloader
97+
COPY --from=downloader /protoc-gen-grpc-web /usr/local/bin/
98+
COPY --from=downloader /protoc-gen-js-x86_64 /protoc-gen-js-x86_64
99+
COPY --from=downloader /protoc-gen-js-aarch64 /protoc-gen-js-aarch64
100+
101+
VOLUME /repo
102+
WORKDIR /repo

0 commit comments

Comments
 (0)