Docker搭配Python Poetry使用时apt缺失与Jupyter找不到问题咨询
问题根因分析
- 第一个报错
apt-get: not found:你使用的基础镜像是python:3.8-alpine3.13,Alpine Linux的包管理器是apk而非Debian/Ubuntu体系的apt-get,这一步执行失败会直接中断镜像构建流程,后续的依赖安装步骤根本没有运行。 - 第二个报错找不到Jupyter:有三个核心原因
- 构建流程被前面的apt-get报错打断,pip安装依赖的步骤完全没有执行
poetry export默认不会导出[tool.poetry.dev-dependencies]下的包,Jupyter属于开发依赖,导出的requirements.txt里本身就没有Jupyter- 你使用了
pip install --user参数,依赖会安装到当前用户的家目录下,默认不会加入系统PATH,就算安装完成也找不到可执行文件
修复后的Dockerfile
FROM python:3.8-alpine3.13 ENV POETRY_VIRTUALENVS_CREATE=false \ POETRY_VERSION=1.1.11 # 统一使用apk安装所有需要的系统依赖 RUN apk add --no-cache python3-dev gcc libc-dev musl-dev openblas gfortran build-base postgresql-libs postgresql-dev libffi-dev openssh-client git # 安装指定版本的Poetry RUN pip install "poetry==$POETRY_VERSION" COPY pyproject.toml poetry.lock ./ # 导出依赖时添加--dev参数包含开发依赖,才能把Jupyter导出到requirements.txt RUN poetry export -f requirements.txt --output requirements.txt --dev --without-hashes && sed -i 's/^-e //' requirements.txt # 全局安装依赖,去掉--user避免PATH问题 RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"]
可选优化建议
你可以省去导出requirements.txt的步骤,直接用Poetry安装依赖,更符合Poetry的使用逻辑:
# 替换导出requirements+pip安装的两个步骤 RUN poetry install --no-root --no-interaction
你已经设置了POETRY_VIRTUALENVS_CREATE=false,Poetry会直接把依赖安装到系统Python环境中,不需要额外处理PATH问题。
内容的提问来源于stack exchange,提问作者qalis
相关产品推荐
相关产品推荐

