繁体   English   中英

在 Ubuntu Docker 镜像中将 python3.8 升级到 3.10

[英]Upgrade python3.8 to 3.10 in Ubuntu Docker image

我正在使用剧作家基础图像

FROM mcr.microsoft.com/playwright

不幸的是,这是python3.8自带的。 我可以使用 python3.10 图像并在其上安装 playright,但它带来了其他复杂性,因此我选择将 playright 图像上的 python 升级到 3.10。

到目前为止,我的 Dockerfile 看起来像这样

FROM mcr.microsoft.com/playwright

apt install -y software-properties-common && add-apt-repository -y ppa:deadsnakes/ppa && apt update && apt install -y python3.10

RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2

这工作正常,但问题是在设置替代方案后“我如何制作 python3.10”默认版本?

谢谢

python3只是/usr/bin/python3.8的符号链接。 您可以使用以下命令创建新链接:

RUN ln -sf /usr/bin/python3.10 /usr/bin/python3

-f (强制)选项用新符号链接覆盖现有符号链接。

有两点需要注意:

  1. Python 可执行文件
  2. 备选方案优先

可执行

在基础镜像中运行:

# which python
# which python3
/usr/bin/python3
# ls -l /usr/bin/python*
lrwxrwxrwx 1 root root       9 Mar 13  2020 /usr/bin/python3 -> python3.8
-rwxr-xr-x 1 root root 5230592 Jun 22 20:18 /usr/bin/python3.8

使用以下 Dockerfile(来自问题);

FROM mcr.microsoft.com/playwright
RUN apt update
RUN apt install -y software-properties-common
RUN add-apt-repository -y ppa:deadsnakes/ppa
RUN apt update
RUN apt install -y python3.10

RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2

python现在由替代品添加和控制,但python3未受影响:

# ls -l /usr/bin/python*
lrwxrwxrwx 1 root root      24 Dec 16 04:10 /usr/bin/python -> /etc/alternatives/python
lrwxrwxrwx 1 root root       9 Mar 13  2020 /usr/bin/python3 -> python3.8
-rwxr-xr-x 1 root root 5565184 Dec  7 01:12 /usr/bin/python3.10
-rwxr-xr-x 1 root root 5230592 Jun 22 20:18 /usr/bin/python3.8
# ls -l /etc/alternatives/python
lrwxrwxrwx 1 root root 18 Dec 16 04:10 /etc/alternatives/python -> /usr/bin/python3.8

优先事项

从备选方案的联机帮助页

如果该组处于自动模式,并且新添加的备选方案的优先级高于该组的任何其他已安装备选方案,则符号链接将更新以指向新添加的备选方案。

这是数字,所以并不是说 1 是赢家,2 是第二好,而是 2 比 1 高。

解析度

通过将可执行形式python更改为python3和优先级,使3.103.8为 1(如此):

FROM mcr.microsoft.com/playwright
RUN apt update
RUN apt install -y software-properties-common
RUN add-apt-repository -y ppa:deadsnakes/ppa
RUN apt update
RUN apt install -y python3.10

RUN update-alternatives --install /usr/bin/python3 python /usr/bin/python3.10 2
RUN update-alternatives --install /usr/bin/python3 python /usr/bin/python3.8 1

默认的 python 版本应该是 3.10:

# which python
# which python3
# ls -l /usr/bin/python*
lrwxrwxrwx 1 root root      24 Dec 16 04:16 /usr/bin/python3 -> /etc/alternatives/python
-rwxr-xr-x 1 root root 5565184 Dec  7 01:12 /usr/bin/python3.10
-rwxr-xr-x 1 root root 5230592 Jun 22 20:18 /usr/bin/python3.8
# ls -l /etc/alternatives/python
lrwxrwxrwx 1 root root 19 Dec 16 04:16 /etc/alternatives/python -> /usr/bin/python3.10
# python3 --version
Python 3.10.9

This Dockerfile should do the trick I think,虽然我没有使用update-alternatives ,所以也许不是你要找的东西

FROM mcr.microsoft.com/playwright


RUN apt update && apt install software-properties-common -y && add-apt-repository ppa:deadsnakes/ppa
RUN apt install python3.10 -y
RUN rm /usr/bin/python3 && ln -s /usr/bin/python3.10 /usr/bin/python3

基本上我们只是删除/usr/bin/python3的旧符号链接(指向/usr/bin/python3.8二进制文件)并在/usr/bin/python3 (指向/usr/bin/python3.10我们刚刚安装的/usr/bin/python3.10二进制文件)。 这是输出

➜ docker build . --tag test
[+] Building 14.7s (8/8) FINISHED                                                                                      
 => [internal] load build definition from Dockerfile                                                              0.0s
 => => transferring dockerfile: 280B                                                                              0.0s
 => [internal] load .dockerignore                                                                                 0.0s
 => => transferring context: 2B                                                                                   0.0s
 => [internal] load metadata for mcr.microsoft.com/playwright:latest                                              0.2s
 => CACHED [1/4] FROM mcr.microsoft.com/playwright@sha256:adade3016b008d5a892fd228499eed34919b4d8128b9c04bd447eb  0.0s
 => [2/4] RUN apt update && apt install software-properties-common -y && add-apt-repository ppa:deadsnakes/ppa    9.5s
 => [3/4] RUN apt install python3.10 -y                                                                           4.6s
 => [4/4] RUN rm /usr/bin/python3 && ln -s /usr/bin/python3.10 /usr/bin/python3                                   0.2s 
 => exporting to image                                                                                            0.2s 
 => => exporting layers                                                                                           0.2s 
 => => writing image sha256:fba6bb8a4cbd26ec4be70fd9d9d7638bcd2139fed83fd6ca52ef52992d47d1dc                      0.0s 
 => => naming to docker.io/library/os                                                                             0.0s 
➜ docker run -it test      
root@0bbc072f22ce:/# which python3 
/usr/bin/python3
root@0bbc072f22ce:/# python3 --version 
Python 3.10.9

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM