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

我有一些使用 gRPC/golang 进行通信的服务器和客户端。现在我想将我的应用程序容器化,但是包含 goland 执行和 grpc 支持的 docker 镜像的大小更大(超过 1GB)。我想减小 docker 图像的大小。


所需的 golang 版本为 1.9 及更高版本。这是给出的 Dockerfile 脚本。如果有其他方法请建议。


FROM golang:1.11


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


CMD ["go", "run", "helloworld/greeter_server/main.go"]


开满天机
浏览 102回答 2
2回答

慕标5832272

尝试制作这样的多级 docker 镜像# Compile stageFROM golang:1.11 as build-envRUN 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:helloworldRUN go build -o server helloworld/greeter_server/main.go# Making imageFROM alpine:3.8 AS hostRUN apk add --no-cache \        ca-certificatesCOPY --from=build-env /helloworld/server /# copy any other files you needWORKDIR /EXPOSE 8000CMD ["server"]

翻阅古今

您可以尝试使用distroless基础镜像和多阶段构建。那可能对你有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP