Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting specific python version in docker file with specfic non-python base image

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 versions is to support running cuda10.0 python3.5 and a gcc version<7 to compile the driver all together on the same box

When I try and build the docker environments (see below) I always end up with the system update files which load python3.6

The first version I run (below) runs a system update dependencies which installs python 3.6 I have tried many variants to avoid this but always end up 3.6 in the final image.

Any suggestions for getting this running with python3.5 are welcome

Thanks

FROM nvidia/cuda

RUN apt-get update && apt-get install -y libsm6 libxext6 libxrender-dev python3.5 python3-pip 

COPY . /app
WORKDIR /app

RUN pip3 install -r requirements.txt
ENTRYPOINT [ "python3" ]
CMD [ "app.py" ]

Another variant (below) I have tried is with virtualenv and here again I can't seem to force a python 3.5 environment

FROM nvidia/cuda

RUN apt-get update && apt-get install -y --no-install-recommends libsm6 libxext6 libxrender-dev python3.5 python3-pip python3-virtualenv

ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m virtualenv --python=/usr/bin/python3 $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

COPY . /app
WORKDIR /app

RUN pip3 install -r requirements.txt
ENTRYPOINT [ "python3" ]
CMD [ "app.py" ]
like image 315
SeanD Avatar asked Jun 25 '26 06:06

SeanD


1 Answers

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"]
like image 64
Airenas Avatar answered Jun 27 '26 05:06

Airenas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!