在 docker 运行 和构建之间获取不同的 python 版本

Getting different python version between docker run and build

我正在尝试使用 official centos python 2.7 image 作为基础构建一个 python 图像:

当我通过 bash shell 运行 基础映像时,我得到了与 docker 尝试构建它时不同的 python 版本我的 docker 文件。

示例:

docker system prune    # clean slate
docker pull centos/python-27-centos7:latest
docker run -it centos/python-27-centos7:latest /bin/bash
which python
> /opt/app-root/bin/python
python --version
> Python 2.7.16
exit

所以...在基本图像上打开 bash shell,我已经安装了 python 2.7.16。

app.dockerfile 的内容:

FROM centos/python-27-centos7:latest
RUN which python
RUN python --version
RUN pip install -U pip

当我构建 docker 文件时:

docker build --no-cache -f app.dockerfile .

我得到这个输出:

Sending build context to Docker daemon  14.34kB
Step 1/7 : FROM centos/python-27-centos7:2.7
 ---> 55ede294318e
Step 2/7 : RUN which python
 ---> Running in 2c1c217dc7a2
/opt/app-root/bin/python
Removing intermediate container 2c1c217dc7a2
 ---> 7f3692a71370
Step 3/7 : RUN python --version
 ---> Running in 12cfab19a44d
Python 2.7.5
Removing intermediate container 12cfab19a44d
 ---> 7d354cadd2af
Step 4/7 : RUN pip install -U pip
 ---> Running in 4d58f3999602
Traceback (most recent call last):
  File "/opt/app-root/bin/pip", line 7, in <module>
    from pip import main
  File "/opt/app-root/lib/python2.7/site-packages/pip/__init__.py", line 4, in <module>
    import logging
  File "/opt/rh/python27/root/usr/lib64/python2.7/logging/__init__.py", line 26, in <module>
    import sys, os, time, cStringIO, traceback, warnings, weakref, collections
  File "/opt/rh/python27/root/usr/lib64/python2.7/weakref.py", line 14, in <module>
    from _weakref import (
ImportError: cannot import name _remove_dead_weakref
The command '/bin/sh -c pip install -U pip' returned a non-zero code: 1

图像现在报告 python - 在 完全相同的路径 - 是 2.7.5。当我进入 pip install 命令时,它失败了,因为 2.7.5 中存在一个错误,但已由 2.7.16 解决。

为什么我会看到这种差异? 运行将 bash shell 添加到图像上,然后 运行ning 构建步骤有什么不同?

查看我上面的评论 - 似乎我使用的图像类型与我认为的不同。