Skip to content

Instantly share code, notes, and snippets.

@satyrius
Created March 28, 2016 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satyrius/8fbebe22827b978b9df5 to your computer and use it in GitHub Desktop.
Save satyrius/8fbebe22827b978b9df5 to your computer and use it in GitHub Desktop.
Python 2.7 minimal Docker image (with psycopg2, nginx and supervisor)
FROM debian:jessie
ENV PYENV_ROOT=/opt/pyenv PATH=/opt/pyenv/shims:/opt/pyenv/bin:$PATH LANG=en_US.UTF-8
RUN set -x \
&& echo "LANG=$LANG" > /etc/default/locale \
&& echo "$LANG UTF-8" > /etc/locale.gen \
&& export DEBIAN_FRONTEND=noninteractive \
&& apt-get update -qq \
&& apt-get install -y locales \
&& apt-get install -y \
libpq5 \
nginx-light \
&& BUILD_DEPS='build-essential curl git-core zlib1g-dev libssl-dev libbz2-dev libreadline-dev libpq-dev' \
&& apt-get install -y ${BUILD_DEPS} \
&& PYTHON_VERSION=2.7.11 \
&& curl --silent --show-error --retry 5 https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash \
&& pyenv update \
&& pyenv install ${PYTHON_VERSION} && pyenv global ${PYTHON_VERSION} \
&& rm -rf ${PYENV_ROOT}/plugins \
&& pip install --upgrade pip && pip --version \
&& pip install \
psycopg2==2.6.1 \
supervisor==3.2.3 \
&& apt-get autoremove -y ${BUILD_DEPS} \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
@satyrius
Copy link
Author

The key to minimify image is to remove all build dependencies in the same step. The result is 282.5 Mb

Build dependencies:

  • It's a good practice to install exact Python, use pyenv for it;
  • There are a lot of packages you have to install to build python, they are listed in BUILD_DEPS. And you should remove them after build.

About runtime dependencies:

  • libpq5 is required for psycopg2 (if you use PostgreSQL for you app);
  • nginx-light vs nginx-full is 1Mb vs _50Mb_6 sj use first;
  • I use supervisor as init process for my containers, you should install it using pip to do not install extra system dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment