我在 CI/CD (Jenkins) 中使用 Alpine Linux 作为我的 Python 应用程序的 Docker 镜像。这是针对 AWS Lambda 的,基本上唯一值得注意的要求是用于访问 Postgres 的 aws-psycopg2
。在本地一切正常,它可以很好地部署无服务器并在 AWS 上运行,唯一的问题是在 Jenkins 上。我在那里遇到错误:
16:24:55 ImportError: Error loading shared library libresolv.so.2: No such file or directory (needed by /usr/local/lib/python3.9/site-packages/psycopg2/../psycopg2_binary.libs/libgssapi_krb5-497db0c6.so.2.2)
我没有以任何方式使用 Oracle,例如in this question .
我的jenkinsfile.groovy
:
def testInstallStage = {
stage('Install') {
sh 'apk add python3-dev gcc libc-dev musl-dev openblas gfortran build-base postgresql-libs postgresql-dev libffi-dev cargo'
sh 'pip install --upgrade pip && pip install poetry'
sh 'poetry export --without-hashes --dev -f requirements.txt -o requirements.txt --with-credentials'
sh 'pip install -r requirements.txt'
sh 'pip install awscli'
sh 'aws configure set aws_access_key_id default_access_key'
sh 'aws configure set aws_secret_access_key default_secret_key'
sh 'aws configure set default.region eu-west-1'
sh 'cp config/jenkins.json config.json'
}
}
def testStage = {
stage('Tests') {
sh 'pip install nose && nosetests'
}
}
new pl.ruby.PipelineRunner(this, notificationChannel).run() {
pythonStack([image: '3.9.13-alpine3.16'], {
container('python') {
testInstallStage.call()
testStage.call()
}
})
}
apk add
整行是我试图解决这个问题。我尝试安装许多不同的依赖项。我应该安装什么才能让它工作?我知道这是 Alpine 上缺少的一些库,因为在 Ubuntu 20.04 和 AWS Lambda 运行时它工作正常。
最佳答案
库 libresolv.so.2
是系统 C 库的一部分。基于 Alpine 的图像使用不同版本的 C 库 (musl libc),它要小得多,但偶尔也会遇到这样的兼容性问题。
解决此问题的最简单方法是切换到基于更标准 GNU libc 的基于 Debian 或 Ubuntu 的镜像。由于您在这里使用的是未经修改的图像,因此您只需删除图像标签中的“Alpine”引用即可
pythonStack([image: '3.9.13'], ...)
在自定义 Dockerfile 中,如果您需要安装任何操作系统级别的包,您可能还需要更改包管理器调用,因为 Debian/Ubuntu 和 Alpine 具有不同的打包格式、工具和名称。 testInstallStage
的第一行也会受此影响。
使用基于 Alpine 的图像的典型原因是节省空间。这里实际节省的空间相当小(几十兆字节),一旦你开始安装 C 头文件,图像很快就会变得比这大得多。您可能不会注意到进行此更改时性能或图像大小的实际差异。
关于python - 在 Python Alpine Docker 中加载共享库 libresolv.so.2 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72871126/