Docker Community Forums

Share and learn in the Docker community.

Install python in dockerfile

Hello,
I want to install python in the dockerfile but when I test it with RUN python -V it says:

/bin/sh 1 python not found.

I have also tried building python from source, but still get this error. Can someone help? There is something wrong with the aliases I think.

FROM yummygooey/raspbian-buster
RUN apt-get update
RUN apt-get remove --purge -y python3.7

Install Python

RUN apt-get install -y python3.6
CMD alias python3=/usr/bin/python3.6
CMD alias python=/usr/bin/python3.6

RUN /bin/bash -c alias python=/usr/bin/python3.6
RUN /bin/bash -c echo ‘alias python=python3.6’ >> ~/.bashrc
RUN echo -e ‘#!/bin/bash\npython3=/usr/bin/python3.6’ > ~/.bashrc &&
chmod +x ~/.bashrc
RUN python3 -V

The reason it doesn’t work is that you never actually establish the alias because ~/.bashrc is never executed. I would use a symbolic link instead.

Try this:

FROM yummygooey/raspbian-buster
RUN apt-get update \
    && apt-get remove --purge -y python3.7

# Install Python
RUN apt-get install -y python3.6 \
    && ln -s /usr/bin/python3.6 /usr/bin/python3
RUN python3 -V

Of course, you don’t want to keep RUN python3 -V in the final Dockerfile as it adds a layer with no added value but I wanted to show you that this call now works because of the symbolic link.