Dockerfile RUN merge pip commands












-1















I had an old dockerfile which looks like this



FROM ubuntu:16.04
ENV VISUAL=vim
ENV EDITOR=$VISUAL
ENV TERM=xterm
ENV TERMINFO=/etc/terminfo
ENV PYTHONIOENCODING=utf-8
RUN apt-get --yes update && apt-get --yes upgrade && apt-get --yes install python
python-dev
python-pip
<...lots of other apt-get install...>
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
<...other staffs>


It worked well, but I want to reduce image size by reducing layers. So I merged the last 2 lines



RUN pip install --upgrade pip && 
pip install -r requirements.txt


But the build fails...



Step 15/45 : RUN pip install --upgrade pip &&  pip install -r requirements.txt
---> Running in b96971e60263
Collecting pip
Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
Installing collected packages: pip
Found existing installation: pip 8.1.1
Not uninstalling pip at /usr/lib/python2.7/dist-packages, outside environment /usr
Successfully installed pip-18.1
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
from pip import main
ImportError: cannot import name main


What did I miss when I merged those 2 lines?










share|improve this question



























    -1















    I had an old dockerfile which looks like this



    FROM ubuntu:16.04
    ENV VISUAL=vim
    ENV EDITOR=$VISUAL
    ENV TERM=xterm
    ENV TERMINFO=/etc/terminfo
    ENV PYTHONIOENCODING=utf-8
    RUN apt-get --yes update && apt-get --yes upgrade && apt-get --yes install python
    python-dev
    python-pip
    <...lots of other apt-get install...>
    RUN pip install --upgrade pip
    RUN pip install -r requirements.txt
    <...other staffs>


    It worked well, but I want to reduce image size by reducing layers. So I merged the last 2 lines



    RUN pip install --upgrade pip && 
    pip install -r requirements.txt


    But the build fails...



    Step 15/45 : RUN pip install --upgrade pip &&  pip install -r requirements.txt
    ---> Running in b96971e60263
    Collecting pip
    Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
    Installing collected packages: pip
    Found existing installation: pip 8.1.1
    Not uninstalling pip at /usr/lib/python2.7/dist-packages, outside environment /usr
    Successfully installed pip-18.1
    Traceback (most recent call last):
    File "/usr/bin/pip", line 9, in <module>
    from pip import main
    ImportError: cannot import name main


    What did I miss when I merged those 2 lines?










    share|improve this question

























      -1












      -1








      -1








      I had an old dockerfile which looks like this



      FROM ubuntu:16.04
      ENV VISUAL=vim
      ENV EDITOR=$VISUAL
      ENV TERM=xterm
      ENV TERMINFO=/etc/terminfo
      ENV PYTHONIOENCODING=utf-8
      RUN apt-get --yes update && apt-get --yes upgrade && apt-get --yes install python
      python-dev
      python-pip
      <...lots of other apt-get install...>
      RUN pip install --upgrade pip
      RUN pip install -r requirements.txt
      <...other staffs>


      It worked well, but I want to reduce image size by reducing layers. So I merged the last 2 lines



      RUN pip install --upgrade pip && 
      pip install -r requirements.txt


      But the build fails...



      Step 15/45 : RUN pip install --upgrade pip &&  pip install -r requirements.txt
      ---> Running in b96971e60263
      Collecting pip
      Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
      Installing collected packages: pip
      Found existing installation: pip 8.1.1
      Not uninstalling pip at /usr/lib/python2.7/dist-packages, outside environment /usr
      Successfully installed pip-18.1
      Traceback (most recent call last):
      File "/usr/bin/pip", line 9, in <module>
      from pip import main
      ImportError: cannot import name main


      What did I miss when I merged those 2 lines?










      share|improve this question














      I had an old dockerfile which looks like this



      FROM ubuntu:16.04
      ENV VISUAL=vim
      ENV EDITOR=$VISUAL
      ENV TERM=xterm
      ENV TERMINFO=/etc/terminfo
      ENV PYTHONIOENCODING=utf-8
      RUN apt-get --yes update && apt-get --yes upgrade && apt-get --yes install python
      python-dev
      python-pip
      <...lots of other apt-get install...>
      RUN pip install --upgrade pip
      RUN pip install -r requirements.txt
      <...other staffs>


      It worked well, but I want to reduce image size by reducing layers. So I merged the last 2 lines



      RUN pip install --upgrade pip && 
      pip install -r requirements.txt


      But the build fails...



      Step 15/45 : RUN pip install --upgrade pip &&  pip install -r requirements.txt
      ---> Running in b96971e60263
      Collecting pip
      Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
      Installing collected packages: pip
      Found existing installation: pip 8.1.1
      Not uninstalling pip at /usr/lib/python2.7/dist-packages, outside environment /usr
      Successfully installed pip-18.1
      Traceback (most recent call last):
      File "/usr/bin/pip", line 9, in <module>
      from pip import main
      ImportError: cannot import name main


      What did I miss when I merged those 2 lines?







      python docker pip dockerfile






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 19:06









      SiyuSiyu

      2,99911227




      2,99911227
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Hypothesis: In pip install --upgrade pip, the pip command run is /usr/bin/pip, and when it upgrades pip, it creates a new pip executable at /usr/local/bin/pip. This new executable is what pip install -r requirements.txt is supposed to run, but when you put them in one RUN command and thus run them in a single shell instance, the shell's caching of command locations kicks in, and thus the second pip in pip ... && pip ... ends up being run from the same location as the first, which fails due to changes in pip's internals between the old and new version. You can force the shell to uncache pip's location by inserting hash -d pip in the middle of the single RUN command:



          RUN pip install --upgrade pip && 
          hash -d pip &&
          pip install -r requirements.txt





          share|improve this answer
























          • wow I've never thought of that. I tried hash -d but it says /bin/sh: 1: hash: Illegal option -d. If I remove -d, it works perfectly.

            – Siyu
            Nov 24 '18 at 19:24











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461493%2fdockerfile-run-merge-pip-commands%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Hypothesis: In pip install --upgrade pip, the pip command run is /usr/bin/pip, and when it upgrades pip, it creates a new pip executable at /usr/local/bin/pip. This new executable is what pip install -r requirements.txt is supposed to run, but when you put them in one RUN command and thus run them in a single shell instance, the shell's caching of command locations kicks in, and thus the second pip in pip ... && pip ... ends up being run from the same location as the first, which fails due to changes in pip's internals between the old and new version. You can force the shell to uncache pip's location by inserting hash -d pip in the middle of the single RUN command:



          RUN pip install --upgrade pip && 
          hash -d pip &&
          pip install -r requirements.txt





          share|improve this answer
























          • wow I've never thought of that. I tried hash -d but it says /bin/sh: 1: hash: Illegal option -d. If I remove -d, it works perfectly.

            – Siyu
            Nov 24 '18 at 19:24
















          1














          Hypothesis: In pip install --upgrade pip, the pip command run is /usr/bin/pip, and when it upgrades pip, it creates a new pip executable at /usr/local/bin/pip. This new executable is what pip install -r requirements.txt is supposed to run, but when you put them in one RUN command and thus run them in a single shell instance, the shell's caching of command locations kicks in, and thus the second pip in pip ... && pip ... ends up being run from the same location as the first, which fails due to changes in pip's internals between the old and new version. You can force the shell to uncache pip's location by inserting hash -d pip in the middle of the single RUN command:



          RUN pip install --upgrade pip && 
          hash -d pip &&
          pip install -r requirements.txt





          share|improve this answer
























          • wow I've never thought of that. I tried hash -d but it says /bin/sh: 1: hash: Illegal option -d. If I remove -d, it works perfectly.

            – Siyu
            Nov 24 '18 at 19:24














          1












          1








          1







          Hypothesis: In pip install --upgrade pip, the pip command run is /usr/bin/pip, and when it upgrades pip, it creates a new pip executable at /usr/local/bin/pip. This new executable is what pip install -r requirements.txt is supposed to run, but when you put them in one RUN command and thus run them in a single shell instance, the shell's caching of command locations kicks in, and thus the second pip in pip ... && pip ... ends up being run from the same location as the first, which fails due to changes in pip's internals between the old and new version. You can force the shell to uncache pip's location by inserting hash -d pip in the middle of the single RUN command:



          RUN pip install --upgrade pip && 
          hash -d pip &&
          pip install -r requirements.txt





          share|improve this answer













          Hypothesis: In pip install --upgrade pip, the pip command run is /usr/bin/pip, and when it upgrades pip, it creates a new pip executable at /usr/local/bin/pip. This new executable is what pip install -r requirements.txt is supposed to run, but when you put them in one RUN command and thus run them in a single shell instance, the shell's caching of command locations kicks in, and thus the second pip in pip ... && pip ... ends up being run from the same location as the first, which fails due to changes in pip's internals between the old and new version. You can force the shell to uncache pip's location by inserting hash -d pip in the middle of the single RUN command:



          RUN pip install --upgrade pip && 
          hash -d pip &&
          pip install -r requirements.txt






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 19:19









          jwodderjwodder

          33.6k45684




          33.6k45684













          • wow I've never thought of that. I tried hash -d but it says /bin/sh: 1: hash: Illegal option -d. If I remove -d, it works perfectly.

            – Siyu
            Nov 24 '18 at 19:24



















          • wow I've never thought of that. I tried hash -d but it says /bin/sh: 1: hash: Illegal option -d. If I remove -d, it works perfectly.

            – Siyu
            Nov 24 '18 at 19:24

















          wow I've never thought of that. I tried hash -d but it says /bin/sh: 1: hash: Illegal option -d. If I remove -d, it works perfectly.

          – Siyu
          Nov 24 '18 at 19:24





          wow I've never thought of that. I tried hash -d but it says /bin/sh: 1: hash: Illegal option -d. If I remove -d, it works perfectly.

          – Siyu
          Nov 24 '18 at 19:24




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461493%2fdockerfile-run-merge-pip-commands%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ottavio Pratesi

          Tricia Helfer

          15 giugno