如何在docker文件中包含Python包?

时间:2019-06-03 15:23:40

标签: python-3.x docker dockerfile python-packaging

我正在尝试在我的docker文件中包括整个目录。这是我当前的Dockerfile:

FROM python:3

COPY requirements.txt ./

RUN pip install -r requirements.txt

ADD streaming_integration_test.py /

CMD python ./streaming_integration_test.py

但是,在构建此docker文件并运行它之后,出现以下错误:

     File "./streaming_integration_test.py", line 3, in <module>
    from data_streamer.file_utilities import FileUtilities
ModuleNotFoundError: No module named 'data_streamer'

file_utilities.py是软件包的一部分,并且位于目录data_streamer中,我是Docker的新手,我不确定自己在做什么错。感谢所有提前答复的人。

1 个答案:

答案 0 :(得分:1)

FROM python:3                               # pull filesystem    
COPY requirements.txt ./                    # copy single file
RUN pip install -r requirements.txt         # run command
ADD streaming_integration_test.py /         # add single file
CMD python ./streaming_integration_test.py  # run command on "docker run"ň

所以您还需要添加以下内容:

COPY ./data_streamer /data_streamer         # copy folder

它将文件夹(及其内容)复制到新的图像层中,或使用docker run命令将文件夹(在主机系统上)作为卷安装在Docker容器中(类似于mount命令)在Unix系统上):

# mount host folder `data_streamer` from the current directory (pwd) to `/data_streamer`
docker run --volume $(pwd)/data_streamer:/data_streamer [IMAGE_NAME]