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 OverflowSetting specific python version in docker file with specfic non-python base image - Stack Overflow
Docker image with two different python versions
Docker keeps installing the wrong version of Python
Docker change default python version
Videos
» pip install docker
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"]
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"]
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.
» pip install docker-py
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
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.