【问题标题】:Wrong Python version running in Docker在 Docker 中运行的 Python 版本错误
【发布时间】:2021-12-01 01:04:02
【问题描述】:

无论我尝试什么,我的 Docker 容器都在使用 Python 3.10,而不是我需要和指定的,即 Python 3.7。如何强制 Docker 为特定的图像/容器使用 3.7?

我是新手 Docker 用户,其中一些 Docker 配置文件不是我写的,请原谅我的无知。

错误

这是错误。您可以清楚地看到 Docker 使用的是 Python 3.10。

website_1  | Traceback (most recent call last):
website_1  |   File "/code/manage.py", line 10, in <module>
website_1  |     execute_from_command_line(sys.argv)
website_1  |   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
website_1  |     utility.execute()
website_1  |   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 357, in execute
website_1  |     django.setup()
website_1  |   File "/usr/local/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
website_1  |     apps.populate(settings.INSTALLED_APPS)
website_1  |   File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 120, in populate
website_1  |     app_config.ready()
website_1  |   File "/code/website/apps.py", line 11, in ready
website_1  |     import website.signals
website_1  |   File "/code/website/signals.py", line 4, in <module>
website_1  |     from wand.image import Image, Color
website_1  |   File "/usr/local/lib/python3.10/site-packages/wand/image.py", line 3383, in <module>
website_1  |     class Iterator(Resource, collections.Iterator):
website_1  | AttributeError: module 'collections' has no attribute 'Iterator'

Docker 配置文件

我的 Dockerfile

FROM python:3.7

# Setup some other prereqs needed:
RUN apt-get update && apt-get --assume-yes install imagemagick ghostscript sqlite3

# Set the stdout/stderr streams in Python to be unbuffered
ENV PYTHONUNBUFFERED 1

# Create a system user which we'll use later.
# We're using the 'apache' user since that's what we're trying to map
# outside the container -- it could be called anything, but apache is convenient
RUN useradd -u 48 apache
RUN groupmod -g 48 apache

# The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. 
# The resulting committed image will be used for the next step in the Dockerfile.
RUN mkdir /code

# The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions 
# that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used 
# in any subsequent Dockerfile instruction. 
WORKDIR /code

# COPY and RUN the requirements.txt into the docker container
COPY requirements.txt /code/
RUN pip3.7 install -r requirements.txt

# Our local user needs write access to a website and static files
RUN chown -R apache /code/

COPY . /code/

#Run the process as our local user:
USER apache

COPY docker-entrypoint.sh docker-entrypoint.sh
CMD ["/code/docker-entrypoint.sh"] 

docker-compose.yml:

version: '3'

services:
  db:
     image: 'postgres'
     restart: always
     ports:
       - '1456'
     environment:
      - POSTGRES_DB=databasename
      - POSTGRES_USER=username
      - POSTGRES_PASSWORD=passwordname
     volumes:
      - postgres-data:/var/lib/postgresql/data
  website:    
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    command: python3 manage.py runserver 0.0.0.0:8000
    ports:
      - '127.0.0.1:8985:8000'
    volumes:
      - .:/code
    depends_on:
      - db
    command: ["./docker-entrypoint.sh", "db", "python", "manage.py"]
    links:
      - "db:database"
volumes:
    postgres-data:   

还有docker-entrypoint.sh

#!/bin/bash

# Collect static files
echo "Collecting static files"
python manage.py collectstatic --noinput

# Apply database migrations
echo "Running makemigrations and migrate"
python manage.py makemigrations
python manage.py migrate

echo "Running makemigrations and migrate explicitly to website (often fixes some first-time run issues)"
python manage.py makemigrations website
python manage.py migrate website

# Start server
echo "Starting server"
python manage.py runserver 0.0.0.0:8000

作为更新,我能够通过完全擦除映像、容器、重新启动计算机、Docker 等并重建来“修复”此问题。

另外,如果它对其他人有用,对我有用的一个直觉检查是在交互式 shell 中运行图像并检查它使用的是哪个 Python 版本。

$ docker run --rm -it makelab_image sh
$ which python
/usr/local/bin/python
$ python --version
Python 3.7.12

【问题讨论】:

    标签: python python-3.x django docker docker-compose


    【解决方案1】:

    除非你明确在Dockerfile升级python,否则图片中的python version无疑是3.7,至少在Dockerfile你买得起我没看到这个:

    $ docker run --rm -it python:3.7 python --version
    Python 3.7.12
    

    另外,一个可能的原因可能是你过去有时用python:3.10python:3.7 制作了错误的标签,就像下一个,那么它可能会导致你的问题:

    $ docker tag python:3.10 python:3.7
    $ docker run --rm -it python:3.7 python --version
    Python 3.10.0
    

    如果是上述情况,请执行下一步清理环境,然后重复您的步骤再次检查:

    $ docker rmi python:3.7
    

    【讨论】:

    • 谢谢@atline。这是一个有用的直觉检查。我最终清理了我的环境并重建了一切。然后它起作用了。如果它对其他人有用,我还使用图像运行了一个交互式 shell,以检查正在运行的 Python 版本。我用该信息更新了我的原始帖子。
    【解决方案2】:

    遇到了同样的问题,但使用的是 nvidia 基础映像 (FROM nvidia/cuda:11.2.0-cudnn8-runtime-ubuntu20.04)。这对我有用,希望它也对你有用。 在 Dockerfile 中添加:

    RUN apt-get update && apt-get install -y \
        build-essential \
        zlib1g-dev \
        libncurses5-dev \
        libgdbm-dev \
        libnss3-dev \
        libssl-dev \
        libsqlite3-dev \
        libreadline-dev \
        libffi-dev \
        wget \
        libbz2-dev
    
    RUN wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz && \
        tar -xf Python-3.7.4.tgz && \
        cd Python-3.7.4 && \
        ./configure --enable-optimizations && \
        make -j$(nproc) && \
        sudo make altinstall
    

    在此之后,您可以使用命令python3.7 使用python 3.7,并且可以使用pip3.7 install [packag name] 安装软件包。

    这个./configure --enable-optimizations 将从源代码安装python 3.7 并针对操作系统进行优化,这将需要很长时间。

    【讨论】:

      【解决方案3】:

      另外可以帮助验证全局 (/usr/bin/python3 -V) 和本地 (/usr/local/bin/python3 -V) 安装的 python 版本。如果不同(比如 global 是 3.10,local 是 3.7)并且您希望使用本地版本作为默认版本,则创建从全局到本地的符号链接(例如 ln -sf /usr/local/bin/python3 /usr/bin/python3

      【讨论】:

        【解决方案4】:

        在根目录下创建runtime.txt并写入:

        python-3.7
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-08
        • 1970-01-01
        • 1970-01-01
        • 2019-07-21
        • 2011-05-10
        • 2022-11-24
        • 1970-01-01
        • 2020-05-22
        相关资源
        最近更新 更多