猿问

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

我正在使用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"


和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



繁星点点滴滴
浏览 290回答 2
2回答

蓝山帝景

替代方法不要安装poetry到您的部署环境中。它是一个包管理工具,旨在改进库的开发和协作。如果你想部署一个应用程序,你只需要一个包安装程序(阅读:pip)——关于构建过程和虚拟环境的固执己见poetry是有害的,而不是有益的。在这种情况下,您要复制到 docker 映像中的工件是1)您最近构建的库和2)已测试依赖项的操舵室,如poetry.lock.第一个很容易,运行poetry build -f wheel,你有一个很好的便携轮。第二个还不容易,因为poetry不支持建造操舵室(也许永远不会),并且pip wheel不接受poetry.lock的文件格式。因此,如果您想走这条路,您需要使用支持的 beta 版本poetry(v1.0.0b7相当稳定)poetry export -f requirements.txt > requirements.txt,它可以让您创建一个requirements.txt与当前锁定文件等效的文件。一旦你得到它,你可以运行pip wheel -w dist -r requirements.txt,最后,你完成了为 docker 镜像创建所有的工件。现在,以下将起作用:FROM python:3.7.1-alpineWORKDIR /opt/projectCOPY dist distRUN pip install --no-index --find-links dist todo_apiENTRYPOINT python -m aiohttp.web todo_api.main:main优点在您的服务器中没有不必要的依赖poetry(可能是相关的,因为它仍然是<v1.0)您跳过服务器中的 virtualenv 并将所有内容直接安装到系统中(您可能仍然选择自己创建一个 virtualenv 并将您的应用程序安装到其中,因为将您的应用程序安装到系统 python 的侧包中可能会导致问题)您的安装步骤不会针对 pypi 运行,因此可以保证此部署在您测试时可以正常工作(这是许多业务设置中非常重要的一点)缺点每次都是手工做有点痛苦,这里的目标执行者应该是CI/CD而不是人如果您的工作站架构和 docker 映像不同,您构建和复制的轮子可能不兼容

白衣非少年

以下对我有用:FROM python:3.7.1-alpineWORKDIR /opt/projectRUN pip install --upgrade pip && pip --no-cache-dir install poetryCOPY ./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/virtualenvsUpdating dependenciesResolving dependencies...Writing lock filePackage operations: 15 installs, 0 updates, 0 removals&nbsp; - Installing idna (2.8)&nbsp; - Installing multidict (4.5.2)&nbsp; - Installing six (1.12.0)&nbsp; - Installing async-timeout (3.0.1)&nbsp; - Installing attrs (18.2.0)&nbsp; - Installing chardet (3.0.4)&nbsp; - Installing marshmallow (2.19.5)&nbsp; - Installing pymongo (3.8.0)&nbsp; - Installing python-dateutil (2.8.0)&nbsp; - Installing yarl (1.3.0)&nbsp; - Installing aiohttp (3.5.4)&nbsp; - Installing asyncio (3.4.3)&nbsp; - Installing motor (2.0.0)&nbsp; - Installing pyyaml (3.13)&nbsp; - Installing umongo (2.1.0)Removing intermediate container c6a9c7652b5c&nbsp;---> 89354562cf16Successfully built 89354562cf16Successfully tagged 57331667:latest
随时随地看视频慕课网APP

相关分类

Python
我要回答