| 1 |
# Copyright 2025 Google LLC |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
# This Dockerfile creates a MOSS-compliant image for the Go librariangen, |
| 16 |
# which is invoked by the Librarian tool. It uses a multi-stage build to |
| 17 |
# create a minimal final image. |
| 18 |
|
| 19 |
# --- Builder Stage --- |
| 20 |
# This stage builds the librariangen binary using the MOSS-compliant base image. |
| 21 |
FROM marketplace.gcr.io/google/debian12:latest AS builder |
| 22 |
|
| 23 |
# Set environment variables for tool versions for easy updates. |
| 24 |
ENV GO_VERSION=1.24.0 |
| 25 |
|
| 26 |
# Install build dependencies. |
| 27 |
RUN apt-get update && \ |
| 28 |
apt-get install -y \ |
| 29 |
build-essential \ |
| 30 |
ca-certificates \ |
| 31 |
curl \ |
| 32 |
wget && \ |
| 33 |
rm -rf /var/lib/apt/lists/* |
| 34 |
|
| 35 |
# Install the specific Go version required for compatibility. |
| 36 |
RUN wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz -O go.tar.gz && \ |
| 37 |
tar -C /usr/local -xzf go.tar.gz && \ |
| 38 |
rm go.tar.gz |
| 39 |
ENV PATH=/usr/local/go/bin:$PATH |
| 40 |
RUN go version |
| 41 |
|
| 42 |
WORKDIR /src |
| 43 |
|
| 44 |
# Copy go.mod and go.sum to download dependencies before copying all the source. |
| 45 |
# This allows Docker to cache the dependencies layer. |
| 46 |
COPY go.mod go.sum ./ |
| 47 |
RUN go mod download |
| 48 |
|
| 49 |
# Copy all source code. |
| 50 |
COPY . . |
| 51 |
|
| 52 |
# Build the librariangen binary. |
| 53 |
RUN CGO_ENABLED=0 GOOS=linux go build -v -o /librariangen . |
| 54 |
|
| 55 |
|
| 56 |
# --- Final Stage --- |
| 57 |
# This stage creates the final, minimal image with the compiled binary and |
| 58 |
# all required runtime dependencies pinned to specific versions for compatibility. |
| 59 |
FROM marketplace.gcr.io/google/debian12:latest |
| 60 |
|
| 61 |
# Set environment variables for tool versions for easy updates. |
| 62 |
ENV GO_VERSION=1.24.0 |
| 63 |
ENV PROTOC_VERSION=25.7 |
| 64 |
ENV GO_PROTOC_PLUGIN_V1_VERSION=1.5.4 |
| 65 |
ENV GO_PROTOC_PLUGIN_V2_VERSION=1.35.2 |
| 66 |
ENV GO_GRPC_PLUGIN_VERSION=1.3.0 |
| 67 |
ENV GAPIC_GENERATOR_VERSION=0.53.1 |
| 68 |
ENV STATICCHECK_VERSION=2023.1.6 |
| 69 |
|
| 70 |
# Set up a home directory for the non-root user that will be running this container. |
| 71 |
# Go tools will use this directory for caches, which resolves permission issues. |
| 72 |
ENV HOME=/home/nonroot |
| 73 |
# GOPATH defaults to $HOME/go, so we need to add that to the PATH. |
| 74 |
ENV PATH=$HOME/go/bin:/usr/local/go/bin:/usr/local/bin:$PATH |
| 75 |
|
| 76 |
# Install essential system packages. |
| 77 |
# Clean up apt cache to keep the image smaller. |
| 78 |
RUN apt-get update && \ |
| 79 |
apt-get install -y \ |
| 80 |
ca-certificates \ |
| 81 |
curl \ |
| 82 |
git \ |
| 83 |
unzip \ |
| 84 |
wget && \ |
| 85 |
rm -rf /var/lib/apt/lists/* |
| 86 |
|
| 87 |
# Install the specific Go version required for compatibility. |
| 88 |
|
| 89 |
# Note: This is needed for the go install commands below. |
| 90 |
RUN wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz -O go.tar.gz && \ |
| 91 |
tar -C /usr/local -xzf go.tar.gz && \ |
| 92 |
rm go.tar.gz |
| 93 |
|
| 94 |
# Install the specific protoc (Protobuf Compiler) version required for compatibility. |
| 95 |
RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-x86_64.zip -O protoc.zip && \ |
| 96 |
unzip protoc.zip -d /usr/local && \ |
| 97 |
rm protoc.zip && \ |
| 98 |
protoc --version |
| 99 |
|
| 100 |
# Build the "old" Go protoc plugin manually; using go install just redirects to the new one. |
| 101 |
# Note that the "old" plugin uses the internal code in the "new" plugin, so we need to use |
| 102 |
# "go mod edit" to end up with a consistent version. |
| 103 |
# The plugin is built as protoc-gen-go_v1, so that we can use "go_v1" as the name in protoc options. |
| 104 |
# This is ghastly, and should all be removed as soon as possible by migrating all packages to go_grpc. |
| 105 |
RUN (export GOTOOLCHAIN='auto' && \ |
| 106 |
git clone --depth 1 --branch=v${GO_PROTOC_PLUGIN_V1_VERSION} https://github.com/golang/protobuf protoc-gen-go-v1 && \ |
| 107 |
cd protoc-gen-go-v1 && \ |
| 108 |
go mod edit -require google.golang.org/protobuf@v${GO_PROTOC_PLUGIN_V2_VERSION} && \ |
| 109 |
go mod tidy && \ |
| 110 |
go build -o $HOME/go/bin/protoc-gen-go_v1 ./protoc-gen-go) |
| 111 |
|
| 112 |
# Install required Go tools for protoc and the post-processor. |
| 113 |
RUN (export GOTOOLCHAIN='auto' && \ |
| 114 |
go install google.golang.org/protobuf/cmd/protoc-gen-go@v${GO_PROTOC_PLUGIN_V2_VERSION} && \ |
| 115 |
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v${GO_GRPC_PLUGIN_VERSION} && \ |
| 116 |
go install github.com/googleapis/gapic-generator-go/cmd/protoc-gen-go_gapic@v${GAPIC_GENERATOR_VERSION} && \ |
| 117 |
go install golang.org/x/tools/cmd/goimports@latest && \ |
| 118 |
go install honnef.co/go/tools/cmd/staticcheck@${STATICCHECK_VERSION}) |
| 119 |
|
| 120 |
# Create and set permissions for the home directory so it is writable by any user. |
| 121 |
RUN mkdir -p ${HOME} && chmod -R 777 ${HOME} |
| 122 |
|
| 123 |
# Copy the compiled librariangen binary from the builder stage. |
| 124 |
COPY --from=builder /librariangen /usr/local/bin/librariangen |
| 125 |
|
| 126 |
# Set the entrypoint for the container to run the compiled librariangen. |
| 127 |
# The Librarian will provide commands like 'generate' as arguments. |
| 128 |
ENTRYPOINT [ "/usr/local/bin/librariangen" ] |