This commit is contained in:
talksik
2026-04-29 14:51:04 -07:00
commit f6d5f4f0d4
3 changed files with 106 additions and 0 deletions
+42
View File
@@ -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")
```
+20
View File
@@ -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
+44
View File
@@ -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