Install specific version of python in docker - python-3.x

I try to run container from image nvcr.io/nvidia/tensorflow:22.08-tf2-py3. But I have a problem.
The built docker-image contains python3.8. But I don't understand why I have this version of python in my docker-image. It is necessary to use python with version>=3.10 for correct work with libraries that I need. Version=3.8 is not explicitly specified in Dockerfile. When I try to install an another version:
RUN apt-get update && apt-get install -y software-properties-common && add-apt-repository ppa:deadsnakes/ppa && apt-get install -y python3.11
RUN python3.11 -m pip install --upgrade --no-cache -r requirements.txt
I get an error /usr/bin/python3.11: No module named pip during image building.
How can I correctly install specific version of python in my docker-image using Dockerfile?

You got Python 3.11 alright but you are missing the PIP module. Add python-pip or python3-pip to the list of packages you install with apt-get.

This is regarding your problem with python3.11 pip and not the tensor version support
Looking at the original python3.11 docker in here you should be able to use the bellow code.
# if this is called "PIP_VERSION", pip explodes with "ValueError: invalid truth value '<VERSION>'"
ENV PYTHON_PIP_VERSION 22.3
# https://github.com/docker-library/python/issues/365
ENV PYTHON_SETUPTOOLS_VERSION 65.5.0
# https://github.com/pypa/get-pip
ENV PYTHON_GET_PIP_URL https://github.com/pypa/get-pip/raw/66030fa03382b4914d4c4d0896961a0bdeeeb274/public/get-pip.py
ENV PYTHON_GET_PIP_SHA256 1e501cf004eac1b7eb1f97266d28f995ae835d30250bec7f8850562703067dc6
RUN set -eux; \
\
wget -O get-pip.py "$PYTHON_GET_PIP_URL"; \
echo "$PYTHON_GET_PIP_SHA256 *get-pip.py" | sha256sum -c -; \
\
export PYTHONDONTWRITEBYTECODE=1; \
\
python get-pip.py \
--disable-pip-version-check \
--no-cache-dir \
--no-compile \
"pip==$PYTHON_PIP_VERSION" \
"setuptools==$PYTHON_SETUPTOOLS_VERSION" \
; \
rm -f get-pip.py; \
\
pip --version
I was able to install pip for my python3.11 on Fedora(outside Docker) using python3 -m ensurepip (taken from here)

Related

Docker installed the wrong version of Python despite specifying the version

This is the part of my Dockerfile that installs Python and my code's dependencies.
FROM ubuntu:18.04
RUN apt-get update && \
apt-get install -y software-properties-common && \
add-apt-repository ppa:deadsnakes/ppa && apt-get update && apt-get install -y \
python3.8 \
python3-pip \
&& rm -rf /var/lib/apt/lists/*
RUN ln -s /usr/bin/python3 /usr/bin/python
RUN ln -s /usr/bin/pip3 /usr/bin/pip
# Update Python with the required packages
RUN pip install --upgrade pip
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
The image gets created and then when I ran the code I got this error back
q9zp213vt4-algo-1-cqgxl | /usr/local/lib/python3.6/dist-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
This message alerted me to the use of Python 3.6 and when I checked my image's Python version using the CLI I could indeed see it was the default Python version 3.6.9.
Apologies for this basic question, but I'm not familiar with working with Docker and I'm not sure where I'm going wrong. The Base image of Ubuntu cannot be changed.
You need to set the specific Python 3 version. RUN ln -s /usr/bin/python3 /usr/bin/python only tells Ubuntu to use the default Python 3 instead of Python 2, but not which Python 3. On my computer, python3 is linked to python3.10. You can forcibly replace the version with RUN ln -fs /usr/bin/python3.8 /usr/bin/python3

Install python 3.5 inside docker with a base image centos7

I am trying to install python 3.5 inside docker with a base image centos7. This is our Dockerfile
FROM base-centos7:0.0.8
# Install basic tools
RUN yum install -y which vim wget git gcc
# Install python 3.5
RUN yum install -y https://repo.ius.io/ius-release-el7.rpm \
&& yum update -y \
&& yum install -y python35u python35u-libs python35u-devel python35u-pip
RUN python3.5 -m pip install --upgrade pip
But during the build, docker build image is failing with the following errors
executor failed running [/bin/sh -c yum install -y https://repo.ius.io/ius-release-el7.rpm
&& yum update -y
&& sudo yum install -y python35u python35u-libs python35u-devel python35u-pip]: exit code: 127.
Can anyone guide me in resolving this issue. and why am I seeing this issue in very first place.
You can use python image from docker hub
https://hub.docker.com/_/python
Example of dockerfile :
FROM python:3.6
RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "/code/app.py"]
i think it's easy , isn't ?
the centos repo uses:
FROM centos/s2i-base-centos7
EXPOSE 8080
ENV PYTHON_VERSION=3.5 \
PATH=$HOME/.local/bin/:$PATH \
PYTHONUNBUFFERED=1 \
PYTHONIOENCODING=UTF-8 \
LC_ALL=en_US.UTF-8 \
LANG=en_US.UTF-8 \
PIP_NO_CACHE_DIR=off
RUN INSTALL_PKGS="rh-python35 rh-python35-python-devel rh-python35-python-setuptools rh-python35-python-pip nss_wrapper \
httpd24 httpd24-httpd-devel httpd24-mod_ssl httpd24-mod_auth_kerb httpd24-mod_ldap \
httpd24-mod_session atlas-devel gcc-gfortran libffi-devel libtool-ltdl enchant" && \
yum install -y centos-release-scl && \
yum -y --setopt=tsflags=nodocs install --enablerepo=centosplus $INSTALL_PKGS && \
rpm -V $INSTALL_PKGS && \
# Remove centos-logos (httpd dependency) to keep image size smaller.
rpm -e --nodeps centos-logos && \
yum -y clean all --enablerepo='*'
source here
The problem is not difficult, I build the image changing
FROM base-centos7:0.0.8 ====> FROM centos:7
You can consult the images version of centos in https://hub.docker.com/_/centos
PD: The container showed: errro exited(1), you should focus on the main process.

How can I install my python code in a docker container?

I have a python 3.6 app that I would like to install on a docker container. I installed the app in a virtual env (miniconda3) on my pc (windows 10) and need to do the same in a docker container (python3.6 base image (linux)). I installed the app locally using a setup.py file using python setup.py install, and an egg file as well as a separate directory with the application's code were created in site-packages. When I tried to do the same in the docker container, the installation creates only an egg file in site packages, and the app can't be imported. The other installed packages are fine.
Dockerfile:
FROM python:3.6
WORKDIR /opt
# create a virtual environment and add it to PATH so that it is
applied for all future RUN and CMD calls
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
# Install Mono for pythonnet.
RUN apt-get update \
&& apt-get install --yes \
apt-transport-https \
git \
dirmngr \
clang \
gnupg \
ca-certificates \
# Dependency for pyodbc.
unixodbc-dev \
&& apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-
keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF \
&& echo "deb http://download.mono-project.com/repo/debian
stretch/snapshots/5.20 main" | tee /etc/apt/sources.list.d/mono-
official-stable.list \
&& apt-get update \
&& apt-get install --yes \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
COPY src ./src
COPY setup.py ./setup.py
COPY config.json ./config.json
COPY BCUtility.dll ./BCUtility.dll
COPY settings.ini ./settings.ini
COPY redis_config.json ./redis_config.json
COPY sql_config.json ./sql_config.json
RUN python3 -m venv $VIRTUAL_ENV \
# From here on, use virtual env's python.
&& venv/bin/pip install --upgrade pip \
&& venv/bin/pip install --no-cache-dir --upgrade pip setuptools wheel \
&& venv/bin/pip install --no-cache-dir -r requirements.txt \
# Dependency for pythonnet.
&& venv/bin/pip install --no-cache-dir pycparser \
&& venv/bin/pip install -U --no-cache-dir "pythonnet==2.5.1" \
# install the app
&& venv/bin/python setup.py install
cmd /opt/venv/bin/python src/app.py
Any idea where I'm going wrong?

How can i upgrade pip's setup tools

I am quite new to Docker/Python and trying to update an existing AWX's Docker Image's Dockerfile to ensure that i have the latest versions of the following packages in /usr/lib/python3.6/site-packages
/usr/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py
/usr/lib/python3.6/site-packages/pip/_vendor/urllib3/connectionpool.py
/usr/lib/python3.6/site-packages/pip/_vendor/urllib3/poolmanager.py
/usr/lib/python3.6/site-packages/pip/_vendor/urllib3/util/retry.py
Following is my environment info:
Docker version 19.03.3, build a872fc2f86
Docker Base Image: centos:latest
The Dockerfile in which i am making changes for this is available here.
From what i have read from multiple posts/blogs so far, this should normally be updated by a simple:
pip3 install --upgrade pip
but when i run this, i am getting the following (and it shows a different path which includes local in the path.
bash-4.4# pip3 install --upgrade pip
Requirement already up-to-date: pip in /usr/local/lib/python3.6/site-packages (19.3.1)
I have even tried the following but this didn't help:
bash-4.4# python3 -m ensurepip --upgrade
Requirement already up-to-date: setuptools in /usr/lib/python3.6/site-packages
Requirement already up-to-date: pip in /usr/local/lib/python3.6/site-packages
bash-4.4# pip3 install --upgrade setuptools
Requirement already up-to-date: setuptools in /usr/local/lib/python3.6/site-packages (42.0.2)
This is how the above changes were applied in the Dockerfile
In this Dockerfile, i have added the following lines after line 124 in the Dockerfile to update pip packages:
RUN yum update -y
RUN pip2 install --upgrade pip
RUN pip3 install --upgrade pip
Snapshot of required sections from Dockerfile
FROM centos:latest
USER root
# sync with installer/roles/image_build/templates/Dockerfile.j2
RUN dnf -y update && \
dnf -y install epel-release 'dnf-command(config-manager)' && \
dnf module -y enable 'postgresql:10' && \
dnf config-manager --set-enabled PowerTools && \
.
.
.
ansible \
python3-devel \
python3-libselinux \
python3-pip \
python3-psycopg2 \
python3-setuptools \
dnf-utils
ADD https://github.com/krallin/tini/releases/download/v0.14.0/tini /tini
RUN chmod +x /tini
RUN python3 -m ensurepip && pip3 install virtualenv
RUN pip3 install supervisor
ADD Makefile /tmp/Makefile
RUN mkdir /tmp/requirements
ADD requirements/requirements_ansible.txt \
requirements/requirements_ansible_uninstall.txt \
requirements/requirements_ansible_git.txt \
requirements/requirements.txt \
requirements/requirements_tower_uninstall.txt \
requirements/requirements_git.txt \
/tmp/requirements/
RUN cd /tmp && VENV_BASE="/var/lib/awx/venv" make requirements_awx requirements_ansible_py3
.
.
.
RUN echo "{{ awx_version }}" > /var/lib/awx/.tower_version
COPY {{ awx_sdist_file }} /tmp/{{ awx_sdist_file }}
RUN OFFICIAL=yes /var/lib/awx/venv/awx/bin/pip install /tmp/{{ awx_sdist_file }}
Somehow the changes are not getting reflected in the correct path. Can someone please suggest how to update the packages inside: /usr/lib/python3.6/site-packages

Is sklearn compatible with Linux-alpine?

I get an error when I try to build an alpine based docker image that includes the sklearn package.
I've tried a few variations of pip installation, different package combinations, and outdated versions of sklearn to see if they are compatible. I've also run the container in -it mode and tried to install the package manually from there. When I remove the sklearn line, the Dockerfile builds and the container runs just fine. Sklearn works in an Ubuntu:latest Dockerfile I've built, but I'm trying to reduce my footprint, so I was hoping to get it to work on alpine...
Here's my Dockerfile code:
FROM alpine:latest
RUN apk upgrade --no-cache \
&& apk update \
&& apk add --no-cache \
musl \
build-base \
python3 \
python3-dev \
postgresql-dev \
bash \
git \
&& pip3 install --no-cache-dir --upgrade pip \
&& pip3 install sklearn \
&& rm -rf /var/cache/* \
&& rm -rf /root/.cache/*
And here's the error I'm getting:
ERROR: Command "/usr/bin/python3.6 /usr/lib/python3.6/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /tmp/tmpqjsz0004" failed with error code 1 in /tmp/pip-install-xlvbli9u/scipy
Alpine Linux doesn't support PEP 513. I found that something like this works:
RUN apk add --no-cache gcc g++ gfortran lapack-dev libffi-dev libressl-dev musl-dev && \
mkdir scipy && cd scipy && \
wget https://github.com/scipy/scipy/releases/download/v1.3.2/scipy-1.3.2.tar.gz && \
tar -xvf scipy-1.3.2.tar.gz && \
cd scipy-1.3.2 && \
python3 -m pip --no-cache-dir install .

Resources