无法在Docker容器中安装依赖项

时间:2019-08-02 18:18:50

标签: python docker python-poetry

我正在使用parLapply(cl, replicate(7, inputs, simplify=FALSE), perms) 库来管理项目依赖项,所以当我使用

poetry

我收到此错误

docker build --tag=helloworld .

[AttributeError] 'NoneType' object has no attribute 'group' 软件包上安装中断

这是我的umongo (2.1.0)文件

pyproject.toml

还有[tool.poetry.dependencies] python = "^3.7.0" asyncio = "^3.4" aiohttp = "^3.4" motor = "^2.0" umongo = "^2.0" pyyaml = "^3.13" [tool.poetry.dev-dependencies] pytest = "^3.4" black = {version = "^18.3-alpha.0",allows-prereleases = true} mypy = "^0.650.0" wemake-python-styleguide = "^0.5.1" pytest-mock = "^1.10" pytest-asyncio = "^0.9.0" pytest-aiohttp = "^0.3.0" https://pastebin.com/kUjAKJHM

Dockerfile:

poetry.lock

2 个答案:

答案 0 :(得分:3)

替代方法

请勿将poetry安装到您的部署环境中。这是一个软件包管理工具,旨在改善图书馆的开发和协作。如果要部署应用程序,则只需要一个软件包安装程序(阅读:pip),而poetry关于构建过程和虚拟环境的坚定立场是有害的,而不是无济于事。

在这种情况下,您要复制到docker映像中的工件是 1)您正在使用的库的最新版本以及 2)测试的依存关系,由poetry.lock定义。

第一个很简单,运行poetry build -f wheel,您会拥有一个很好的便携式轮子。第二种方法并不容易,因为poetry不支持建立操舵室(也许永远不会),并且pip wheel不接受poetry.lock的文件格式。因此,如果您想走这条路,就需要开发一个poetry(很稳定的v1.0.0b7测试版),它支持poetry export -f requirements.txt > requirements.txt,可让您创建一个{{1} }文件,相当于您当前的锁定文件。

一旦知道了,就可以运行requirements.txt,然后 finally ,就可以为docker映像创建所有工件了。现在,以下将起作用:

pip wheel -w dist -r requirements.txt

优点

  • 对服务器中的FROM python:3.7.1-alpine WORKDIR /opt/project COPY dist dist RUN pip install --no-index --find-links dist todo_api ENTRYPOINT python -m aiohttp.web todo_api.main:main 没有不必要的依赖(可能很重要,因为它仍然是poetry
  • 您跳过服务器中的virtualenv并将所有内容直接安装到系统中
  • 您的安装步骤并未针对pypi进行,因此可以保证该部署可以在您对其进行测试的范围内正常工作(这在许多业务设置中都是非常重要的一点)

缺点

  • 如果您每次手动操作都会很痛苦,这里的目标执行者应该是CI / CD,而不是人类
  • 如果工作站的架构和docker映像不同,则构建和复制的轮子可能不兼容

答案 1 :(得分:1)

以下对我有用:

FROM python:3.7.1-alpine

WORKDIR /opt/project

RUN pip install --upgrade pip && pip --no-cache-dir install poetry

COPY ./pyproject.toml .

RUN poetry install --no-dev

与pyproject.toml:

[tool.poetry]
name = "57331667"
version = "0.0.1"
authors = ["skufler <skufler@email.com>"]

[tool.poetry.dependencies]
python = "^3.7.0"
asyncio = "^3.4"
aiohttp = "^3.4"
motor = "^2.0"
umongo = "^2.0"
pyyaml = "^3.13"

[tool.poetry.dev-dependencies]
pytest = "^3.4"
black = {version = "^18.3-alpha.0",allows-prereleases = true}
mypy = "^0.650.0"
wemake-python-styleguide = "^0.5.1"
pytest-mock = "^1.10"
pytest-asyncio = "^0.9.0"
pytest-aiohttp = "^0.3.0"

然后:

docker build --tag=57331667 --file=./Dockerfile .

结果:

...
Creating virtualenv 57331667-py3.7 in /root/.cache/pypoetry/virtualenvs
Updating dependencies
Resolving dependencies...

Writing lock file


Package operations: 15 installs, 0 updates, 0 removals

  - Installing idna (2.8)
  - Installing multidict (4.5.2)
  - Installing six (1.12.0)
  - Installing async-timeout (3.0.1)
  - Installing attrs (18.2.0)
  - Installing chardet (3.0.4)
  - Installing marshmallow (2.19.5)
  - Installing pymongo (3.8.0)
  - Installing python-dateutil (2.8.0)
  - Installing yarl (1.3.0)
  - Installing aiohttp (3.5.4)
  - Installing asyncio (3.4.3)
  - Installing motor (2.0.0)
  - Installing pyyaml (3.13)
  - Installing umongo (2.1.0)
Removing intermediate container c6a9c7652b5c
 ---> 89354562cf16
Successfully built 89354562cf16
Successfully tagged 57331667:latest