问题
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