我的目标是对 python 3.6 网络应用程序进行 docker 化。 在我的工作期间,我开发了我的 Web 应用程序正在使用的私有(private) Python 包(“VmwareProviderBL”)。
构建我的 docker 镜像运行良好,但是当我尝试运行它时,我收到一条错误消息,提示找不到我的私有(private) Python 包。
我的 Dockerfile
# Use an official Python runtime as a parent image
FROM python:3.6-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run WebController.py when the container launches
CMD ["python", "WebApi/WebController.py"]
尝试运行图像时出现错误
Traceback (most recent call last):
File "WebApi/WebController.py", line 3, in <module>
from VmwareProviderBL import SnapshotBL, MachineBL, Utils
ModuleNotFoundError: No module named 'VmwareProviderBL'
我的包层次结构
--VmwareVirtualMachineProvider
--WebApi
--VmwareProviderBL
--requirements
--Dockerfile
有什么想法吗? 我知道我需要以某种方式将这个包添加到我的 Dockerfile 中,但没有在网上找到任何示例。
请您参考如下方法:
当您导入 一个模块时,Python 会查找 (a) 内置路径 (sys.path),(b) PYTHONPATH 中的位置 变量,以及 (c) 在包含您正在运行的脚本的目录中。
您正在为您的模块安装要求 (pip install -r requirements.txt),但您从未安装VmwareProviderBL 模块本身。这意味着它不会在上面的 (a) 中可用。您正在运行的脚本 (WebApi/WebController.py) 与 VmwareProviderBL 模块不在同一目录中,因此排除了 (c)。
解决此问题的最佳方法是在您的项目中包含一个 setup.py 文件,这样您就可以简单地 pip install . 安装要求和模块本身。
您还可以修改 PYTHONPATH 环境变量以包含包含 VmwareProviderBL 模块的目录。例如,添加到您的 Dockerfile:
ENV PYTHONPATH=/app
