我使用剧作家基础图像
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"设置替代方案后的默认版本?
感谢有两点需要注意:
Python可执行- 选择优先
执行
在基本映像中运行:
# 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
优先From alternative的手册:
如果组处于自动模式,并且新添加的备选项优先级高于此组的任何其他已安装的替代方案符号链接将被更新以指向新添加的备选项。
这是一个数字,所以不是1是赢家,2是次优,而是2比1高。
的决议通过将可执行形式python
更改为python3
,并将优先级更改为3.10
将为2,3.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
python3
只是指向/usr/bin/python3.8
的符号链接。您可以使用以下命令创建一个新的链接:
RUN ln -sf /usr/bin/python3.10 /usr/bin/python3
-f
(force)选项将用新的符号链接覆盖现有的符号链接。
这个Dockerfile应该做的技巧,我认为,虽然我不使用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
二进制文件)。下面是输出
➜ 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