[英]Can't install dependencies in docker container
我正在使用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
不要將poetry
安裝到您的部署環境中。 它是一個包管理工具,旨在改進庫的開發和協作。 如果你想部署一個應用程序,你只需要一個包安裝程序(閱讀: pip
)——關於構建過程和虛擬環境的poetry
的固執立場是有害的,而不是有幫助的。
在這種情況下,您要復制到 docker 映像中的工件是1)您正在處理的庫的最新版本和2)測試依賴項的駕駛室,如由poetry.lock
定義的。
第一個很簡單,運行poetry build -f wheel
並且你有一個非常便攜的輪子。 第二個還不容易,因為poetry
不支持建造駕駛室(也許永遠不會),而pip wheel
不接受poetry.lock
的文件格式。 所以如果你想走這條路,你需要開發一個支持poetry export -f requirements.txt > requirements.txt
的poetry
測試版( v1.0.0b7
相當穩定),它可以讓你創建一個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
)以下對我有用:
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
如果您想在生產中使用 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.