英文:
How to use docker to generate grpc code based on go.mod versions?
问题
使用官方的golang Docker镜像,我可以使用protoc命令生成x.pb.go和x_grpc.pb.go文件。问题是它使用的是最新版本,而我想使用go.mod文件中的版本来生成这些文件。
我尝试从golang镜像开始,然后获取我的项目的go.mod文件,获取依赖项并尝试从那里生成。这是我的Dockerfile:
FROM golang:1.15
WORKDIR /app
RUN apt-get update
RUN apt install -y protobuf-compiler
COPY go.* .
RUN go mod download
RUN go get all
RUN export PATH="$PATH:$(go env GOPATH)/bin"
RUN mkdir /api
然后,我尝试将.proto文件和/pb文件夹的卷绑定以输出它们,并再次使用protoc命令(我现在正在尝试直接从Docker中运行)。类似这样:
protoc --proto_path=/api --go_out=/pb --go-grpc_out=/pb /api/x.proto
然而,我遇到了以下错误:
protoc-gen-go: program not found or is not executable
--go_out: protoc-gen-go: Plugin failed with status code 1.
我的go.sum文件中有google.golang.org/protobuf v1.25.0,为什么找不到它呢?
英文:
Using the official golang docker image, I can use the protoc command to generate the x.pb.go and x_grpc.pb.go files. The problem is that it uses the latest versions, while I want to generate those using whichever version that is part of the go.mod file.
I tried to start from the golang image, then get my project's go.mod file, get the dependencies and try to generate from there. Here is my dockerfile:
FROM golang:1.15
WORKDIR /app
RUN apt-get update
RUN apt install -y protobuf-compiler
COPY go.* .
RUN go mod download
RUN go get all
RUN export PATH="$PATH:$(go env GOPATH)/bin"
RUN mkdir /api
Then I try to bind the volume of the .proto file and the /pb folder to output them, and use the protoc command again (I'm trying directly from the docker right now). Something like this:
protoc --proto_path=/api --go_out=/pb --go-grpc_out=/pb /api/x.proto
I'm getting this error though:
protoc-gen-go: program not found or is not executable
--go_out: protoc-gen-go: Plugin failed with status code 1.
My go.sum file has google.golang.org/protobuf v1.25.0 in it, so how come it is not found?
答案1
得分: 2
go.mod和go.sum用于在构建go程序时进行版本控制。这不是你在这里所需要的。你希望protoc编译器在对.proto文件进行编译时使用正确的插件版本。
要安装所需的protoc-gen-go(如果使用gRPC还要安装protoc-gen-go-grpc)插件,请直接安装它们。将你的Dockerfile更新如下:
FROM golang:1.15
WORKDIR /app
RUN apt-get update
RUN apt install -y protobuf-compiler
RUN GO111MODULE=on \
go get google.golang.org/protobuf/cmd/protoc-gen-go@v1.25.0 \
google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
# export is redundant here `/go/bin` is already in `golang` image's path
# (and actual any env change here is lost once the command completes)
# RUN export PATH="$PATH:$(go env GOPATH)/bin"
RUN mkdir /api
如果你想要最新版本的任一插件,可以使用@latest,或者省略@后缀。
英文:
go.mod & go.sum are used for versioning when building go programs. This is not what you need here. You want the protoc compiler to use the correct plugin versions when running it against your .proto file(s).
To install the desired protoc-gen-go (and protoc-gen-go-grpc if using gRPC) plugins, install them directly. Update your Dockerfile like so:
FROM golang:1.15
WORKDIR /app
RUN apt-get update
RUN apt install -y protobuf-compiler
RUN GO111MODULE=on \
go get google.golang.org/protobuf/cmd/protoc-gen-go@v1.25.0 \
google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
# export is redundant here `/go/bin` is already in `golang` image's path
# (and actual any env change here is lost once the command completes)
# RUN export PATH="$PATH:$(go env GOPATH)/bin"
RUN mkdir /api
If you want the latest versions of either plugin, either use @latest - or drop the @ suffix
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论