python - 在 Python Alpine Docker 中加载共享库 libresolv.so.2 时出错

标签 python amazon-web-services docker jenkins alpine-linux

我在 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/

相关文章:

python - 给定输入字符串的字典值的所有可能组合。 Python

python - 使用带有 tkinter 输入 GUI 形式的内部函数结束正在运行的函数,并在退出 GUI 时显示警告消息

laravel - 如何在 AWS CodeCommit 和 CodePipeline 中为 Laravel 动态生成 .env 文件

python - 从视频第一帧的鼠标事件中记录两组点位置

python - 使用 Python 在 Google App Engine 中对图像进行逐像素操作

javascript - S3 : How to upload a large file using S3 in nodejs using aws-sdk

hadoop - 将不同 S3 文件夹中的文件作为输入传递给 mapreduce

docker - Vagrant:在 Windows 上使用 boot2docker 框同步文件夹

docker - 如何始终使用最新版本的 Docker 基础镜像?

python-3.x - 如何通过本地 Docker 容器中的 python 应用程序从云存储桶中读取文件