Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

Commit 26089d4

Browse files
authored
Add support for building docker images with specified python version (#2154)
* Changes to support different python versions * Updated default version to 3.9 * spellcheck lint
1 parent b8f52d8 commit 26089d4

File tree

5 files changed

+51
-28
lines changed

5 files changed

+51
-28
lines changed

docker/Dockerfile

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,36 @@ ARG BASE_IMAGE=ubuntu:rolling
2020

2121
FROM ${BASE_IMAGE} AS compile-image
2222
ARG BASE_IMAGE=ubuntu:rolling
23+
ARG PYTHON_VERSION=3.9
2324
ENV PYTHONUNBUFFERED TRUE
2425

2526
RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
2627
#apt --fix-broken -y install && \
2728
apt-get update && \
29+
apt-get upgrade -y && \
30+
apt-get install software-properties-common -y && \
31+
add-apt-repository -y ppa:deadsnakes/ppa && \
2832
apt remove python-pip python3-pip && \
2933
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
3034
ca-certificates \
3135
g++ \
32-
python3.8 \
33-
python3.8-dev \
34-
python3.8-distutils \
35-
python3.8-venv \
36+
python$PYTHON_VERSION \
37+
python$PYTHON_VERSION-dev \
38+
python3-distutils \
39+
python$PYTHON_VERSION-venv \
3640
python3-venv \
3741
openjdk-17-jdk \
3842
curl \
3943
&& rm -rf /var/lib/apt/lists/* \
4044
&& cd /tmp \
4145
&& curl -O https://bootstrap.pypa.io/get-pip.py \
42-
&& python3.8 get-pip.py
46+
&& python$PYTHON_VERSION get-pip.py
4347

4448

45-
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 \
46-
&& update-alternatives --install /usr/local/bin/pip pip /usr/local/bin/pip3.8 1
49+
RUN update-alternatives --install /usr/bin/python python /usr/bin/python$PYTHON_VERSION 1 \
50+
&& update-alternatives --install /usr/local/bin/pip pip /usr/local/bin/pip3 1
4751

48-
RUN python3.8 -m venv /home/venv
52+
RUN python$PYHON_VERSION -m venv /home/venv
4953

5054
ENV PATH="/home/venv/bin:$PATH"
5155

@@ -81,9 +85,9 @@ ENV PYTHONUNBUFFERED TRUE
8185
RUN --mount=type=cache,target=/var/cache/apt \
8286
apt-get update && \
8387
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
84-
python3.8 \
85-
python3.8-distutils \
86-
python3.8-dev \
88+
python$PYTHON_VERSION \
89+
python3-distutils \
90+
python$PYTHON_VERSION-dev \
8791
# using openjdk-17-jdk due to circular dependency(ca-certificates) bug in openjdk-17-jre-headless debian package
8892
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1009905
8993
openjdk-17-jdk \

docker/Dockerfile.dev

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# syntax = docker/dockerfile:experimental
22
#
33
# Following comments have been shamelessly copied from https://github.com/pytorch/pytorch/blob/master/Dockerfile
4-
#
4+
#
55
# NOTE: To build this you will need a docker version > 18.06 with
66
# experimental enabled and DOCKER_BUILDKIT=1
77
#
88
# If you do not use buildkit you are not going to have a good time
99
#
10-
# For reference:
10+
# For reference:
1111
# https://docs.docker.com/develop/develop-images/build_enhancements/
1212

1313
ARG BASE_IMAGE=ubuntu:rolling
@@ -22,36 +22,40 @@ ARG CUDA_VERSION
2222
ARG BUILD_WITH_IPEX
2323
ARG IPEX_VERSION=1.11.0
2424
ARG IPEX_URL=https://software.intel.com/ipex-whl-stable
25+
ARG PYTHON_VERSION=3.9
2526

2627
ENV PYTHONUNBUFFERED TRUE
2728

2829
RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
2930
apt-get update && \
31+
apt-get upgrade -y && \
32+
apt-get install software-properties-common -y && \
33+
add-apt-repository -y ppa:deadsnakes/ppa && \
3034
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
3135
fakeroot \
3236
ca-certificates \
3337
dpkg-dev \
3438
sudo \
3539
g++ \
3640
git \
37-
python3.8 \
38-
python3.8-dev \
39-
python3.8-venv \
40-
python3.8-distutils \
41-
# python3-venv \
41+
python$PYTHON_VERSION \
42+
python$PYTHON_VERSION-dev \
43+
python3-distutils \
44+
python$PYTHON_VERSION-venv \
45+
python3-venv \
4246
build-essential \
4347
openjdk-17-jdk \
4448
curl \
4549
vim \
46-
numactl \
50+
numactl \
4751
&& if [ "$BUILD_WITH_IPEX" = "true" ]; then apt-get update && apt-get install -y libjemalloc-dev libgoogle-perftools-dev libomp-dev && ln -s /usr/lib/x86_64-linux-gnu/libjemalloc.so /usr/lib/libjemalloc.so && ln -s /usr/lib/x86_64-linux-gnu/libtcmalloc.so /usr/lib/libtcmalloc.so && ln -s /usr/lib/x86_64-linux-gnu/libiomp5.so /usr/lib/libiomp5.so; fi \
4852
&& rm -rf /var/lib/apt/lists/* \
4953
&& cd /tmp \
5054
&& curl -O https://bootstrap.pypa.io/get-pip.py \
51-
&& python3.8 get-pip.py
55+
&& python$PYTHON_VERSION get-pip.py
5256

53-
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1 \
54-
&& update-alternatives --install /usr/local/bin/pip pip /usr/local/bin/pip3.8 1
57+
RUN update-alternatives --install /usr/bin/python python /usr/bin/python$PYTHON_VERSION 1 \
58+
&& update-alternatives --install /usr/local/bin/pip pip /usr/local/bin/pip3 1
5559

5660
# Build Dev Image
5761
FROM compile-image AS dev-image
@@ -61,9 +65,9 @@ RUN if [ "$MACHINE_TYPE" = "gpu" ]; then export USE_CUDA=1; fi \
6165
&& git clone https://github.com/pytorch/serve.git \
6266
&& cd serve \
6367
&& git checkout ${BRANCH_NAME} \
64-
&& python3.8 -m venv /home/venv \
68+
&& python$PYTHON_VERSION -m venv /home/venv \
6569
&& python -m pip install -U pip setuptools \
66-
&& if [ -z "$CUDA_VERSION" ]; then python ts_scripts/install_dependencies.py --environment=dev; else python ts_scripts/install_dependencies.py --environment=dev --cuda $CUDA_VERSION; fi \
70+
&& if [ -z "$CUDA_VERSION" ]; then python ts_scripts/install_dependencies.py --environment=dev; else python ts_scripts/install_dependencies.py --environment=dev --cuda $CUDA_VERSION; fi \
6771
&& if [ "$BUILD_WITH_IPEX" = "true" ]; then python -m pip install --no-cache-dir intel_extension_for_pytorch==${IPEX_VERSION} -f ${IPEX_URL}; fi \
6872
&& python ts_scripts/install_from_src.py \
6973
&& useradd -m model-server \

docker/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Use `build_image.sh` script to build the docker images. The script builds the `p
3939
|-cv, --cudaversion| Specify to cuda version to use. Supported values `cu92`, `cu101`, `cu102`, `cu111`, `cu113`, `cu116`, `cu117`. Default `cu117`|
4040
|-ipex, --build-with-ipex| Specify to build with intel_extension_for_pytorch. If not specified, script builds without intel_extension_for_pytorch.|
4141
|--codebuild| Set if you need [AWS CodeBuild](https://aws.amazon.com/codebuild/)|
42+
|-py, --pythonversion| Specify the python version to use. Supported values `3.8`, `3.9`, `3.10`. Default `3.9`|
4243

4344

4445
**PRODUCTION ENVIRONMENT IMAGES**

docker/build_image.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CUDA_VERSION=""
1111
UBUNTU_VERSION="ubuntu:20.04"
1212
USE_LOCAL_SERVE_FOLDER=false
1313
BUILD_WITH_IPEX=false
14+
PYTHON_VERSION=3.9
1415

1516
for arg in "$@"
1617
do
@@ -25,6 +26,7 @@ do
2526
echo "-t, --tag specify tag name for docker image"
2627
echo "-lf, --use-local-serve-folder specify this option for the benchmark image if the current 'serve' folder should be used during automated benchmarks"
2728
echo "-ipex, --build-with-ipex specify to build with intel_extension_for_pytorch"
29+
echo "-py, --pythonversion specify to python version to use: Possible values: 3.8 3.9 3.10"
2830
exit 0
2931
;;
3032
-b|--branch_name)
@@ -64,6 +66,17 @@ do
6466
BUILD_WITH_IPEX=true
6567
shift
6668
;;
69+
-py|--pythonversion)
70+
PYTHON_VERSION="$2"
71+
if [[ $PYTHON_VERSION = 3.8 || $PYTHON_VERSION = 3.9 || $PYTHON_VERSION = 3.10 ]]; then
72+
echo "Valid python version"
73+
else
74+
echo "Valid python versions are 3.8, 3.9 and 3.10"
75+
exit 1
76+
fi
77+
shift
78+
shift
79+
;;
6780
# With default ubuntu version 20.04
6881
-cv|--cudaversion)
6982
CUDA_VERSION="$2"
@@ -116,10 +129,10 @@ fi
116129

117130
if [ $BUILD_TYPE == "production" ]
118131
then
119-
DOCKER_BUILDKIT=1 docker build --file Dockerfile --build-arg BASE_IMAGE=$BASE_IMAGE --build-arg CUDA_VERSION=$CUDA_VERSION -t $DOCKER_TAG .
132+
DOCKER_BUILDKIT=1 docker build --file Dockerfile --build-arg BASE_IMAGE=$BASE_IMAGE --build-arg CUDA_VERSION=$CUDA_VERSION --build-arg PYTHON_VERSION=$PYTHON_VERSION -t $DOCKER_TAG .
120133
elif [ $BUILD_TYPE == "benchmark" ]
121134
then
122-
DOCKER_BUILDKIT=1 docker build --pull --no-cache --file Dockerfile.benchmark --build-arg USE_LOCAL_SERVE_FOLDER=$USE_LOCAL_SERVE_FOLDER --build-arg BASE_IMAGE=$BASE_IMAGE --build-arg BRANCH_NAME=$BRANCH_NAME --build-arg CUDA_VERSION=$CUDA_VERSION --build-arg MACHINE_TYPE=$MACHINE -t $DOCKER_TAG .
135+
DOCKER_BUILDKIT=1 docker build --pull --no-cache --file Dockerfile.benchmark --build-arg USE_LOCAL_SERVE_FOLDER=$USE_LOCAL_SERVE_FOLDER --build-arg BASE_IMAGE=$BASE_IMAGE --build-arg BRANCH_NAME=$BRANCH_NAME --build-arg CUDA_VERSION=$CUDA_VERSION --build-arg MACHINE_TYPE=$MACHINE --build-arg PYTHON_VERSION=$PYTHON_VERSION -t $DOCKER_TAG .
123136
else
124-
DOCKER_BUILDKIT=1 docker build --pull --no-cache --file Dockerfile.dev -t $DOCKER_TAG --build-arg BUILD_TYPE=$BUILD_TYPE --build-arg BASE_IMAGE=$BASE_IMAGE --build-arg BRANCH_NAME=$BRANCH_NAME --build-arg CUDA_VERSION=$CUDA_VERSION --build-arg MACHINE_TYPE=$MACHINE --build-arg BUILD_WITH_IPEX=$BUILD_WITH_IPEX .
137+
DOCKER_BUILDKIT=1 docker build --pull --no-cache --file Dockerfile.dev -t $DOCKER_TAG --build-arg BUILD_TYPE=$BUILD_TYPE --build-arg BASE_IMAGE=$BASE_IMAGE --build-arg BRANCH_NAME=$BRANCH_NAME --build-arg CUDA_VERSION=$CUDA_VERSION --build-arg MACHINE_TYPE=$MACHINE --build-arg BUILD_WITH_IPEX=$BUILD_WITH_IPEX --build-arg PYTHON_VERSION=$PYTHON_VERSION .
125138
fi

ts_scripts/spellcheck_conf/wordlist.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,4 +1022,5 @@ vec
10221022
torchdata
10231023
CodeQL
10241024
Dependabot
1025-
Snyk
1025+
Snyk
1026+
pythonversion

0 commit comments

Comments
 (0)