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

2024-03-29 09:56:16 发布

您现在位置:Python中文网/ 问答频道 /正文

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

当我通过bashshell运行基本映像时,我得到的python版本与docker尝试从我的dockerfile构建它时的版本不同。你知道吗

示例:

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

所以。。。在基本映像上打开一个bashshell,我已经安装了python2.7.16。你知道吗

app.dockerfile的内容:

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

当我构建dockerfile时:

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中存在一个bug,但是2.7.16解决了这个bug。你知道吗

为什么我看到了这种不同?在图像上运行bashshell和在其上运行构建步骤有什么不同?你知道吗


Tags: installpipdockerruninimportdockerfileapp