使用docker部署poetry管理的项目

前言

poetry是和virtualenv pipenv一样的包管理工具, 其使用方式类似于npm
我们使用poetry创建虚拟环境后会生成poetry.lockpyproject.toml两个文件
一般来说我们需要将poetry换成国内源(pyproject.toml 文件追加):

[[tool.poetry.source]]
name = "tsinghua"
url = "https://pypi.tuna.tsinghua.edu.cn/simple"

由于没有requirements.txt, 我们该如何使用Dockerfile部署服务呢?
很简单, 在创建容器的时候生成即可.

Dockerfile格式

FROM python:3.9

# 创建工作目录
RUN mkdir -p /code

# 指定pip源, 因为需要安装poetry
COPY pip.conf /root/.pip/pip.conf

# 将当前工作目录设置为 /code
WORKDIR /code

# 添加文件到容器中
ADD . /code

# 安装poetry
RUN pip install poetry
# 生成requirements.txt
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes

# 正式安装依赖
# !!! 注意--trusted-host 的值为tool.poetry.source的域名
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt --trusted-host pypi.tuna.tsinghua.edu.cn

# 运行服务
CMD ["python", "main.py"]

注, pip.conf形如:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn
posted @ 2022-03-07 16:11  403·Forbidden  阅读(988)  评论(0编辑  收藏  举报