在 Docker 上使用需求文件构建 pipenv

Building pipenv with requirements file on Docker

我在构建我的 flask 应用程序的 docker 化版本时遇到了很大的麻烦。 起初我无法从容器本身提供的 requirements.txt 文件安装任何依赖项。这是文件夹结构:

.
├── app
│   ├── ASRModule.py
│   ├── auth.py
│   ├── config
│   ├── files
│   ├── index.py
│   ├── __init__.py
│   ├── Interval.py
│   ├── MySQLDBHandler.py
│   ├── __pycache__
│   ├── SIPCall.py
│   ├── SOAPClient.py
│   ├── static
│   ├── stats.py
│   ├── templates
│   ├── TrunkOccupation.py
│   └── TrunkTraffic.py
├── Dockerfile
├── instance
└── requirements.txt

还有我要在其上构建容器的 Dockerfile:

FROM python:3.5.2-alpine

COPY . /flask
WORKDIR /flask

RUN pip install --upgrade pip
RUN pip install pipenv

CMD ["pipenv", "shell", "testshell"]
CMD ["pipenv","install", "-r ./requirements.txt"]

根据我的理解,在完成构建后我应该有相同的文件夹结构,除了上面一个名为 flask 的目录,它将保存所有上述文件和目录。我还应该有一个名为 testshell 的 virtualenv,应该安装来自 requirements.txt 的所有依赖项。到目前为止,一切都如丝般顺滑。 然而,令我失望的是,在我尝试 运行 这个容器后,我看到了一个正确构建的虚拟环境和这样的错误:

Requirements file doesn't appear to exist. Please ensure the file exists in your project directory or you provided the correct path.

我为需求文件尝试了各种路径,但没有任何帮助。 如果能指出我犯错的地方,我将不胜感激。

错误相当简单,但很难被 错误 错误消息注意到 - 它没有说明 哪个 文件试图加载。它 尝试 加载的文件是 '/flask/ ./requirements.txt',即 requirements.txt 在名为 space[= 的子目录中39=]. - c.f。来自 pip 的错误消息:

% "pip" "install" "-r ./requirements.txt"
Could not open requirements file: 
[Errno 2] No such file or directory: ' ./requirements.txt'

解决方法是 删除 space,或者正确拆分参数:

CMD ["pipenv", "install", "-r./requirements.txt"]

CMD ["pipenv", "install", "-r", "./requirements.txt"]

两者都应该有效。


我建议您向pipenv issue tracker抱怨错误信息。