Docker pip依赖项安装错误

时间:2019-01-30 01:41:13

标签: python python-3.x docker pip

我正在尝试为我编写的Flask应用构建Docker映像,但是在安装构建依赖项时遇到与pip相关的错误,如下面的日志所示。

我正在使用pipenv进行依赖项管理,并且可以使用pipenv run python3 run.py

使应用在本地运行而不会出现任何错误

似乎无法安装bcrypt,但我不知道为什么。

Dockerfile:

FROM alpine:3.8

RUN apk add --no-cache python3-dev && pip3 install --upgrade pip
WORKDIR /app
COPY . /app
RUN pip3 --no-cache-dir install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python3"]
CMD ["run.py"]

requirements.txt(通过pipenv shell生成; pip Frozen> requirements.txt)

bcrypt==3.1.6
blinker==1.4
cffi==1.11.5
Click==7.0
Flask==1.0.2
Flask-Bcrypt==0.7.1
Flask-Login==0.4.1
Flask-Mail==0.9.1
Flask-SQLAlchemy==2.3.2
Flask-WTF==0.14.2
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.0
Pillow==5.4.1
pycparser==2.19
six==1.12.0
SQLAlchemy==1.2.17
Werkzeug==0.14.1
WTForms==2.2.1

Docker构建映像进程日志:

$ docker build -t flaskapp:latest .
Sending build context to Docker daemon   2.16MB
Step 1/8 : FROM alpine:3.8
 ---> 3f53bb00af94
Step 2/8 : RUN apk add --no-cache python3-dev && pip3 install --upgrade pip
 ---> Using cache
 ---> 3856c6d59bbe
Step 3/8 : WORKDIR /app
 ---> Using cache
 ---> 54ed0e7464e4
Step 4/8 : COPY . /app
 ---> Using cache
 ---> 9e045f4ce91c
Step 5/8 : RUN pip3 --no-cache-dir install -r requirements.txt
 ---> Running in 25909f37b071
Collecting bcrypt==3.1.6 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/ce/3a/3d540b9f5ee8d92ce757eebacf167b9deedb8e30aedec69a2a072b2399bb/bcrypt-3.1.6.tar.gz (42kB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'error'
  Complete output from command /usr/bin/python3.6 /usr/lib/python3.6/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-9iojppec/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- setuptools wheel "cffi>=1.1; python_implementation != 'PyPy'":
  Collecting setuptools
    Downloading https://files.pythonhosted.org/packages/bf/ae/a23db1762646069742cc21393833577d3fa438eecaa59d11fb04fa57fcd5/setuptools-40.7.1-py2.py3-none-any.whl (574kB)
  Collecting wheel
    Downloading https://files.pythonhosted.org/packages/ff/47/1dfa4795e24fd6f93d5d58602dd716c3f101cfd5a77cd9acbe519b44a0a9/wheel-0.32.3-py2.py3-none-any.whl
  Collecting cffi>=1.1
    Downloading https://files.pythonhosted.org/packages/e7/a7/4cd50e57cc6f436f1cc3a7e8fa700ff9b8b4d471620629074913e3735fb2/cffi-1.11.5.tar.gz (438kB)
      Complete output from command python setup.py egg_info:
      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

          No working compiler found, or bogus compiler options passed to
          the compiler from Python's standard "distutils" module.  See
          the error messages above.  Likely, the problem is not related
          to CFFI but generic to the setup.py of any Python package that
          tries to compile C code.  (Hints: on OS/X 10.8, for errors about
          -mno-fused-madd see http://stackoverflow.com/questions/22313407/
          Otherwise, see https://wiki.python.org/moin/CompLangPython or

编辑:

将Dockerfile中的RUN命令更改为:

RUN apk add --no-cache python3-dev openssl-dev libffi-dev gcc musl-dev && pip3 install --upgrade pip

我现在收到此错误(发布了相关部分):

...
Collecting SQLAlchemy==1.2.17 (from -r requirements.txt (line 17))
Downloading https://files.pythonhosted.org/packages/c6/52/73d1c92944cd294a5b165097038418abb6a235f5956d43d06f97254f73bf/SQLAlchemy-1.2.17.tar.gz (5.7MB)
Collecting Werkzeug==0.14.1 (from -r requirements.txt (line 18))
Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
Collecting WTForms==2.2.1 (from -r requirements.txt (line 19))
Downloading https://files.pythonhosted.org/packages/9f/c8/dac5dce9908df1d9d48ec0e26e2a250839fa36ea2c602cc4f85ccfeb5c65/WTForms-2.2.1-py2.py3-none-any.whl (166kB)
Exception:
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/pip/_internal/cli/base_command.py", line 176, in main
status = self.run(options, args)
File "/usr/lib/python3.6/site-packages/pip/_internal/commands/install.py", line 346, in run
session=session, autobuilding=True
File "/usr/lib/python3.6/site-packages/pip/_internal/wheel.py", line 886, in build
assert have_directory_for_build
AssertionError
The command '/bin/sh -c pip3 --no-cache-dir install -r requirements.txt' returned a non-zero code: 2

EDIT2:

这似乎是Alpine Linux的已知问题。此处更多信息:have_directory_for_build AssertionError when installing with --no-cache-dir (19.0.1)

4 个答案:

答案 0 :(得分:3)

问题是cffi需要编译器和开发库才能在安装时使用。

解决这个问题的一种简单方法是在docker构建过程中简单地安装所需的软件包。

  

运行apk添加--no-cache python3-dev openssl-dev libffi-dev gcc && pip3 install --upgrade pip

这可能不是最佳的长期解决方案,而像multi-stage builds这样的长期解决方案可能会更好。

答案 1 :(得分:2)

我尝试提取alpine:3.4的版本,并且可以运行,但是使用3.7和3.9时,每次在python中安装依赖项都会崩溃。应该是高山的东西。

已解决:
我在安装 Fabric2 时出错,它的依赖项之一是 bcrypt cryptography ,这些引发了很多错误,因此在{{ 3}}它表示您必须在Dockerfile中添加以下行:

运行apk安装gcc musl-dev python3-dev libffi-dev openssl-dev make

最糟糕的是,它增加了231MiB中的层大小

降低图像大小的一种好方法是在一个RUN子句中安装这些工具,安装python库并卸载之前所需的工具
图像大小缩小到83MiB,所以这是最好的主意。

在这种情况下,我使用了最新版本的alpine,现在可以正常使用了!

答案 2 :(得分:0)

在安装python要求之前,尝试使用apt将gcc安装到您的计算机中。

答案 3 :(得分:0)

您可以将Python docker文件与alpine一起使用:Python 3.7 Alpine 3.8 Dockerfile

您可以根据自己拥有的python版本查看here来获取适当的映像,它应该消除必须安装所有缺少的python依赖项的开销。然后,您可以在dockerfile的顶部编写:

FROM 3.7.2-alpine3.8

如果您不想在rootff中添加libffi-dev和gcc到以下命令,则可以解决您的问题。如果您查看链接的dockerfile,虽然可以看到python映像中可能还包含许多其他依赖项。

RUN apk add --no-cache python3-dev libffi-dev gcc && pip3 install --upgrade pip 

希望这会有所帮助!