【问题标题】:Docker fails to install cffi with python:3.9-alpine in DockerfileDocker 无法在 Dockerfile 中使用 python:3.9-alpine 安装 cffi
【发布时间】:2022-06-30 04:44:23
【问题描述】:

我正在尝试使用 docker-compose 运行以下 Dockerfile。 我四处搜索,但找不到有关如何使用 python:3.9-alpine 安装 cffi 的解决方案。

我还阅读了这篇文章,其中指出 pip 21.2.4 或更高版本可能是一个可能的解决方案,但对我来说并没有成功

https://www.pythonfixing.com/2021/09/fixed-why-i-getting-this-error-while.html

Docker 文件

FROM python:3.9-alpine

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt .

RUN apk add --update --no-cache postgresql-client

RUN apk add --update --no-cache --virtual .tmp-build-deps \
    gcc libc-dev linux-headers postgresql-dev
RUN pip3 install --upgrade pip && pip3 install -r /requirements.txt

RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY . /app

RUN adduser -D user

USER user

这是 requirements.txt 文件。

asgiref==3.5.0
backports.zoneinfo==0.2.1
certifi==2021.10.8
cffi==1.15.0
cfgv==3.3.1
...

错误信息:

process-exited-with-error
#9 47.99   
#9 47.99   × Running setup.py install for cffi did not run successfully.
#9 47.99   │ exit code: 1
#9 47.99   ╰─> [58 lines of output]
#9 47.99       Package libffi was not found in the pkg-config search path.
#9 47.99       Perhaps you should add the directory containing `libffi.pc'
#9 47.99       to the PKG_CONFIG_PATH environment variable
#9 47.99       Package 'libffi', required by 'virtual:world', not found
#9 47.99       Package libffi was not found in the pkg-config search path.
#9 47.99       Perhaps you should add the directory containing `libffi.pc'
#9 47.99       to the PKG_CONFIG_PATH environment variable
#9 47.99       Package 'libffi', required by 'virtual:world', not found
#9 47.99       Package libffi was not found in the pkg-config search path.
#9 47.99       Perhaps you should add the directory containing `libffi.pc'
#9 47.99       to the PKG_CONFIG_PATH environment variable
#9 47.99       Package 'libffi', required by 'virtual:world', not found
#9 47.99       Package libffi was not found in the pkg-config search path.
#9 47.99       Perhaps you should add the directory containing `libffi.pc'
#9 47.99       to the PKG_CONFIG_PATH environment variable
#9 47.99       Package 'libffi', required by 'virtual:world', not found
#9 47.99       Package libffi was not found in the pkg-config search path.
#9 47.99       Perhaps you should add the directory containing `libffi.pc'
#9 47.99       to the PKG_CONFIG_PATH environment variable
#9 47.99       Package 'libffi', required by 'virtual:world', not found
#9 47.99       running install
#9 47.99       running build
#9 47.99       running build_py
#9 47.99       creating build
#9 47.99       creating build/lib.linux-aarch64-3.9
#9 47.99       creating build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/__init__.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/cffi_opcode.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/commontypes.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/vengine_gen.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/vengine_cpy.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/backend_ctypes.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/api.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/ffiplatform.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/verifier.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/error.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/setuptools_ext.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/lock.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/recompiler.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/pkgconfig.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/cparser.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/model.py -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/_cffi_include.h -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/parse_c_type.h -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/_embedding.h -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       copying cffi/_cffi_errors.h -> build/lib.linux-aarch64-3.9/cffi
#9 47.99       warning: build_py: byte-compiling is disabled, skipping.
#9 47.99       
#9 47.99       running build_ext
#9 47.99       building '_cffi_backend' extension
#9 47.99       creating build/temp.linux-aarch64-3.9
#9 47.99       creating build/temp.linux-aarch64-3.9/c
#9 47.99       gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -DUSE__THREAD -DHAVE_SYNC_SYNCHRONIZE -I/usr/include/ffi -I/usr/include/libffi -I/usr/local/include/python3.9 -c c/_cffi_backend.c -o build/temp.linux-aarch64-3.9/c/_cffi_backend.o
#9 47.99       c/_cffi_backend.c:15:10: fatal error: ffi.h: No such file or directory
#9 47.99          15 | #include <ffi.h>
#9 47.99             |          ^~~~~~~
#9 47.99       compilation terminated.
#9 47.99       error: command '/usr/bin/gcc' failed with exit code 1
#9 47.99       [end of output]
#9 47.99   
#9 47.99   note: This error originates from a subprocess, and is likely not a problem with pip.
#9 47.99 error: legacy-install-failure
#9 47.99 
#9 47.99 × Encountered error while trying to install package.
#9 47.99 ╰─> cffi
#9 47.99 
#9 47.99 note: This is an issue with the package mentioned above, not pip.
#9 47.99 hint: See above for output from the failure.

【问题讨论】:

  • 让我为您阅读错误消息... "Package 'libffi', required" 嗯!

标签: python docker docker-compose pip python-cffi


【解决方案1】:

缺少 libffi 库。

将其添加到您的 dockerfile:

RUN apk add libffi-dev

【讨论】:

    【解决方案2】:

    @Klaus D. 的评论很有帮助。 我更新了 Dockerfile:

    RUN apk add --update --no-cache --virtual .tmp-build-deps \
        gcc libc-dev linux-headers postgresql-dev \
        && apk add libffi-dev
    

    【讨论】:

    • 只是 libffi-dev 没有为我剪掉它;这完成了这项工作。谢谢!
    • 在使用 alpine 标记的 python 图像时,我遇到了与 cffi 相同的问题。 python 3.9 或 3.10 都不会成功构建。我需要做的最少的补充是:RUN apk add gcc libc-dev libffi-dev
    【解决方案3】:

    如果您正在安装在openjdk:11.0.11-jre-slim-buster Docker 容器上运行的 Python 包,并且您会遇到此问题。添加以下命令以构建您的 docker 映像。

    RUN apt-get install -y build-essential libssl-dev libffi-dev python-dev
    RUN python3 -m pip install --upgrade pip
    COPY ./requirements.txt /
    

    【讨论】:

      猜你喜欢
      • 2020-01-07
      • 2019-02-23
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 2017-05-25
      • 2020-10-17
      相关资源
      最近更新 更多