No matter what I try, my Docker container is using Python 3.10 rather than what I need and specify, which is Python 3.7. How can I force Docker to use 3.7 for a specific image/container?
I'm a novice Docker user and some of these Docker configuration files were not written by me, so forgive my ignorance.
The Error
Here's the error. You can clearly see that Docker is using 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 configuration files
My 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"]
The 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:
And the 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
As an update, I was able to "fix" this by completely wiping the image, container, restarting my computer, Docker, etc. and rebuilding.
Also, in case it's useful to others, one gut check that was useful for me was to run the image in an interactive shell and check which Python version it was using.
$ docker run --rm -it makelab_image sh
$ which python
/usr/local/bin/python
$ python --version
Python 3.7.12
CodePudding user response:
Unless you explicitly in Dockerfile
upgrade python, the python version
in image is 3.7
no doubt, at least in the Dockerfile
you afford I didn't see this:
$ docker run --rm -it python:3.7 python --version
Python 3.7.12
Additional, a possible reason maybe you sometimes in the past, make a wrong tag for python:3.7
with python:3.10
, like next, then it could result your issues:
$ docker tag python:3.10 python:3.7
$ docker run --rm -it python:3.7 python --version
Python 3.10.0
If above is your case, do next to clean the environment, then repeat your steps to check it again:
$ docker rmi python:3.7
CodePudding user response:
Create runtime.txt in root directory and write in it:
python-3.7
CodePudding user response:
Had the same problem, but with the nvidia base image (FROM nvidia/cuda:11.2.0-cudnn8-runtime-ubuntu20.04
) . This worked to me, hope it works for you too.
Add this in 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
after this you can use python 3.7 with the command python3.7
and you can install packages with pip3.7 install [packag name]
.
This ./configure --enable-optimizations
will install python 3.7 from source code with optimizations for the OS, and it will take a long time.