【问题标题】:How to decrease the size of docker image with gRPC in golang supported?如何在支持 golang 的 gRPC 中减小 docker 镜像的大小?
【发布时间】:2019-07-11 14:03:55
【问题描述】:

我有一些使用 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"]

【问题讨论】:

  • 当前目录的内容是什么?您是否尝试使用stage 构建?你用过.dockerignore 吗?
  • 当前目录是 grpc 示例,可在 grpc 存储库 [github.com/grpc/grpc-go/tree/master/examples] 中找到。我不知道舞台搭建。可以提供资源吗?
  • 你需要从上下文中排除你不需要的东西,你可以通过COPY只需要directroy或使用.dockerignore文件来完成。以下是关于多阶段构建blog.alexellis.io/mutli-stage-docker-builds 的一些信息
  • 你能分享你从哪张图片开始的吗? Dockerfile中的FROM
  • @Matteo 目前我是从 golang 1.11 开始,但我已经看到它是从 debian 开始的,它们可能体积很小,但我不太了解。

标签: docker go grpc


【解决方案1】:

尝试制作像这样的多级 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"]

【讨论】:

  • 您在 Linux 机器上关注这篇文章,只需将 alpine:3.8 替换为 debian:linux,您就可以开始了
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多