SeanD
SeanD

Reputation: 135

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" ]

Upvotes: 8

Views: 34379

Answers (3)

Giacomo Brunetta
Giacomo Brunetta

Reputation: 1567

You can follow the funnydman example, but there is another way to do that. You can install another Python version and then you can specify the priority. For example, if the base image automatically works with Python3.6, you can install also Python3.5 and define Python3.5 with an higher priority than Python3.6, using update-alternatives:

FROM nvidia/cuda

RUN apt-get update && apt-get install -y libsm6 libxext6 libxrender-dev python3.5 python3-pip && \
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 2 && \
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1

COPY . /app
WORKDIR /app

RUN pip3 install -r requirements.txt

ENTRYPOINT [ "python3" ]
CMD [ "app.py" ]

Upvotes: 0

Airenas
Airenas

Reputation: 425

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"]

Upvotes: 6

funnydman
funnydman

Reputation: 11326

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"]

Upvotes: 2

Related Questions