繁体   English   中英

Docker pip 依赖项安装错误

[英]Docker pip dependencies installation error

我正在尝试为我编写的 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 freeze > 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

编辑2:

似乎这是一个 Alpine Linux 已知问题。 此处有更多信息:使用 --no-cache-dir (19.0.1) 安装时有 has_directory_for_build AssertionError

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

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

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

这可能不是最好的长期解决方案,像多阶段构建这样的东西可能会更好。

我尝试拉取 alpine:3.4 的版本并且它可以工作,但是对于 3.7 和 3.9,python 中依赖项的安装每次都会崩溃。 它应该是一些高山形象。

解决了:
我在安装Fabric2 时遇到了错误,它的依赖项之一是bcryptcryptography ,并且它们抛出了许多错误,因此在Cryptography 注释中查找它说您必须在 Dockerfile 中添加这一行:

运行 apk-install gcc musl-dev python3-dev libffi-dev openssl-dev make

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

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

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

您可以在 alpine 中使用 python docker文件: Python 3.7 Alpine 3.8 Dockerfile

您可以查看此处以根据您拥有的 Python 版本获取适当的图像,它应该消除必须安装任何缺少的 Python 依赖项的开销。 然后在你的 dockerfile 的顶部,你可以写:

FROM 3.7.2-alpine3.8

如果您不想让根目录添加 libffi-dev 和 gcc 到以下命令应该可以解决您的问题。 如果您查看链接的 dockerfile,您可以看到 Python 映像中可能需要包含许多其他依赖项。

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

希望这可以帮助!

在安装 python 要求之前,尝试将 gcc 安装到 apt 中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM