为了账号安全,请及时绑定邮箱和手机立即绑定

如何在支持 golang 的 gRPC 中减小 docker 镜像的大小?

如何在支持 golang 的 gRPC 中减小 docker 镜像的大小?

Go
开满天机 2023-06-05 19:48:19
我有一些使用 gRPC/golang 进行通信的服务器和客户端。现在我想将我的应用程序容器化,但是包含 goland 执行和 grpc 支持的 docker 镜像的大小更大(超过 1GB)。我想减小 docker 图像的大小。所需的 golang 版本为 1.9 及更高版本。这是给出的 Dockerfile 脚本。如果有其他方法请建议。FROM golang:1.11RUN apt-get update && \    apt-get -y install git unzip build-essential autoconf libtoolRUN git clone https://github.com/google/protobuf.git && \    cd protobuf && \    ./autogen.sh && \    ./configure && \    make && \    make install && \    ldconfig && \    make clean && \    cd .. && \    rm -r protobufRUN go get google.golang.org/grpcRUN go get github.com/golang/protobuf/protoc-gen-goRUN ls -laWORKDIR /helloworldCOPY . /helloworldRUN protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworldCMD ["go", "run", "helloworld/greeter_server/main.go"]
查看完整描述

2 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

尝试制作这样的多级 docker 镜像


# Compile stage

FROM golang:1.11 as build-env


RUN apt-get update && \

    apt-get -y install git unzip build-essential autoconf libtool


RUN git clone https://github.com/google/protobuf.git && \

    cd protobuf && \

    ./autogen.sh && \

    ./configure && \

    make && \

    make install && \

    ldconfig && \

    make clean && \

    cd .. && \

    rm -r protobuf


RUN go get google.golang.org/grpc


RUN go get github.com/golang/protobuf/protoc-gen-go


RUN ls -la


WORKDIR /helloworld


COPY . /helloworld


RUN protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld

RUN go build -o server helloworld/greeter_server/main.go


# Making image

FROM alpine:3.8 AS host

RUN apk add --no-cache \

        ca-certificates

COPY --from=build-env /helloworld/server /

# copy any other files you need


WORKDIR /

EXPOSE 8000

CMD ["server"]


查看完整回答
反对 回复 2023-06-05
?
翻阅古今

TA贡献1780条经验 获得超5个赞

您可以尝试使用distroless基础镜像和多阶段构建。那可能对你有帮助。



查看完整回答
反对 回复 2023-06-05
  • 2 回答
  • 0 关注
  • 98 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信