5

I'm using poetry library to manage project dependencies, so when I use

docker build --tag=helloworld .

I got this error

[AttributeError]               
'NoneType' object has no attribute 'group'  

Installing breaks on umongo (2.1.0) package

Here is my pyproject.toml file

[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"

And poetry.lock https://pastebin.com/kUjAKJHM

Dockerfile:

FROM python:3.7.1-alpine

RUN mkdir -p /opt/project/todo_api
RUN pip --no-cache-dir install poetry

COPY ./pyproject.toml /opt/project
COPY poetry.lock /opt/project

RUN cd /opt/project && poetry install --no-dev

COPY ./todo_api /opt/project/todo_api
COPY ./todo_api.yml /opt/project/todo_api.yml

WORKDIR /opt/project

ENTRYPOINT poetry run python -m aiohttp.web todo_api.main:main
4

3 回答 3

7

替代方法

不要安装poetry到您的部署环境中。它是一个包管理工具,旨在改进库的开发和协作。如果你想部署一个应用程序,你只需要一个包安装程序(阅读:pip)——关于构建过程和虚拟环境的固执己见poetry是有害的,而不是有益的。

在这种情况下,您要复制到 docker 映像中的工件是1)您最近构建的库和2)已测试依赖项的操舵室,如poetry.lock.

第一个很容易,运行poetry build -f wheel,你有一个很好的便携轮。第二个还不容易,因为poetry不支持建造操舵室(也许永远不会),并且pip wheel不接受poetry.lock的文件格式。因此,如果您想走这条路,您需要使用支持的 beta 版本poetryv1.0.0b7相当稳定)poetry export -f requirements.txt > requirements.txt,它可以让您创建一个requirements.txt与当前锁定文件等效的文件。

一旦你得到它,你可以运行pip wheel -w dist -r requirements.txt最后,你完成了为 docker 镜像创建所有的工件。现在,以下将起作用:

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(可能是相关的,因为它仍然是<v1.0
  • 您跳过服务器中的 virtualenv 并将所有内容直接安装到系统中(您可能仍然选择自己创建一个 virtualenv 并将您的应用程序安装到其中,因为将您的应用程序安装到系统 python 的侧包中可能会导致问题
  • 您的安装步骤不会针对 pypi 运行,因此可以保证此部署在您测试时可以正常工作(这是许多业务设置中非常重要的一点)

缺点

  • 每次都是手工做有点痛苦,这里的目标执行者应该是CI/CD而不是人
  • 如果您的工作站架构和 docker 映像不同,您构建和复制的轮子可能不兼容
于 2019-08-06T10:51:29.113 回答
2

以下对我有用:

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
于 2019-08-02T21:14:24.210 回答
0

如果您想在生产中使用 pip3 安装它,最新版本的 Poetry(2021 年末)如何导出 requirements.txt 文件:

# Production with no development dependencies
poetry export --no-interaction --no-ansi --without-hashes --format requirements.txt --output ./requirements.prod.txt

# For development, including development dependencies
poetry export --no-interaction --no-ansi --without-hashes --format requirements.txt --dev --output ./requirements.dev.txt
于 2021-12-07T20:58:51.393 回答