Build Golang Application with librdkafka in a Debian Docker Image?

谁说我不能喝 提交于 2020-03-21 06:10:17

问题


With Alpine, Alpine fully supports recent versions of librdkafka, I can just do apk add in my Dockerfile, and the following works:

FROM golang:1.13-alpine3.10 as builder

WORKDIR /app
COPY go.mod go.sum ./
COPY src ./src/

RUN set -eux; \
  apk add --no-cache gcc git libc-dev librdkafka-dev; \
  go build -o ./ ./...

Now, for a particular project, I need to make Debian friendly binaries, that will run on Debian/Ubuntu servers.

The problem is that:

  • The official Debian repositories only support really old 0.11.x versions of librdkafka. Even for stretch and buster including backports repos. They don't have more recent versions.
  • The official Confluent repositories only support librdkafka on Debian 8 (jessie). They don't support librdkafka at all on Debian 9 (stretch) or 10 (buster) due to a libssl version incompatibility.
  • The official golang images only support Debian 9 (stretch) and 10 (buster). They don't support Debian 8 (Jessie).

My options:

  • Use a dev branch of the Golang Kafka client that doesn't need librdkafka installed at the system level. This would be amazing if it was stable and recommended.
  • Manually install/build librdkafka on Debian 9/10.
  • Get a Debian 8 golang image?
  • Can I do Debian target builds from Alpine? I suspect no, but it's worth asking.

What is the recommended solution?


回答1:


Here is the solution which worked for me. I had to download it from source and it gives the latest version

Sample Dockerfile looks like this:

FROM golang:1.12.9-alpine AS build-stage

LABEL app="application_name"

ENV PATH=$PATH:$GOROOT/bin:$GOPATH/bin

# Because of https://github.com/docker/docker/issues/14914
# required by go get
RUN apk add --update --no-cache alpine-sdk bash python ca-certificates \
      libressl \
      tar \
      git openssh openssl yajl-dev zlib-dev cyrus-sasl-dev openssl-dev coreutils

WORKDIR /src/application_name

RUN git clone https://github.com/edenhill/librdkafka.git
WORKDIR /src/application_name/librdkafka
RUN /src/application_name/librdkafka/configure --prefix /usr
RUN make
RUN make install

WORKDIR /src/application_name
COPY . .
# build the application
RUN GOOS=linux go build -a -o image-name .


来源:https://stackoverflow.com/questions/58128541/build-golang-application-with-librdkafka-in-a-debian-docker-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!