为啥 pytorch Docker 镜像没有自带torch?

Posted

技术标签:

【中文标题】为啥 pytorch Docker 镜像没有自带torch?【英文标题】:Why does the pytorch Docker image not come with torch?为什么 pytorch Docker 镜像没有自带torch? 【发布时间】:2019-11-17 02:52:16 【问题描述】:

我正在尝试构建一个 docker 容器,使我能够运行需要 python torch 模块的代码。

我选择从pytorch/pytorch:latest 基础镜像开始我的镜像并手动添加一些必需的模块。构建、推送和拉取到远程服务器成功(至少没有错误消息)。

目前我的 Dockerfile 看起来像这样:

FROM pytorch/pytorch:latest

RUN apt-get update \
     && apt-get install -y \
        libgl1-mesa-glx \
        libx11-xcb1 \
     && apt-get clean all \
     && rm -r /var/lib/apt/lists/*

RUN /opt/conda/bin/conda install --yes \
    astropy \
    matplotlib \
    pandas \
    glob2 \
    PIL \
    scikit-learn \
    scikit-image \
    numpy

但是,在容器中运行我的 python 脚本时,我得到ImportError: No module named torch。这让我觉得很奇怪,因为它让我假设 pytorch 基础映像不包含 torch 模块...?

尽管如此,我还是尝试将 torch 添加到要在 Dockerfile 中安装的模块列表中,但是初始构建已经失败并出现错误消息 PackagesNotFoundError: The following packages are not available from current channels: - torch。不幸的是,遵循here 的建议对我没有帮助。

我很感激任何解释为什么在构建的容器中找不到 torch 模块,当然还有任何帮助来解决这个问题!谢谢!

【问题讨论】:

你试过了吗? 是的,我尝试了通过 pip 将 Torch 安装到容器中的方法,效果很好。但是,我需要它在图像中更易于访问,以便随机用户不必手动将 Torch 安装到每个启动的容器中 Sara 看到我的答案,torch 将成为您的 dockerfile 的一部分,例如 ` RUN /opt/conda/bin/conda install --yes \ astropy \ matplotlib \ pandas \ glob2 \ scikit-learn \ scikit -image\numpy\torch` 【参考方案1】:

首先,您的假设是错误的,要验证这一点,您只需从基础镜像运行容器,您可以查看官方的Dockerfile 或首先运行基础镜像 pytorch/pytorch:最新 并验证基础映像是否按您的需要工作?

这是官方镜像中已安装模块的列表,在列表底部,您可以看到火炬。

这是一个使用基础图像的 torch 的简单示例。

至于您的 Dockerfile,所以包 PIL 正在从头开始破坏 docker 构建,但如果 PyTorch 是基础映像,则这不可见。

由于某种原因,我在子图像中找不到火炬,所以使用 pip install 安装它,然后它就可以工作了。

这里是Dockerfile

FROM pytorch/pytorch:latest

RUN apt-get update \
     && apt-get install -y \
        libgl1-mesa-glx \
        libx11-xcb1 \
     && apt-get clean all \
     && rm -r /var/lib/apt/lists/*

RUN /opt/conda/bin/conda install --yes \
    astropy \
    matplotlib \
    pandas \
    glob2 \
    scikit-learn \
    scikit-image \
    numpy \ 
    torch

更新

这里是让torch可用的方法

FROM pytorch/pytorch:latest

RUN apt-get update \
     && apt-get install -y \
        libgl1-mesa-glx \
        libx11-xcb1 \
     && apt-get clean all \
     && rm -r /var/lib/apt/lists/*

RUN /opt/conda/bin/conda install --yes \
    astropy \
    matplotlib \
    pandas \
    scikit-learn \
    scikit-image 

RUN pip install torch

【讨论】:

@sara actullay on 这些astropy \ matplotlib \ pandas \ glob2 \ scikit-learn \ scikit-image \ numpy \ torch 在这些模块上更新了打破一切的python版本 谢谢Adiii,我假设您在使用pip 安装torch 时在容器中?我是否正确理解这一点,然后镜像中将不提供torch,但您每次都需要在容器中手动安装torch? 不,你不需要每次都安装,你可以看到我在你的dockerfile中添加了torch 我的问题是我收到错误消息:PackagesNotFoundError: The following packages are not available from current channels: - torch(请参阅我的问题中的倒数第二段)每当我在 Dockerfile 的包列表中包含 torch 时。 不,它对我来说工作正常,但图像尺寸很大可能是由于网速慢的问题

以上是关于为啥 pytorch Docker 镜像没有自带torch?的主要内容,如果未能解决你的问题,请参考以下文章

Docker: 使用jupyter notebook基础镜像搭建自己的 pytorch 开发环境

PyTorch:NVIDIA NGC 映像还是 Docker Hub 映像?

为啥修改docker镜像源为国内文件

为啥“docker”不能删镜像?

docker镜像为啥有自己的ip

Docker镜像