Integrating Python Poetry with Docker(将 Python 诗歌与 Docker 集成)
问题描述
你能给我一个 Dockerfile 的例子,我可以从 poetry.lock 和 pyproject.toml 从 Docker 进入我的镜像/容器?
Can you give me an example of a Dockerfile in which I can install all the packages I need from poetry.lock and pyproject.toml into my image/container from Docker?
推荐答案
将 poetry 与 docker 一起使用时要记住几件事.
There are several things to keep in mind when using poetry together with docker.
poetry的官方安装方式是通过:
Official way to install poetry is via:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
这种方式允许 poetry 及其依赖项与您的依赖项隔离.但是,在我看来,这不是一件好事,原因有两个:
This way allows poetry and its dependencies to be isolated from your dependencies. But, in my point of view, it is not a very good thing for two reasons:
poetry版本可能会得到更新,它会破坏你的构建.在这种情况下,您可以指定POETRY_VERSION环境变量.安装人员会尊重它- 我不喜欢将互联网上的东西通过管道传输到我的容器中,而不受任何可能的文件修改的保护
poetryversion might get an update and it will break your build. In this case you can specifyPOETRY_VERSIONenvironment variable. Installer will respect it- I do not like the idea to pipe things from the internet into my containers without any protection from possible file modifications
所以,我使用 pip install 'poetry==$POETRY_VERSION'.如您所见,我仍然建议您固定您的版本.
So, I use pip install 'poetry==$POETRY_VERSION'. As you can see, I still recommend to pin your version.
另外,将此版本也固定在您的 pyproject.toml 中:
Also, pin this version in your pyproject.toml as well:
[build-system]
# Should be the same as `$POETRY_VERSION`:
requires = ["poetry>=1.0"]
build-backend = "poetry.masonry.api"
它将保护您免受本地和 docker 环境之间的版本不匹配.
It will protect you from version mismatch between your local and docker environments.
我们希望缓存我们的需求,并且仅在 pyproject.toml 或 poetry.lock 文件更改时重新安装它们.否则构建会很慢.为了实现工作缓存层,我们应该把:
We want to cache our requirements and only reinstall them when pyproject.toml or poetry.lock files change. Otherwise builds will be slow. To achieve working cache layer we should put:
COPY poetry.lock pyproject.toml /code/
poetry 安装后,但在添加任何其他文件之前.
After the poetry is installed, but before any other files are added.
接下来要记住的是virtualenv 创建.我们在 docker 中不需要它.它已经被隔离了.因此,我们使用 poetry config virtualenvs.create false 设置将其关闭.
The next thing to keep in mind is virtualenv creation. We do not need it in docker. It is already isolated. So, we use poetry config virtualenvs.create false setting to turn it off.
如果你和我一样在开发和生产中使用相同的Dockerfile,你需要根据一些环境变量安装不同的依赖集:
If you use the same Dockerfile for both development and production as I do, you will need to install different sets of dependencies based on some environment variable:
poetry install $(test "$YOUR_ENV" == production && echo "--no-dev")
这种方式 $YOUR_ENV 将控制将安装哪些依赖项集:全部(默认)或仅带有 --no-dev 标志的生产.
This way $YOUR_ENV will control which dependencies set will be installed: all (default) or production only with --no-dev flag.
您可能还想添加更多选项以获得更好的体验:
You may also want to add some more options for better experience:
--no-interaction不问任何交互式问题--no-ansi标志,使您的输出更加日志友好
--no-interactionnot to ask any interactive questions--no-ansiflag to make your output more log friendly
结果
你最终会得到类似的东西:
Result
You will end up with something similar to:
FROM python:3.6.6-alpine3.7
ARG YOUR_ENV
ENV YOUR_ENV=${YOUR_ENV}
PYTHONFAULTHANDLER=1
PYTHONUNBUFFERED=1
PYTHONHASHSEED=random
PIP_NO_CACHE_DIR=off
PIP_DISABLE_PIP_VERSION_CHECK=on
PIP_DEFAULT_TIMEOUT=100
POETRY_VERSION=1.0.0
# System deps:
RUN pip install "poetry==$POETRY_VERSION"
# Copy only requirements to cache them in docker layer
WORKDIR /code
COPY poetry.lock pyproject.toml /code/
# Project initialization:
RUN poetry config virtualenvs.create false
&& poetry install $(test "$YOUR_ENV" == production && echo "--no-dev") --no-interaction --no-ansi
# Creating folders, and files for a project:
COPY . /code
您可以在此处找到一个完整的真实示例:wemake-django-template
You can find a fully working real-life example here: wemake-django-template
- 更新
诗歌到1.0
这篇关于将 Python 诗歌与 Docker 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 Python 诗歌与 Docker 集成
基础教程推荐
- 包装空间模型 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
