简体   繁体   English

如何在Docker镜像构建中安装私有Python包

[英]How to Install Private Python Package in Docker image build

my goal is to dockerize python 3.6 web application. 我的目标是dockerize python 3.6 Web应用程序。 During my work I developed private python package ('VmwareProviderBL') that my web application is using. 在我的工作中,我开发了我的Web应用程序正在使用的私有python包('VmwareProviderBL')。

Build my docker image working perfectly, but when I am trying to run it, I get an error saying my private Python package is not found. 构建我的docker镜像工作完美,但是当我尝试运行它时,我收到一条错误,说我找不到我的私有Python包。

My Dockerfile 我的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"]

My Error when trying to run the image 尝试运行图像时出现错误

Traceback (most recent call last):
  File "WebApi/WebController.py", line 3, in <module>
    from VmwareProviderBL import SnapshotBL, MachineBL, Utils
ModuleNotFoundError: No module named 'VmwareProviderBL'

My package hierarchy 我的包层次结构

--VmwareVirtualMachineProvider
  --WebApi
  --VmwareProviderBL
  --requirements
  --Dockerfile

Any ideas anyone? 任何人的想法? I know I need somehow to add this package to my Dockerfile, did not find any example online. 我知道我需要以某种方式将此包添加到我的Dockerfile,没有在网上找到任何示例。

When you import a module, Python looks in (a) the built-in path ( sys.path ), (b) locations in the PYTHONPATH variable, and (c) in the directory containing the script you're running. import模块时,Python会查找(a)内置路径( sys.path ),(b) PYTHONPATH变量中的位置,以及(c)包含您正在运行的脚本的目录。

You're installing the requirements for your module ( pip install -r requirements.txt ), but you're never installing the VmwareProviderBL module itself. 您正在安装模块的需求pip install -r requirements.txt ),但您从不安装VmwareProviderBL模块本身。 This means it's not going to be available in (a) above. 这意味着它不会在上面的(a)中提供。 The script you're running ( WebApi/WebController.py ) isn't located in the same directory as the VmwareProviderBL module, so that rules out (c). 您正在运行的脚本( WebApi/WebController.py )与VmwareProviderBL模块不在同一目录中,因此排除了(c)。

The best way of solving this problem would be to include a setup.py file in your project so that you could simply pip install . 解决此问题的最佳方法是在项目中包含setup.py文件,以便您可以简单地进行pip install . to install both the requirements and the module itself. 安装需求和模块本身。

You could also modify the PYTHONPATH environment variable to include the directory that contains the VmwareProviderBL module. 您还可以修改PYTHONPATH环境变量以包含包含VmwareProviderBL模块的目录。 For example, add to your Dockerfile: 例如,添加到Dockerfile:

ENV PYTHONPATH=/app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM