How to Deploy specific version of Python using Docker

tech kamar
2 min readMar 5, 2024

Python trouble

Python is a very easy to learn language and with the advent of Machine Leaning its the go to programming language.

The trouble with Python starts when you start using dependencies you use by installing them as PIP packages.

These libraries like NumPy internally uses C, C++, Fortran etc to run.

Say you had developed your app with Python3.10 and try deploying it in Linux docker container. While installing python from repository. It might give you Python3.12.

You might be thinking “Is that such a big issue?”

Answer is YES IT IS.

There is high chance that some your PIP packages won’t get installed. Some might break while compiling itself.

So how do i get rid of it?

The answer is simple. Build your own Python version from Scratch and use it.

So if your application runs well on Python3.10. You better download Python3.10 source code and build it.

Here is how to do it in Docker

  1. Choose a stable base image which doesn’t have much vulnerabilities and also uses less RAM to run. So my go to is always been Debian

Now lets see the Docker image

FROM debian:trixie


# Set the working directory inside the container

WORKDIR /app

# Copy the requirements file to the working directory
RUN mkdir /opt/python3.10

# To avoid .pyc files and save space
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Install all dependecnies you need to compile Python3.10
RUN apt update
RUN apt install -y wget libffi-dev gcc build-essential curl tcl-dev tk-dev uuid-dev lzma-dev liblzma-dev libssl-dev libsqlite3-dev

# Download Python source code from official site and build it
RUN wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz
RUN tar -zxvf Python-3.10.0.tgz
RUN cd Python-3.10.0 && ./configure --prefix=/opt/python3.10 && make && make install

# Delete the python source code and temp files
RUN rm Python-3.10.0.tgz
RUN rm -r Python-3.10.0/

# Now link it so that $python works
RUN ln -s /opt/python3.10/python3.10 /usr/bin/python

# Now add your extra commands below to run your own applications. Thank you

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response