From f6d5f4f0d4730e321c4478a3483c626aaa1615ea Mon Sep 17 00:00:00 2001 From: talksik Date: Wed, 29 Apr 2026 14:51:04 -0700 Subject: [PATCH] init --- README.md | 42 ++++++++++++++++++++++++++++++++++++++ build_and_push_docker.sh | 20 ++++++++++++++++++ golang-protoc.Dockerfile | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 README.md create mode 100755 build_and_push_docker.sh create mode 100644 golang-protoc.Dockerfile diff --git a/README.md b/README.md new file mode 100644 index 0000000..07c3b5a --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ + +## Example usage +I use a script, which by default, assumes that your protobuf files are in relative path `./protocol`, and will generate a folder for outputs in `./genproto`: +```sh +#!/bin/bash -eu + +PATH=$PATH:$(go env GOPATH)/bin + +# Default values +protodir="./protocol" +outdir="./genproto" + +# Parse command-line options +while getopts "p:o:" opt; do + case $opt in + p) protodir="$OPTARG" ;; + o) outdir="$OPTARG" ;; + \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;; + esac +done + +# Print the directories for verification +echo "Proto directory: $protodir" +echo "Output directory: $outdir" + +rm -rf $outdir +mkdir -p $outdir + +# use the public image from dockerhub which contains tools for protoc & golang/grpc +docker run --rm \ + -v "$protodir/":/protocol \ + -v "$outdir":/genproto \ + --workdir / \ + talksik/golang-protoc:latest \ + protoc --proto_path=./protocol \ + --go_out=./genproto \ + --go_opt=paths=source_relative \ + --go-grpc_out=./genproto \ + --go-grpc_opt=paths=source_relative \ + $(find ./protocol -name "*.proto") +``` + diff --git a/build_and_push_docker.sh b/build_and_push_docker.sh new file mode 100755 index 0000000..91e2c29 --- /dev/null +++ b/build_and_push_docker.sh @@ -0,0 +1,20 @@ +#!/bin/bash -eu + +echo "The following script will build the talksik/golang-protoc image and push to docker both architectures: amd64 & arm64" + +read -p "Press 'y' to confirm: " -n 1 confirm + +DOCKER_HUB_REPOSITORY=talksik/golang-protoc:latest + +if [[ "$confirm" == "y" ]]; then + docker buildx build \ + --platform linux/amd64,linux/arm64 \ + --build-arg PLATFORM=$(echo $BUILDPLATFORM | cut -d '/' -f 2) \ + --push \ + -f ./golang-protoc.Dockerfile \ + -t $DOCKER_HUB_REPOSITORY . \ + --progress=plain +else + echo "Confirmation canceled." +fi + diff --git a/golang-protoc.Dockerfile b/golang-protoc.Dockerfile new file mode 100644 index 0000000..e59d3ac --- /dev/null +++ b/golang-protoc.Dockerfile @@ -0,0 +1,44 @@ +# first stage to build the protobuf files and go project +FROM golang:1.23-alpine AS builder +ARG PLATFORM +ARG PROTOC_VERSION="28.3" + +RUN apk add --no-cache \ + wget \ + unzip + +# ----------------- +# set up container for building protobuf + +# By default Intel chipset (x86_64) is assumed but if the host device is an Apple +# silicon (arm) chipset based then a relevant (aarch_64) release file is used. + +WORKDIR /workdir + +RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.35.1 && \ + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.5.1 + +RUN export GOPATH=$(go env GOPATH) && \ + echo "GOPATH is: $GOPATH" + +RUN set -ex; \ + export ZIP=x86_64 && \ + if [ ${PLATFORM} = "arm64" ]; then export ZIP=aarch_64; fi && \ + if [ ${PLATFORM} = "amd64" ]; then export ZIP=x86_64; fi && \ + wget --quiet https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-${ZIP}.zip && \ + unzip -o protoc-${PROTOC_VERSION}-linux-${ZIP}.zip -d /usr/local bin/protoc && \ + unzip -o protoc-${PROTOC_VERSION}-linux-${ZIP}.zip -d /usr/local 'include/*' && \ + rm protoc-${PROTOC_VERSION}-linux-${ZIP}.zip + +RUN protoc --version + +RUN which protoc-gen-go && \ + which protoc-gen-go-grpc + +FROM alpine:latest +COPY --from=builder /usr/local/bin/protoc /usr/local/bin/ +COPY --from=builder /usr/local/include /usr/local/include +COPY --from=builder /go/bin/protoc-gen-go /usr/local/bin/ +COPY --from=builder /go/bin/protoc-gen-go-grpc /usr/local/bin/ + +RUN protoc --version