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

微笑、不失礼 提交于 2021-02-19 16:06:43

问题


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


回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/55346068/how-do-i-alias-python2-to-python3-in-a-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!