Skip to content

Commit

Permalink
Reduce image layers in protoc v25.3 plugins (#1287)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkwarren authored Jun 18, 2024
1 parent fb47d5c commit 5a21262
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 29 deletions.
1 change: 1 addition & 0 deletions plugins/protocolbuffers/cpp/v25.3/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*
!BUILD
!cpp.cc
!disable_cpp_editions.patch
!Dockerfile
13 changes: 9 additions & 4 deletions plugins/protocolbuffers/cpp/v25.3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax=docker/dockerfile:1.5
# syntax=docker/dockerfile:1.7
FROM debian:bookworm-20240211 AS build

ARG TARGETARCH
Expand All @@ -16,9 +16,14 @@ RUN curl -fsSL -o protoc.tar.gz https://github.com/protocolbuffers/protobuf/rele
&& rm protoc.tar.gz
RUN bazel build '//:protoc_lib'
COPY --link BUILD cpp.cc plugins/
RUN bazel build '//plugins:protoc-gen-cpp.stripped'
COPY --link disable_cpp_editions.patch .
RUN git apply < disable_cpp_editions.patch \
&& bazel build '//plugins:protoc-gen-cpp.stripped'

FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608
COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-cpp .
FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base

FROM scratch
COPY --link --from=base / /
COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-cpp .
USER nobody
ENTRYPOINT ["/protoc-gen-cpp"]
20 changes: 20 additions & 0 deletions plugins/protocolbuffers/cpp/v25.3/disable_cpp_editions.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
diff --git a/src/google/protobuf/compiler/cpp/generator.h b/src/google/protobuf/compiler/cpp/generator.h
index 64f334d5b..fd6e08e84 100644
--- a/src/google/protobuf/compiler/cpp/generator.h
+++ b/src/google/protobuf/compiler/cpp/generator.h
@@ -70,14 +70,7 @@ class PROTOC_EXPORT CppGenerator : public CodeGenerator {
std::string* error) const override;

uint64_t GetSupportedFeatures() const override {
- return FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS;
- }
-
- Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; }
- Edition GetMaximumEdition() const override { return Edition::EDITION_2023; }
-
- std::vector<const FieldDescriptor*> GetFeatureExtensions() const override {
- return {GetExtensionReflection(pb::cpp)};
+ return FEATURE_PROTO3_OPTIONAL;
}

private:
9 changes: 6 additions & 3 deletions plugins/protocolbuffers/csharp/v25.3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax=docker/dockerfile:1.5
# syntax=docker/dockerfile:1.7
FROM debian:bookworm-20240211 AS build

ARG TARGETARCH
Expand All @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib'
COPY --link BUILD csharp.cc plugins/
RUN bazel build '//plugins:protoc-gen-csharp.stripped'

FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608
COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-csharp .
FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base

FROM scratch
COPY --link --from=base / /
COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-csharp .
USER nobody
ENTRYPOINT ["/protoc-gen-csharp"]
9 changes: 6 additions & 3 deletions plugins/protocolbuffers/java/v25.3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax=docker/dockerfile:1.5
# syntax=docker/dockerfile:1.7
FROM debian:bookworm-20240211 AS build

ARG TARGETARCH
Expand All @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib'
COPY --link BUILD java.cc plugins/
RUN bazel build '//plugins:protoc-gen-java.stripped'

FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608
COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-java .
FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base

FROM scratch
COPY --link --from=base / /
COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-java .
USER nobody
ENTRYPOINT ["/protoc-gen-java"]
9 changes: 6 additions & 3 deletions plugins/protocolbuffers/kotlin/v25.3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax=docker/dockerfile:1.5
# syntax=docker/dockerfile:1.7
FROM debian:bookworm-20240211 AS build

ARG TARGETARCH
Expand All @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib'
COPY --link BUILD kotlin.cc plugins/
RUN bazel build '//plugins:protoc-gen-kotlin.stripped'

FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608
COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-kotlin .
FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base

FROM scratch
COPY --link --from=base / /
COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-kotlin .
USER nobody
ENTRYPOINT ["/protoc-gen-kotlin"]
1 change: 1 addition & 0 deletions plugins/protocolbuffers/objc/v25.3/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*
!BUILD
!disable_objc_editions.patch
!Dockerfile
!objectivec.cc
13 changes: 9 additions & 4 deletions plugins/protocolbuffers/objc/v25.3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax=docker/dockerfile:1.5
# syntax=docker/dockerfile:1.7
FROM debian:bookworm-20240211 AS build

ARG TARGETARCH
Expand All @@ -16,9 +16,14 @@ RUN curl -fsSL -o protoc.tar.gz https://github.com/protocolbuffers/protobuf/rele
&& rm protoc.tar.gz
RUN bazel build '//:protoc_lib'
COPY --link BUILD objectivec.cc plugins/
RUN bazel build '//plugins:protoc-gen-objectivec.stripped'
COPY --link BUILD disable_objc_editions.patch .
RUN git apply < disable_objc_editions.patch \
&& bazel build '//plugins:protoc-gen-objectivec.stripped'

FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608
COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-objectivec .
FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base

FROM scratch
COPY --link --from=base / /
COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-objectivec .
USER nobody
ENTRYPOINT ["/protoc-gen-objectivec"]
16 changes: 16 additions & 0 deletions plugins/protocolbuffers/objc/v25.3/disable_objc_editions.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
diff --git a/src/google/protobuf/compiler/objectivec/generator.h b/src/google/protobuf/compiler/objectivec/generator.h
index be5a6a448..4da2dba0d 100644
--- a/src/google/protobuf/compiler/objectivec/generator.h
+++ b/src/google/protobuf/compiler/objectivec/generator.h
@@ -47,10 +47,8 @@ class PROTOC_EXPORT ObjectiveCGenerator : public CodeGenerator {
std::string* error) const override;

uint64_t GetSupportedFeatures() const override {
- return (FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS);
+ return FEATURE_PROTO3_OPTIONAL;
}
- Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; }
- Edition GetMaximumEdition() const override { return Edition::EDITION_2023; }
};

} // namespace objectivec
9 changes: 6 additions & 3 deletions plugins/protocolbuffers/php/v25.3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax=docker/dockerfile:1.5
# syntax=docker/dockerfile:1.7
FROM debian:bookworm-20240211 AS build

ARG TARGETARCH
Expand All @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib'
COPY --link BUILD php.cc plugins/
RUN bazel build '//plugins:protoc-gen-php.stripped'

FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608
COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-php .
FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base

FROM scratch
COPY --link --from=base / /
COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-php .
USER nobody
ENTRYPOINT ["/protoc-gen-php"]
7 changes: 5 additions & 2 deletions plugins/protocolbuffers/pyi/v25.3/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib'
COPY BUILD pyi.cc plugins/
RUN bazel build '//plugins:protoc-gen-pyi.stripped'

FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608
COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-pyi .
FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base

FROM scratch
COPY --link --from=base / /
COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-pyi .
USER nobody
ENTRYPOINT ["/protoc-gen-pyi"]
2 changes: 1 addition & 1 deletion plugins/protocolbuffers/pyi/v25.3/buf.plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ registry:
requires_python: ">=3.8"
deps:
# https://pypi.org/project/protobuf/
- "protobuf"
- "protobuf~=4.25"
- "types-protobuf"
9 changes: 6 additions & 3 deletions plugins/protocolbuffers/python/v25.3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax=docker/dockerfile:1.5
# syntax=docker/dockerfile:1.7
FROM debian:bookworm-20240211 AS build

ARG TARGETARCH
Expand All @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib'
COPY --link BUILD python.cc plugins/
RUN bazel build '//plugins:protoc-gen-python.stripped'

FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608
COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-python .
FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base

FROM scratch
COPY --link --from=base / /
COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-python .
USER nobody
ENTRYPOINT ["/protoc-gen-python"]
9 changes: 6 additions & 3 deletions plugins/protocolbuffers/ruby/v25.3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# syntax=docker/dockerfile:1.5
# syntax=docker/dockerfile:1.7
FROM debian:bookworm-20240211 AS build

ARG TARGETARCH
Expand All @@ -18,7 +18,10 @@ RUN bazel build '//:protoc_lib'
COPY --link BUILD ruby.cc plugins/
RUN bazel build '//plugins:protoc-gen-ruby.stripped'

FROM gcr.io/distroless/cc-debian12:latest@sha256:899570acf85a1f1362862a9ea4d9e7b1827cb5c62043ba5b170b21de89618608
COPY --from=build --link --chmod=0755 /build/bazel-bin/plugins/protoc-gen-ruby .
FROM gcr.io/distroless/cc-debian12:latest@sha256:e1065a1d58800a7294f74e67c32ec4146d09d6cbe471c1fa7ed456b2d2bf06e0 AS base

FROM scratch
COPY --link --from=base / /
COPY --link --from=build --chmod=0755 /build/bazel-bin/plugins/protoc-gen-ruby .
USER nobody
ENTRYPOINT ["/protoc-gen-ruby"]

0 comments on commit 5a21262

Please sign in to comment.