You can try using conda. I used several stages to minimize final container and to speedup/cache local builds.

# first stage
FROM nvidia/cuda:11.1-base-ubuntu18.04 as builder
RUN apt-get update && apt-get install -y curl wget gcc build-essential

# install conda
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-4.5.12-Linux-x86_64.sh -O ~/miniconda.sh && \
     /bin/bash ~/miniconda.sh -b -p /opt/conda

# create env with python 3.5
RUN /opt/conda/bin/conda create -y -n myenv python=3.5
    
# install requirements
WORKDIR /app
COPY requirements.txt /app
ENV PATH=/opt/conda/envs/myenv/bin:$PATH    
RUN pip install -r requirements.txt
RUN pip uninstall -y pip


####################
# second stage (note: FROM container must be the same as builder)
FROM nvidia/cuda:11.1-base-ubuntu18.04 as runner

# copy environment data including python
COPY --from=builder /opt/conda/envs/myenv/bin /opt/conda/envs/myenv/bin
COPY --from=builder /opt/conda/envs/myenv/lib /opt/conda/envs/myenv/lib
# do some env settings
ENV PATH=/opt/conda/envs/myenv/bin:$PATH
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8


####################
# final image
from runner
WORKDIR /app    
COPY ./run.py /app
CMD [ "python", "run.py"]
Answer from Airenas on Stack Overflow
🌐
Docker Hub
hub.docker.com › _ › python
python - Official Image | Docker Hub
For information about how to get Docker running on Windows, please see the relevant "Quick Start" guide provided by Microsoft: ... View license information for Python 2⁠ and Python 3⁠.
🌐
Docker
docker-py.readthedocs.io
Docker SDK for Python — Docker SDK for Python 7.1.0 documentation
A Python library for the Docker Engine API. It lets you do anything the docker command does, but from within Python apps – run containers, manage containers, manage Swarms, etc. For more information about the Engine API, see its documentation. The latest stable version is available on PyPI.
Discussions

Setting specific python version in docker file with specfic non-python base image - Stack Overflow
I want to create a docker image with specifically python 3.5 on a specific base image which is the nvidia/cuda (9.0-base image) the latter has no python environment. The reason I need specific ve... More on stackoverflow.com
🌐 stackoverflow.com
Docker keeps installing the wrong version of Python
When you install python3.10 in apt based distributions you get the new one as python3.10. To change the default you could use: RUN update-alternatives --set python /usr/bin/python3.10 I never had the time to read update-alternatives documentation, so maybe there is something missing. More on reddit.com
🌐 r/docker
13
2
May 4, 2023
Docker image with two different python versions
Hello Community, FROM public.ecr.aws/lambda/python:3.8 check our python environment RUN python --version RUN pip --version Copy function code COPY requirements/requirements-prod.txt ${LAMBDA_TASK_ROOT}/requirements.txt COPY src/handler.py ${LAMBDA_TASK_ROOT}/handler.py COPY src/ ${LAMBDA_T... More on forums.docker.com
🌐 forums.docker.com
1
0
February 22, 2024
Docker change default python version
Hi , I have built a docker image using ubuntu. It installs python 3.5.2 But when I open the interactive container using /bin/bash and re-check the python version. I have observed that it is set to default python vers… More on forums.docker.com
🌐 forums.docker.com
0
0
November 4, 2016
🌐
Real Python
realpython.com › python-versions-docker
Run Python Versions in Docker: How to Try the Latest Python Release – Real Python
May 12, 2023 - You can find a list of all available Python images at Docker Hub. python:latest will always give you the latest stable version of Python, while python:rc will provide you with the most recent development version.
🌐
PyPI
pypi.org › project › docker
docker · PyPI
It lets you do anything the docker command does, but from within Python apps – run containers, manage containers, manage Swarms, etc. The latest stable version is available on PyPI.
      » pip install docker
    
Published   May 23, 2024
Version   7.1.0
Top answer
1 of 3
7

You can try using conda. I used several stages to minimize final container and to speedup/cache local builds.

# first stage
FROM nvidia/cuda:11.1-base-ubuntu18.04 as builder
RUN apt-get update && apt-get install -y curl wget gcc build-essential

# install conda
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-4.5.12-Linux-x86_64.sh -O ~/miniconda.sh && \
     /bin/bash ~/miniconda.sh -b -p /opt/conda

# create env with python 3.5
RUN /opt/conda/bin/conda create -y -n myenv python=3.5
    
# install requirements
WORKDIR /app
COPY requirements.txt /app
ENV PATH=/opt/conda/envs/myenv/bin:$PATH    
RUN pip install -r requirements.txt
RUN pip uninstall -y pip


####################
# second stage (note: FROM container must be the same as builder)
FROM nvidia/cuda:11.1-base-ubuntu18.04 as runner

# copy environment data including python
COPY --from=builder /opt/conda/envs/myenv/bin /opt/conda/envs/myenv/bin
COPY --from=builder /opt/conda/envs/myenv/lib /opt/conda/envs/myenv/lib
# do some env settings
ENV PATH=/opt/conda/envs/myenv/bin:$PATH
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8


####################
# final image
from runner
WORKDIR /app    
COPY ./run.py /app
CMD [ "python", "run.py"]
2 of 3
2

You can install from PPA and use it as usual:

FROM nvidia/cuda

RUN apt-get update && apt-get install -y --no-install-recommends software-properties-common \
    libsm6 libxext6 libxrender-dev curl \
    && rm -rf /var/lib/apt/lists/*

RUN echo "**** Installing Python ****" && \
    add-apt-repository ppa:deadsnakes/ppa &&  \
    apt-get install -y build-essential python3.5 python3.5-dev python3-pip && \
    curl -O https://bootstrap.pypa.io/get-pip.py && \
    python3.5 get-pip.py && \
    rm -rf /var/lib/apt/lists/*

COPY requirements.txt requirements.txt

RUN pip3.5 install -r requirements.txt

CMD ["python3.5", "app.py"]
🌐
Python⇒Speed
pythonspeed.com › articles › base-image-python-docker-images
The best Docker base image for your Python application (February 2026)
February 4, 2026 - Another alternative is Docker’s own “official” python image, which comes pre-installed with respective versions of Python (3.10, 3.11, 3.12, etc.), and has multiple variants:
🌐
Reddit
reddit.com › r/docker › docker keeps installing the wrong version of python
r/docker on Reddit: Docker keeps installing the wrong version of Python
May 4, 2023 -

I'm installing Python after pulling from NVIDIA's CUDA 11.8 base image. However, despite installing Python 3.10 Docker's telling me that it's using Python 3.8.

My Docker file is as follows:

FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu20.04
WORKDIR /app
COPY . .
RUN apt-get update && DEBIAN_FRONTEND=noninteractive \
    apt-get install -y python3.10 python3-pip

RUN curl -sSL https://install.python-poetry.org | python3 - --preview
RUN pip3 install --upgrade requests
RUN ln -fs /usr/bin/python3 /usr/bin/python

CMD ["python", "--version"]

After building and running this image, it tells me that Python is running 3.8.

Any idea what may be going wrong? I'm suspecting a Docker server version problem (client is 23.0.1 but server is 19.03.12) but am not sure.

Find elsewhere
🌐
GitHub
github.com › docker › docker-py › releases
Releases · docker/docker-py
May 23, 2024 - Removed SSL version (ssl_version) and explicit hostname check (assert_hostname) options (#3185) assert_hostname has not been used since Python 3.6 and was removed in 3.12 · Python 3.7+ supports TLSv1.3 by default · Websocket support is no ...
Author   docker
🌐
Docker Community
forums.docker.com › docker desktop
Docker image with two different python versions - Docker Desktop - Docker Community Forums
February 22, 2024 - Hello Community, FROM public.ecr.aws/lambda/python:3.8 check our python environment RUN python --version RUN pip --version Copy function code COPY requirements/requirements-prod.txt ${LAMBDA_TASK_ROOT}/requirements.txt COPY src/handler.py ${LAMBDA_TASK_ROOT}/handler.py COPY src/ ${LAMBDA_TASK_ROOT}/src RUN python3.8 -m pip install -r requirements.txt --target “${LAMBDA_TASK_ROOT}” Set the CMD to your handler CMD [“handler.profile”] Question I have been using the above template to build ...
🌐
Docker Community
forums.docker.com › general
Docker change default python version - General - Docker Community Forums
November 4, 2016 - Hi , I have built a docker image using ubuntu. It installs python 3.5.2 But when I open the interactive container using /bin/bash and re-check the python version. I have observed that it is set to default python vers…
🌐
Medium
mhaske-padmajeet.medium.com › docker-magic-installing-and-managing-multiple-python-versions-00c45868c045
Docker Magic: Installing and Managing Multiple Python Versions | by Padmajeet Mhaske | Medium
March 5, 2025 - A Dockerfile itself cannot directly contain multiple Python versions, as each Docker image is typically based on a single operating system environment with a single version of Python installed.
🌐
Docker
hub.docker.com › search
Explore Docker's Container Image Repository | Docker Hub
ActiveState's customizable, low-to-no vulnerability container image for python. ... Dockerfiles for CPython of lysnikolaou's tag-strings-rebased branch.
🌐
GitHub
github.com › docker › docker-py
GitHub - docker/docker-py: A Python library for the Docker Engine API · GitHub
A Python library for the Docker Engine API. It lets you do anything the docker command does, but from within Python apps – run containers, manage containers, manage Swarms, etc. The latest stable version is available on PyPI.
Starred by 7.2K users
Forked by 1.7K users
Languages   Python
🌐
PyPI
pypi.org › project › docker-py
docker-py · PyPI
Download URL: docker_py-1.10.6-py2.py3-none-any.whl
      » pip install docker-py
    
Published   Nov 02, 2016
Version   1.10.6