将 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
环境变量.安装人员会尊重它- 我不喜欢将互联网上的东西通过管道传输到我的容器中,而不受任何可能的文件修改的保护
poetry
version might get an update and it will break your build. In this case you can specifyPOETRY_VERSION
environment 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-interaction
not to ask any interactive questions--no-ansi
flag 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
相关文章