我正在构建一个处理Apache Pulsar的GO应用程序。Go客户端需要C++库,正如Pulsar文档所要求的( Kafka btw也是如此)。
我想把所有这些都打包到一个容器中,尽可能小。我通常使用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
...
而本地构建工作。
如何正确集成我需要的库?
发布于 2020-01-19 00:27:34
由于您使用的库具有docker lib依赖项,因此要正确构建Pulsar Golang客户端Docker镜像,您需要使用C++ stage build。我们有确切的用例。您需要下载并安装Pulsar C++ lib for build and run-time image in Dockerfile
。
这是我们的docker文件https://github.com/kafkaesque-io/pulsar-beam/blob/master/Dockerfile。我还建议使用Go模块来构建和管理Go依赖项,作为Docker stage构建的一部分。这就是我们怎么做的。我希望它能帮上忙。
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
占用的空间更小。你也可以试试我还没测试过的阿尔卑斯山。
顺便说一句,Apache提供了一个独立的原生Pulsar Go客户端库,没有C++依赖。在2020年1月撰写本文时,仍然缺少一些功能,例如用于分区主题的自定义路由模式。如果您的项目中不需要这些功能,我建议尝试使用原生Go客户端库,以避免对C++的依赖。出于同样的原因,我们计划很快切换到新的本地库。
https://stackoverflow.com/questions/59800128
复制相似问题