【问题标题】:Docker build how to add libraries to golang buildDocker build 如何将库添加到 golang build
【发布时间】:2020-05-05 02:17:41
【问题描述】:

我正在构建一个处理 Apache Pulsar 的 GO 应用程序。按照 Pulsar 文档的要求,Go 客户端需要 C++ 库(顺便说一句,Kafka 也是如此)。

我想将所有这些打包到一个容器中,尽可能地小。我通常使用 SCRATCH 并从另一个基于 golang 的容器中复制输出。不幸的是,我无法从这个初始容器中获取外部库:

FROM golang:latest as builder
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
ARG DOCKER_GIT_CREDENTIALS
WORKDIR /builder
ADD . /builder
RUN git config --global credential.helper store && echo "${DOCKER_GIT_CREDENTIALS}" > ~/.git-credentials
RUN make build
RUN echo $(go list -m) && mv bin/$(go list -m) app

FROM SCRATCH
COPY --from=builder /builder/app app
EXPOSE 8080
ENTRYPOINT ["./app"]

使用它会使构建失败,寻找丢失的符号

/go/pkg/mod/github.com/apache/pulsar/pulsar-client-go@v0.0.0-20200118070759-21660e9402f8/pulsar/client.go:29:9: undefined: newClient
...

本地构建工作时。

如何正确集成我需要的库?

【问题讨论】:

    标签: docker go dockerfile apache-pulsar


    【解决方案1】:

    由于您使用的库具有 C++ lib 依赖项,因此要正确构建 Pulsar Golang 客户端 docker 映像,您需要使用 Docker 阶段构建。我们有确切的用例。您需要在Dockerfile 中下载并安装 Pulsar C++ lib 用于构建和运行时映像。

    这是我们的 docker 文件 https://github.com/kafkaesque-io/pulsar-beam/blob/master/Dockerfile 。我还建议使用 Go Module 来构建和管理 Go 依赖项,作为 Docker 阶段构建的一部分。这是我们的做法。希望对你有帮助。

    RUN wget --user-agent=Mozilla -O apache-pulsar-client.deb "https://archive.apache.org/dist/pulsar/pulsar-2.4.1/DEB/apache-pulsar-client.deb"
    RUN wget --user-agent=Mozilla -O apache-pulsar-client-dev.deb "https://archive.apache.org/dist/pulsar/pulsar-2.4.1/DEB/apache-pulsar-client-dev.deb"
    
    RUN apt install -y ./apache-pulsar-client.deb
    RUN apt install -y ./apache-pulsar-client-dev.deb
    
    # Copy go mod and sum files
    COPY go.mod go.sum ./
    
    # Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
    RUN go mod download
    

    我使用标准 ubuntu 映像进行构建以及运行时映像。我了解您正在寻找尽可能小的图像尺寸。 ubuntu:18.04 占用空间更小。你也可以试试我还没测试过的alpine。

    顺便说一下,Apache 提供了一个独立的本地 Pulsar Go 客户端库,不依赖于 C++。在 2020 年 1 月撰写本文时,仍然缺少分区主题的自定义路由模式等功能。如果您的项目不需要这些功能,我建议尝试使用本机 Go 客户端库以避免 C++ 依赖。出于同样的原因,我们计划很快切换到新的原生库。

    【讨论】:

    猜你喜欢
    • 2015-12-24
    • 2011-07-10
    • 2018-07-30
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2018-12-15
    相关资源
    最近更新 更多