在Python-3.7-alpine的Docker容器上安装gcc失败。

23
我正在尝试设置一个Python docker容器,在运行命令“docker-compose up”构建Dockerfile时,它会抛出以下错误消息。
两周前成功构建了docker文件,由于在requirements.txt中添加了新的库,我想重新构建docker容器。
尝试构建映像会产生以下错误:
Collecting cryptography
  Downloading cryptography-2.8.tar.gz (504 kB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'error'
  ERROR: Command errored out with exit status 1:
   command: /usr/local/bin/python /usr/local/lib/python3.7/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-6ilf25i_/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'setuptools>=40.6.0' wheel 'cffi>=1.8,!=1.11.3; platform_python_implementation != '"'"'PyPy'"'"''
       cwd: None
  Complete output (140 lines):
  Collecting setuptools>=40.6.0
    Downloading setuptools-46.0.0-py3-none-any.whl (582 kB)
  Collecting wheel
    Downloading wheel-0.34.2-py2.py3-none-any.whl (26 kB)
  Collecting cffi!=1.11.3,>=1.8
    Downloading cffi-1.14.0.tar.gz (463 kB)
  Collecting pycparser
    Downloading pycparser-2.20-py2.py3-none-any.whl (112 kB)
  Building wheels for collected packages: cffi
    Building wheel for cffi (setup.py): started
    Building wheel for cffi (setup.py): finished with status 'error'
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-mzaa6f6w
         cwd: /tmp/pip-install-vm8xra6q/cffi/
    Complete output (56 lines):
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    Package 'libffi', required by 'virtual:world', not found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    Package 'libffi', required by 'virtual:world', not found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    Package 'libffi', required by 'virtual:world', not found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    Package 'libffi', required by 'virtual:world', not found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    Package 'libffi', required by 'virtual:world', not found
    running bdist_wheel
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.7
    creating build/lib.linux-x86_64-3.7/cffi
    copying cffi/pkgconfig.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/error.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/model.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/lock.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/recompiler.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/verifier.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/api.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/setuptools_ext.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/vengine_cpy.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/cffi_opcode.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/cparser.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/commontypes.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/__init__.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/backend_ctypes.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/vengine_gen.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/ffiplatform.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/_cffi_include.h -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/parse_c_type.h -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/_embedding.h -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/_cffi_errors.h -> build/lib.linux-x86_64-3.7/cffi
    running build_ext
    building '_cffi_backend' extension
    creating build/temp.linux-x86_64-3.7
    creating build/temp.linux-x86_64-3.7/c
    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.7m -c c/_cffi_backend.c -o build/temp.linux-x86_64-3.7/c/_cffi_backend.o
    c/_cffi_backend.c:15:10: fatal error: ffi.h: No such file or directory
       15 | #include <ffi.h>
          |          ^~~~~~~
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
    ERROR: Failed building wheel for cffi
    Running setup.py clean for cffi
  Failed to build cffi
  Installing collected packages: setuptools, wheel, pycparser, cffi
      Running setup.py install for cffi: started
      Running setup.py install for cffi: finished with status 'error'
      ERROR: Command errored out with exit status 1:
       command: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-akcet9bz/install-record.txt --single-version-externally-managed --prefix /tmp/pip-build-env-6ilf25i_/overlay --compile --install-headers /tmp/pip-build-env-6ilf25i_/overlay/include/python3.7m/cffi
           cwd: /tmp/pip-install-vm8xra6q/cffi/
      Complete output (56 lines):
      Package libffi was not found in the pkg-config search path.
      Perhaps you should add the directory containing `libffi.pc'
      to the PKG_CONFIG_PATH environment variable
      Package 'libffi', required by 'virtual:world', not found
      Package libffi was not found in the pkg-config search path.
      Perhaps you should add the directory containing `libffi.pc'
      to the PKG_CONFIG_PATH environment variable
      Package 'libffi', required by 'virtual:world', not found
      Package libffi was not found in the pkg-config search path.
      Perhaps you should add the directory containing `libffi.pc'
      to the PKG_CONFIG_PATH environment variable
      Package 'libffi', required by 'virtual:world', not found
      Package libffi was not found in the pkg-config search path.
      Perhaps you should add the directory containing `libffi.pc'
      to the PKG_CONFIG_PATH environment variable
      Package 'libffi', required by 'virtual:world', not found
      Package libffi was not found in the pkg-config search path.
      Perhaps you should add the directory containing `libffi.pc'
      to the PKG_CONFIG_PATH environment variable
      Package 'libffi', required by 'virtual:world', not found
      running install
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-3.7
      creating build/lib.linux-x86_64-3.7/cffi
      copying cffi/pkgconfig.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/error.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/model.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/lock.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/recompiler.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/verifier.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/api.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/setuptools_ext.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/vengine_cpy.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/cffi_opcode.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/cparser.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/commontypes.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/__init__.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/backend_ctypes.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/vengine_gen.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/ffiplatform.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/_cffi_include.h -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/parse_c_type.h -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/_embedding.h -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/_cffi_errors.h -> build/lib.linux-x86_64-3.7/cffi
      running build_ext
      building '_cffi_backend' extension
      creating build/temp.linux-x86_64-3.7
      creating build/temp.linux-x86_64-3.7/c
      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.7m -c c/_cffi_backend.c -o build/temp.linux-x86_64-3.7/c/_cffi_backend.o
      c/_cffi_backend.c:15:10: fatal error: ffi.h: No such file or directory
         15 | #include <ffi.h>
            |          ^~~~~~~
      compilation terminated.
      error: command 'gcc' failed with exit status 1
      ----------------------------------------
  ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-akcet9bz/install-record.txt --single-version-externally-managed --prefix /tmp/pip-build-env-6ilf25i_/overlay --compile --install-headers /tmp/pip-build-env-6ilf25i_/overlay/include/python3.7m/cffi Check the logs for full command output.
  ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python /usr/local/lib/python3.7/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-6ilf25i_/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'setuptools>=40.6.0' wheel 'cffi>=1.8,!=1.11.3; platform_python_implementation != '"'"'PyPy'"'"'' Check the logs for full command output.

我的requirements.txt文件如下:

Django>=2.1.3,<2.2.0
djangorestframework>3.9.0,<3.10.0
psycopg2>=2.7.5,<2.8.0
flake8>=3.6.0,<3.7.0
attrdict==2.0.1
certifi ==2019.3.9
chardet ==3.0.4
cytoolz == 0.9.0.1
eth-abi==1.3.0
eth-account == 0.3.0
eth-hash==0.2.0
eth-keyfile ==0.5.1
eth-keys ==0.2.1
eth-rlp==0.1.2
eth-typing==2.1.0
eth-utils ==1.4.1
hexbytes==0.1.0
idna==2.8
lru-dict==1.1.6
parsimonious==0.8.1
pycryptodome==3.8.0
requests==2.21.0
rlp==1.1.0
six==1.12.0
toolz==0.9.0
urllib3==1.24.1
web3==4.9.1
websockets==6.0
uwsgi
psycopg2-binary==2.8.3
Pillow
django-storages[azure]
django-countries
wsgi-basic-auth
celery
redis
django-celery-results
django-rest-knox

我正在使用以下 Dockerfile

FROM python:3.7-alpine
MAINTAINER DB

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client jpeg-dev
RUN apk add --update --no-cache --virtual .tmp-build-deps \
      gcc libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static
RUN adduser -D user
RUN chown -R user:user /vol/
RUN chmod -R 755 /vol/web
USER user

新库是什么? - Z4-tier
@Z4-tier - 请查看我上面的需求文件。新添加的库是django-rest-knox和web3.py。 - David
请问这里可能出了什么问题? - David
@Z4-tier - 我找到了 requirements.txt 安装失败的根本原因。"坏苹果" 是 django-rest-knox 库。为什么会这样呢? - David
这是一个好问题。我也在看那个库,但目前我没有一个好的答案。你可以尝试的一件事是从alpine切换到python:3.7。很多时候,使用alpine节省的空间并不值得花费大量的精力来让它首先工作起来。 - Z4-tier
6个回答

29

对我来说,在Docker容器中安装这些软件包有所帮助:

RUN apk update && apk add python3-dev \
                        gcc \
                        libc-dev

9
我还需要安装 libffi-dev,请参考 https://github.com/jfloff/alpine-python/issues/44。 - Justin Harris
1
我也需要 libffi-dev,但不需要 libc-dev。也许这是另一个问题的一部分,我不能确定。 - questionto42
在 CircleCI 上运行工作流时,我遇到了这个问题 image: cimg/base:2022.06-18.04。通过使用上述所有方法,问题得以解决。仅供参考。 - nate

18

以下三种方法中的一种或许可以帮到你:

1 - 使用 python:<version> 或者 python:<version>-slim 镜像代替 python:<version>-alpine:

这种方法使用不同的 Python 基础镜像,其中一些预装了一些实用工具。

Alpine 只提供了一些必要的实用工具,因此你可能需要自己安装更多的实用工具。

2 - 安装所需依赖:

RUN apk update && apk add python3-dev \
                          gcc \
                          libc-dev \
                          libffi-dev

3 - 降级 Python 版本:

我不确定这个解决方案是否可行,也不建议使用它,只是记得曾经有一次它起作用了。


另外需要注意的是,除非你有很多存储空间的问题,否则不建议使用 alpine 镜像。


1
使用不同的Linux版本并不总是可行的。例如,任何在ARM32v5硬件上工作的人,比如树莓派Zero,将无法使用其他Python镜像。只有Alpine适用于这种硬件。 - Vince
使用 python:3.9 而不是 python:3.9-alpine 对我有用。 - Sau001

6

我通过使用非alpine镜像成功解决了同样的错误。因此,我的Dockerfile顶部现在是:

    FROM python:3.8.0

这是因为Alpine镜像比其他Linux发行版要轻巧得多。如果您出于某种原因使用Alpine,则最好在Dockerfile中添加软件包,而不是切换到完整的镜像。 - James

1

我对提到的错误的解决方案:

FROM python:3.8-alpine

RUN apk add --no-cache --virtual .build-deps gcc libffi-dev musl-dev && pip install cython \

   && pip install cffi && apk del .build-deps gcc libffi-dev musl-dev && pip install -r requirements.txt

1

我刚刚找到了一个解决方案。您需要在docker文件中添加以下命令

RUN apk add --update alpine-sdk


(注:该命令是用于安装Alpine SDK的)

0

1
这个链接很有帮助。已经存在的踩可能是因为仅发布链接不符合“Stack Overflow规则”的正式原因。我想它应该在问题下面的评论中。但是,你经常会跳过一两个评论。这就是为什么这个链接几乎得到了我的赞成。 - questionto42

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接