Alpine 3.13.5环境下安装confluent-kafka Python模块失败求助
问题场景
在GitLab Runner的Docker-in-Docker环境(基于docker:19.03镜像,对应Alpine 3.13.5系统)中安装confluent-kafka==1.8.2 Python模块,使用的.gitlab-ci.yml配置如下:
default: image: name: docker:19.03 entrypoint: - '/usr/bin/env' - 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' tags: - general services: - docker:dind before_script: - apk update - apk add gcc libc-dev libffi-dev librdkafka-dev - apk add --update --no-cache python3 python3-dev && ln -sf python3 /usr/bin/python - python3 -m ensurepip - pip3 install --no-cache --upgrade pip setuptools - pip install poetry - poetry install - source `poetry env info --path`/bin/activate
执行poetry install时失败,核心报错:
In file included from /tmp/tmphywbhg0_/confluent-kafka-1.8.2/src/confluent_kafka/src/Admin.c:17:
/tmp/tmphywbhg0_/confluent-kafka-1.8.2/src/confluent_kafka/src/confluent_kafka.h:66:2: error: #error "confluent-kafka-python requires librdkafka v1.6.0 or later. Install the latest version of librdkafka from the Confluent repositories..."
问题原因
Alpine 3.13.5默认软件源中的librdkafka-dev版本低于confluent-kafka==1.8.2要求的v1.6.0,导致编译时版本校验失败。
解决方案
添加Confluent官方软件源,获取符合版本要求的librdkafka-dev包,修改.gitlab-ci.yml的before_script部分:
before_script: - apk update # 导入Confluent GPG密钥并添加软件源 - wget -qO - https://packages.confluent.io/deb/7.2/archive.key | apk add --no-cache --virtual .confluent-archive-keyring - && echo "https://packages.confluent.io/alpine/7.2/main" >> /etc/apk/repositories # 安装指定版本的librdkafka-dev(与confluent-kafka版本匹配) - apk add gcc libc-dev libffi-dev librdkafka-dev=1.8.2-r0 - apk add --update --no-cache python3 python3-dev && ln -sf python3 /usr/bin/python - python3 -m ensurepip - pip3 install --no-cache --upgrade pip setuptools - pip install poetry - poetry install - source `poetry env info --path`/bin/activate
说明
- 添加Confluent的Alpine源后,可获取到与
confluent-kafka==1.8.2兼容的librdkafka-dev版本(指定1.8.2-r0确保版本匹配) - 先导入GPG密钥,避免安装时出现签名验证错误
备选方案:若不想编译依赖,可尝试安装预编译二进制包,在poetry install前执行pip install --only-binary :all: confluent-kafka==1.8.2,但该方式受平台支持限制,优先推荐上述源安装方式。
内容的提问来源于stack exchange,提问作者curiousguy

