ZhangZhihui's Blog  

 

FROM --platform=$BUILDPLATFORM alpine as protoc
ARG BUILDPLATFORM=linux/amd64 TARGETOS=linux TARGETARCH=amd64

# download the protoc binary from github
# We unzip the file into /usr/local. Notice that we are extracting both the protoc
# binary (/bin/protoc) and the /include folder because the first one is the compiler that we are
# going to use and the second one is all the files needed to include Well-Known Types.
RUN export PROTOC_VERSION=26.1 \
    && export PROTOC_ARCH=$(uname -m | sed s/aarch64/aarch_64/) \
    && export PROTOC_OS=$(echo $TARGETOS | sed s/darwin/linux/) \
    && export PROTOC_ZIP=protoc-$PROTOC_VERSION-$PROTOC_OS-$PROTOC_ARCH.zip \
    && echo "downloading: " https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOC_VERSION/$PROTOC_ZIP \
    && wget https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOC_VERSION/$PROTOC_ZIP \
    && unzip -o $PROTOC_ZIP -d /usr/local bin/protoc 'include/*' \
    && rm -f $PROTOC_ZIP

FROM --platform=$BUILDPLATFORM golang:1.22-alpine as build
ARG BUILDPLATFORM=linux/amd64 TARGETOS=linux TARGETARCH=amd64

# copy the protoc binary and the protobuf includes
COPY --from=protoc /usr/local/bin/protoc /usr/local/bin/protoc
COPY --from=protoc /usr/local/include/google /usr/local/include/google

# download protoc plugins
RUN go env -w GOPROXY=https://goproxy.io,direct
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# copy proto files into /go/src/proto
WORKDIR /go/src/proto
COPY ./proto .

# generate code out of proto files
WORKDIR /go
ENV MODULE=zgrpc-go-professionals
RUN protoc --go_out=src \
           --go_opt=module=$MODULE \
           --go-grpc_out=src \
           --go-grpc_opt=module=$MODULE \
           src/proto/todo/v2/*.proto

# copy code into /go/src/server
WORKDIR /go/src/server
COPY ./server .

# copy go.mod into go/src
WORKDIR /go/src
COPY go.mod .

# download dependencies and build
WORKDIR /go/src
RUN go mod tidy
WORKDIR /go/src/server
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -ldflags="-s -w" -o /go/bin/app

FROM scratch

# copy certs into /certs
COPY ./certs/server_cert.pem ./certs/server_cert.pem
COPY ./certs/server_key.pem ./certs/server_key.pem

# copy the previously built binary into smaller image
COPY --from=build /go/bin/app /
EXPOSE 50051
CMD ["/app", "0.0.0.0:50051"]

 

The image is built successfully.

It runs well too:

zzh@ZZHPC:/zdata/Github/zgrpc-go-professionals$ docker run --name zgrpc -p 50051:50051 -d zgrpc-go-professionals:server
79c7dcf72d8addd76c30c92fe72910cad0920eaa809513616c502879f74fab19
zzh@ZZHPC:/zdata/Github/zgrpc-go-professionals$ docker ps 
CONTAINER ID   IMAGE                           COMMAND                  CREATED          STATUS          PORTS                                           NAMES
79c7dcf72d8a   zgrpc-go-professionals:server   "/app 0.0.0.0:50051"     14 seconds ago   Up 13 seconds   0.0.0.0:50051->50051/tcp, :::50051->50051/tcp   zgrpc


zzh@ZZHPC:/zdata/Github/zgrpc-go-professionals$ make runclient
go run ./client 0.0.0.0:50051
--------ADD--------
added task: 1
added task: 2
added task: 3
-------------------
--------LIST-------
id:1 description:"This is a task" due_date:{seconds:1714960336 nanos:826536718} overdue:  false
id:2 description:"This is another task" due_date:{seconds:1714960336 nanos:826536718} overdue:  false
id:3 description:"And yet another task" due_date:{seconds:1714960336 nanos:826536718} overdue:  false
-------------------
-------UPDATE------
updated task with id: 1
updated task with id: 2
updated task with id: 3
id:1 description:"A better name for the task" due_date:{} overdue:  true
id:2 due_date:{seconds:1714978336 nanos:826536718} overdue:  false
id:3 done:true due_date:{} overdue:  false
-------------------
-------DELETE------
2024/05/06 09:52:11 task deleted
2024/05/06 09:52:11 task deleted
2024/05/06 09:52:11 task deleted
-------------------
-------ERROR-------
-------------------

 

posted on 2024-05-05 14:34  ZhangZhihuiAAA  阅读(6)  评论(0编辑  收藏  举报