2

I am trying to build a docker container that will enable me to run my code that requires the python torch module.

I have chosen to start my image from the pytorch/pytorch:latest base image and add a few required modules manually. The build, push, and pull to the remote server were successful (no error messages at least).

Currently my Dockerfile looks like this:

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

However, when running my python script within the container, I get ImportError: No module named torch. This strikes me as rather weird, as it leads me to assume that the pytorch base image does not include the torch module...?

I have nevertheless tried to add torch to the list of modules to install in the Dockerfile, but then the initial build will already fail with the error message PackagesNotFoundError: The following packages are not available from current channels: - torch. Following the advice given here did unfortunately not help me.

I'd appreciate any explanation as to why the torch module could not be found within the built container, and of course any help to fix this problem! Thanks!

sara
  • 135
  • 3
  • 14
  • did you try this? – Adiii Jul 08 '19 at 04:03
  • yes, I tried your approach of installing torch via pip into the container, and it worked fine. However, I need it to rather be accessible in the image, so that a random user does not have to manually install torch into each started container – sara Jul 08 '19 at 08:41
  • Sara see my answers, torch will be a part of your dockerfile like ` RUN /opt/conda/bin/conda install --yes \ astropy \ matplotlib \ pandas \ glob2 \ scikit-learn \ scikit-image \ numpy \ torch` – Adiii Jul 08 '19 at 10:02

1 Answers1

2

First thing, your assumption is false, to verify this you just need to run the container from the base image, as you can check the official Dockerfile or run first the base image that is pytorch/pytorch:latest and verify does the base image working as you need?

Here is the list of installed modules in the official image and at bottom of the list, you can see the torch.

enter image description here

Here is a simple example from the torch using the base image.

enter image description here

As for your Dockerfile, so the package PIL is breaking the docker build from scratch, but this not visible if PyTorch is the base image.

enter image description here

For some reason, I am failed in the child image to find the torch so installed it using pip install and then its able to work.

enter image description here

Here is the 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

Updated

Here is the way to make torch available

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

enter image description here

Exploring
  • 2,493
  • 11
  • 56
  • 97
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • @sara actullay on of these `astropy \ matplotlib \ pandas \ glob2 \ scikit-learn \ scikit-image \ numpy \ torch` on these modules update the python version which break every thing – Adiii Jul 07 '19 at 10:50
  • Thanks Adiii, I am assuming that you are in the container when you are installing torch with pip? Do I understand this correctly, that torch will then not be available in the image, but that you will need to install torch manually in the container every time? – sara Jul 08 '19 at 08:19
  • No you don't need install every time, you can see that I add torch in your dockerfile – Adiii Jul 08 '19 at 10:01
  • My problem with this is that I get the error message: ```PackagesNotFoundError: The following packages are not available from current channels: - torch``` (see second to last paragraph in my question) whenever I include torch in the list of packages in the Dockerfile. – sara Jul 08 '19 at 10:23
  • My Dockerfile looks exactly like the one you posted above. Whenever I then run ```docker build -t .```, I get the error mentioned above. I have tried following the explanation posted here: https://stackoverflow.com/questions/48493505/packagesnotfounderror-the-following-packages-are-not-available-from-current-cha, but this did not work for me. I thought this might be because it is not explained for containers, but for the normal PC environment. – sara Jul 08 '19 at 10:48
  • @sara done, you were right, there was error using conda installation – Adiii Jul 08 '19 at 10:57
  • Ah yes, thanks for the clarification! I've now managed to build and push successfully, however when pulling to the remote server I get a segmentation fault when pulling some of the layers (alwyas the same). Do you also have this issue? – sara Jul 08 '19 at 15:09
  • 1
    No, it's working fine with me, but the image size is heavy may be due to slow internet issue – Adiii Jul 08 '19 at 15:38