GoMonkey
GoMonkey

Reputation: 361

How do I alias python2 to python3 in a docker container?

I am trying to set the default python in my docker container to be python3 and have set the aliases in the dockerfile. When I open the .bashrc file, they show up. As far as I can tell, it should work but the default python version is still 2.7. if I run which python, it will still point to usr/bin/python rather than python3. Same with pip. Can anyone tell me what the problem is? Here is the command I'm using to alias:

   RUN \
   echo 'alias python="/usr/bin/python3"' >> /root/.bashrc && \
   echo 'alias pip="/usr/bin/pip3"' >> /root/.bashrc

Does this look right? I am using ubuntu 17.10

Upvotes: 18

Views: 24613

Answers (2)

rdurrani
rdurrani

Reputation: 121

The answer above is great, except it should be as follows:

RUN ln -s /usr/bin/python3 /usr/bin/python && \
    ln -s /usr/bin/pip3 /usr/bin/pip

Perhaps they typo-ed by writing ls which just lists the contents of the directory, rather than using ln which actually creates symlinks.

Upvotes: 10

iddar
iddar

Reputation: 521

You try to create a symlink for python bin

RUN ln -s /usr/bin/python3 /usr/bin/python & \
    ln -s /usr/bin/pip3 /usr/bin/pip

other option is use update-alternatives for more visit this site

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3

and another option is trying source the bashrc file after updating

RUN \
   echo 'alias python="/usr/bin/python3"' >> /root/.bashrc && \
   echo 'alias pip="/usr/bin/pip3"' >> /root/.bashrc && \
   source /root/.bashrc

I recommend seeing all options of python images on Docker Hub

Tip: use anaconda or conda for managing your python versions (conda site)

Upvotes: 37

Related Questions