Why dockered centos doesn't recognize pip?

2019-07-25 00:45发布

问题:

I want to create a container with python and few packages over centos. I've tried to run several commands inside raw centos container. Everything worked fine I've installed everything I want. Then I created Dockerfile with the same commands executed via RUN and I'm getting /bin/sh: pip: command not found What could be wrong? I mean the situation at all. Why everything could be executed in the command line but not be executed with RUN? I've tried both variants:

RUN command
RUN command
RUN pip install ...

and

RUN command\
    && command\
    && pip install ...

Commands that I execute:

from centos

run yum install -y centos-release-scl\
    && yum install -y rh-python36\
    && scl enable rh-python36 bash\
    && pip install django

UPD: Full path to the pip helped. What's wrong?

回答1:

You need to install pip first using

yum install python-pip

or if you need python3 (from epel)

yum install python36-pip

When not sure, ask yum:

yum whatprovides /usr/bin/pip

python2-pip-18.1-1.fc29.noarch : A tool for installing and managing Python 2 packages
Repo        : @System
Matched from:
Filename    : /usr/bin/pip

python2-pip-18.1-1.fc29.noarch : A tool for installing and managing Python 2 packages
Repo        : updates
Matched from:
Filename    : /usr/bin/pip

python2-pip-18.0-4.fc29.noarch : A tool for installing and managing Python 2 packages
Repo        : fedora
Matched from:
Filename    : /usr/bin/pip

This output is from Fedora29, but you should get similar result in Centos/RHEL

UPDATE

From comment

But when I execute same commands from docker run -ti centos everything is fine. What's the problem?

Maybe your PATH is broken somehow? Can you try full path to pip?



回答2:

As it has already been mentioned by @rkosegi, it must be a PATH issue. The following seems to work:

FROM centos
ENV PATH /opt/rh/rh-python36/root/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RUN yum install -y centos-release-scl
RUN yum install -y rh-python36
RUN scl enable rh-python36 bash
RUN pip install django

I "found" the above PATH by starting a centos container and typing the commands one-by-one (since you've mentioned that it is working).


There is a nice explanation on this, in the slides of BMitch which can be found here: sudo-bmitch.github.io/presentations/dc2018/faq-stackoverflow.html#24

Q: Why doesn't RUN work?

Why am I getting ./build.sh is not found?

RUN cd /app/srcRUN ./build.sh
  • The only part saved from a RUN is the filesystem (as a new layer).
  • Environment variables, launched daemons, and the shell state are all discarded with the temporary container when pid 1 exits.

  • Solution: merge multiple lines with &&:

    RUN cd /app/src && ./build.sh
    


标签: docker pip