Minimal Python Build in Docker Container

sprin.dev at gmail.com sprin.dev at gmail.com
Mon Aug 11 01:45:17 EDT 2014


Hello,

I wanted to share what I've learned about making a reasonably minimal Docker image containing CPython built from source.

Motivation: Many popular distros are not able to provide packaged builds of the desired Python version. Operators of co-tenanted Python services have struggled since the dawn of time to manage the Matrix of Hell with Python apps - system Python version versus versions required by different applications versus shared lib versions, etc. We have made due with "almost good enough" tools like virtualenv, pyenv, and Software Collections. In spite of this, many operators just gave up and decided every Python service ought to live inside its own virtual machine. To me, it seems like we finally have a real solution:

Compile Python from source with the version and deps you need inside a Standard Container on top of the distro of your choosing.

Disclaimer: I have never been involved in the Python project or in the packaging of Python libs for any distro, so excuse me if this is naive.

If you, like me, have decided the key to sanity is in containerized Python services, you might try pulling the official Docker Python image:

python                 2.7.7               a87a2288ce78        2 weeks ago         1.041 GB

Hmm, Python 2.7.8 has been out for over a month with "regression and security fixes over 2.7.7". Also, over 1 GB? The "debian:wheezy" image with Python 2.7.3 installed from apt-get weighs in at 124 MB. And finally, this image is running on top of a pre-release version of the as-yet unreleased debian jessie.

So we have 3 very good reasons from staying away from the official Python Docker image.

Let's build our own. We chose Centos 7 as the standard base for both Docker hosts and guest containers. Here's what I did:

FROM centos:centos7

RUN yum install -y tar gcc make

RUN mkdir /usr/src/python
WORKDIR /usr/src/python
RUN curl -Sl "https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tar.xz" \
    | tar -xJ --strip-components=1
# You may want to verify the download with gpg: https://www.python.org/download

RUN ./configure \
    && make  -j$(nproc) \
    && make install \
    && make clean

# Clean up prior to flattening
RUN yum remove -y tar gcc make \
    && yum clean all \
    && rm -rf /usr/src/python

Beginning with the 244 MB Centos 7 base image, this yields a 369 MB image after flattening, with a compiled Python 2.7.8. While +125 MB to the base is not terrible, it seems like the image could still lose some weight.

Any ideas?

If you would like to check out the built image, it is at marina/python:2.7.8_r1 on the public registry. Of course I recommend building your own with whatever makes sense for your "Python base" for production use!



More information about the Python-list mailing list