python - Docker pip依赖项安装错误

标签 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冻结> 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的已知问题。更多信息在这里:have_directory_for_build AssertionError when installing with --no-cache-dir (19.0.1)

最佳答案

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

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

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



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

关于python - Docker pip依赖项安装错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54432203/

相关文章:

docker - 禁用特定RUN命令的缓存

python - 如何将 "python --version"的输出 append 到 bash shell 中的文件?

Python - 将不同类的对象传递给函数

python - 如何使用 subprocess.POPEN 获取异步输入和输出

json - Pandas |将带有列表/类似数组的字段的 json 文件读取到 boolean 列

linux - docker中安装Alpine的启动命令是什么?

python - Python 中的十进制数相等

python - Pandas 合并_asof : ambiguous argument types error

python - Python 2.x 和 3.x 中用于引发异常的有效语法?

ruby - Chef 容器+ Chef 库