将 Docker 与 Python 诗歌一起使用?

Posted

技术标签:

【中文标题】将 Docker 与 Python 诗歌一起使用?【英文标题】:Using Docker with Python Poetry? 【发布时间】:2022-01-11 16:42:35 【问题描述】:

我一直在使用 Docker 和 pipenv 进行数据科学部署设置,现在我想改用 Poetry。我的 Dockerfile 是:

FROM python:3.8-alpine3.13

ENV POETRY_VIRTUALENVS_CREATE=false \
    POETRY_VERSION=1.1.11

RUN apk add --no-cache python3-dev gcc libc-dev musl-dev openblas gfortran build-base postgresql-libs postgresql-dev libffi-dev \
  && pip install poetry
COPY pyproject.toml poetry.lock ./
RUN poetry export -f requirements.txt --output requirements.txt && sed -i 's/^-e //' requirements.txt

USER root
RUN apt-get update && apt-get install -y --no-install-recommends python3-dev gcc libc-dev musl-dev openssh-client git libpq-dev \
  && apt-get clean -y


# install dependencies from requirements.txt
RUN pip install --no-cache-dir --user -r requirements.txt

COPY . .
CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"]

我的pyproject.toml

[tool.poetry]
name = ""
version = "1.0.0"
description = ""
authors = [""]

[tool.poetry.dependencies]
python = "^3.8"

... # lots of libraries, omitted here

[tool.poetry.dev-dependencies]
black = "*"
ipykernel = "6.*"
ipython = "7.*"
isort = "5.*"
jupyter = "*"
pytest = "6.*"
pre-commit = "2.*"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

这是基于我在 *** 上找到的其他 Dockerfile。我遇到了以下问题:

Step 7/10 : RUN apt-get update && apt-get install -y --no-install-recommends python3-dev gcc libc-dev musl-dev openssh-client git libpq-dev   && apt-get clean -y
 ---> Running in 447ffb83d555
/bin/sh: apt-get: not found
The command '/bin/sh -c apt-get update && apt-get install -y --no-install-recommends python3-dev gcc libc-dev musl-dev openssh-client git libpq-dev   && apt-get clean -y' returned a non-zero code: 127
Running containerdocker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "jupyter": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled 
make: *** [Makefile:6: jupyter_notebook] Error 127

所以这看起来像没有使用 Poetry,安装了 Jupyter,因此找不到它。我该如何解决这个问题?

【问题讨论】:

为什么不从现有的 jupyter 图像开始? @OneCricketeer 因为它们包含很多包,我不需要其中的一半 【参考方案1】:

这不是诗歌的问题,而是因为您尝试在高山映像中使用apt-get 安装软件包(不包括apt-get)。您还多次安装某些软件包,因为您首先使用 apk,然后使用 apt-get。 你也不需要安装 python,因为你使用的是 python 图像作为基础。

我还建议只使用诗歌进行安装,而不是转储到 requirements.txt 并使用官方安装程序安装诗歌。由于不支持 python 3.8,因此您还必须碰撞 python。

看看这个结合 docker 和 Poetry 的答案:Integrating Python Poetry with Docker

您的 Dockerfile 可能看起来像这样:

FROM python:3.9-alpine3.13

ENV POETRY_VIRTUALENVS_CREATE=false \
    POETRY_VERSION=1.1.11 \
    YOUR_ENV=development

RUN apk add --no-cache python3-dev gcc libc-dev musl-dev openblas gfortran build-base postgresql-libs postgresql-dev libffi-dev curl

RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python -
ENV PATH "/root/.local/bin:$PATH"


# install dependencies
COPY pyproject.toml poetry.lock ./
RUN poetry install $(test "$YOUR_ENV" == production && echo "--no-dev") --no-interaction --no-ansi

COPY . .
CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"]

【讨论】:

感谢您提供许多有用的建议。然而,虽然这似乎适用于普通的 Jupyter,但我在添加实际包(例如 NumPy、PyTorch)时遇到了 2 个问题。首先,与 pipenv 相比,安装需要永远,这与我想要实现的完全相反,大约 15 m 而不是不到 5 分钟(与我在其他项目中看到的完全相反)。其次,我收到错误,但它不起作用。完整堆栈跟踪:pastebin.com/5HSQyVvV 为了摆脱错误,我不得不使用 Jupyter Docker Stack 不管怎样,您可能想尝试使用基于 debian 的映像,例如 3.9-slim。根据以往的经验,有很多 python 包没有预编译的 Alpine ***,所以你最终需要下载和编译源代码。因此,我的 alpine 图像构建速度较慢,并且比 slim 图像大。我还建议设置POETRY_HOME【参考方案2】:

根据 Asgeer 的回答,我设法修复了 Dockerfile。然而,使用 Alpine 是不可能的,最终的解决方案是:

FROM jupyter/scipy-notebook:latest

ENV POETRY_VIRTUALENVS_CREATE=false \
    POETRY_VERSION=1.1.11

USER root
RUN apt-get update \
    && apt-get --yes install apt-utils \
    && apt-get --yes install curl

RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python -
ENV PATH "/home/jovyan/.local/bin:$PATH"

# install dependencies
COPY pyproject.toml poetry.lock ./
RUN poetry install --no-interaction --no-ansi

COPY . .
CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"]

【讨论】:

以上是关于将 Docker 与 Python 诗歌一起使用?的主要内容,如果未能解决你的问题,请参考以下文章

如何将定制的 docker 容器与无服务器框架一起使用

将 nodemon 与 docker 和 docker-compose 一起使用

将 docker 插件与 sbt 一起使用

如何将 Nomad 与 Nvidia Docker 一起使用?

Docker:如何将 bash 与基于 Alpine 的 docker 映像一起使用?

使用 Python 诗歌的安装后脚本