Docker 如何使 python 3.8 成为默认值

Docker how to make python 3.8 as default

我正在尝试更新现有的 Dockerfile 以从 python3.5 切换到 python3.8,之前它为 python3.5pip3 创建一个符号链接,如下所示:

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

我已经更新了 Dockerfile 以从 deadsnakes:ppa

安装 python3.8
apt-get install python3-pip python3.8-dev python3.8-distutils python3.8-venv

如果我删除 python3-pip,它会抱怨 gcc

C compiler or Python headers are not installed on this system. Try to run: sudo apt-get install gcc python3-dev

有了这些安装,我正在尝试像这样更新现有的符号链接创建:

RUN ln -s /usr/bin/pip3 /usr/local/lib/python3.8/dist-packages/pip
RUN ln -s /usr/bin/pip /usr/local/lib/python3.8/dist-packages/pip
RUN ln -s /usr/bin/python3.8 /usr/bin/python3

它失败了,说

ln: failed to create symbolic link '/usr/bin/python3': File exists

我假设它失败了,因为 python3 指向 python3.6。 如果我尝试: RUN ln -s /usr/bin/python3.8 /usr/bin/python 它不会抱怨符号链接和图像成功构建,但在稍后安装需求时失败(我们使用 Makefile 目标使用 pip 和 [=26= 在容器内安装依赖项]):

ERROR: Cannot uninstall 'python-apt'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

我认为这是因为 python-apt 作为默认 python3.6 安装的一部分安装并且 python3.8 pip 无法卸载它。

PS:我的 Dockerfile 镜像基于 Ubuntu 18.04,默认带有 python3.6

如何正确地将 Dockerfile/image 从 python3.5 切换到 python3.8?所以我以后可以直接使用 pip 并且它指向 python3.8pip

为什么不使用您需要的配置从 ubuntu:18.04 构建一个新图像? 像这样:

FROM ubuntu:18.04
RUN apt update && apt install software-properties-common -y
RUN add-apt-repository ppa:deadsnakes/ppa && install python3.8 -y
RUN ln -s /usr/bin/pip3 /usr/bin/pip && \
    ln -s /usr/bin/python3.8 /usr/bin/python

以这种方式替换系统 python 通常不是一个好主意(因为它会破坏依赖于那些可执行文件的 operating-system-level 程序)——我在 this video I made "why not global pip / virtualenv?"

更好的方法是创建一个前缀并将其放在更早的 PATH 上(这允许系统可执行文件继续工作,但裸露 python / python3 / 等。将使用您的其他可执行文件)

对于您似乎正在使用的 deadsnakes,类似这样的方法应该有效:

FROM ubuntu:bionic

RUN : \
    && apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        software-properties-common \
    && add-apt-repository -y ppa:deadsnakes \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        python3.8-venv \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && :

RUN python3.8 -m venv /venv
ENV PATH=/venv/bin:$PATH

ENV 行是这里的关键,它将 virtualenv 放在路径的开头

$ docker build -t test . 
...
$ docker run --rm -ti test bash -c 'which python && python --version && which pip && pip --version'
/venv/bin/python
Python 3.8.5
/venv/bin/pip
pip 20.1.1 from /venv/lib/python3.8/site-packages/pip (python 3.8)

免责声明:我是 deadsnakes 的维护者

有时候,修改OS(比如get new Ubuntu clean os)是不利的,因为现在的OS太复杂了。比如我的base OS 是FROM ufoym/deepo:all-cu101.

所以,为了将现有的 python (3.6) 修改为 python 3.8,我添加了这两行:

RUN apt-get update -qq   && apt-get install -y -qq python3.8

RUN rm /usr/bin/python && rm /usr/bin/python3 && ln -s /usr/bin/python3.8 /usr/bin/python &&  ln -s /usr/bin/python3.8 /usr/bin/python3 \
    && rm /usr/local/bin/python && rm /usr/local/bin/python3 && ln -s /usr/bin/python3.8 /usr/local/bin/python &&  ln -s /usr/bin/python3.8 /usr/local/bin/python3 \
    && apt-get install -y python3-pip python-dev python3.8-dev && python3 -m pip install pip --upgrade

第一步是安装python3.8; 第二步修改python和python3的softlink指向python3.8 之后,安装 python3-pip,并更新它以确保 pip 使用当前的 python 3.8 环境。