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

标签 docker go grpc

我有一些服务器和客户端使用 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"]

最佳答案

尝试像这样制作一个多阶段的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"]

关于docker - 如何在支持 golang 的 gRPC 中减小 docker 镜像的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54743025/

相关文章:

Docker 客户端无法连接到守护程序版本 1.9 : connection reset by peer

docker - initctl : Unable to connect to Upstart: Failed to connect to socket/com/ubuntu/upstart: Connection refused in docker image

go - 如何使用 logpacker package paypal 进行信用卡支付?

c++ - bitbake grpc 交叉编译/配置失败,出现错误 c-ares::cares 引用文件/usr/lib/libcares.so.2.2.0

docker - 在Docker容器中使用RegEx的Grep?

mongodb - FIWARE Cygnus 在空间使用方面有任何问题吗?

go - go 如何知道从哪里获取包

pointers - 如何检查嵌套指针中的值

grpc - 我们如何将 gRPC 与 Flatbuffers 一起使用?

c++ - GRPC 错误代码 12 客户端服务器应用程序 C++ [调试]