【问题标题】:How to include Python packages in docker file?如何在 docker 文件中包含 Python 包?
【发布时间】:2019-10-19 04:24:56
【问题描述】:

我正在尝试在我的 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 新手,我不确定自己做错了什么。感谢所有提前回复的人。

【问题讨论】:

  • 你可以在你刚刚用docker run --rm -it myimagename sh构建的镜像中得到一个shell。文件streaming_integration_test.py 是否在您期望的位置?
  • @DavidMaze 我将如何查看文件是否在应有的位置?它被正确放置在我期望它在容器之外的位置。
  • 这是你的形象。通常应用程序会在图像中使用/app 目录,但这根本不是一个硬性规定。请记住,图像中的唯一内容是您复制(或添加,但通常更喜欢复制)的内容。一旦你有了调试 shell,你就可以使用普通的 shell 命令来查看它。

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


【解决方案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 容器中(类似于 unix 系统上的mount 命令) :

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-03
    • 2019-04-26
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    相关资源
    最近更新 更多