|
| 1 | +############################################################################### |
| 2 | +# |
| 3 | +# Multi-stage Python 3.x build |
| 4 | +# |
| 5 | +# build-time environment variables: |
| 6 | +# LTO=1 . enable link-time-optimizations |
| 7 | +# OPTIMAL=1 . enable profile-guided-optimizations (PGO) |
| 8 | +# PYTHON_VERSION=3.5.3 |
| 9 | +# |
| 10 | +# ** NOTE **: |
| 11 | +# . LTO requires PGO |
| 12 | +# . ensure both variables are unset for typical builds |
| 13 | +# |
| 14 | +# building: |
| 15 | +# make build-image . run docker build |
| 16 | +# make build-push . push image to repository |
| 17 | +# make image . build + push |
| 18 | +# |
| 19 | +# Stages: |
| 20 | +# runtime <- debian-base-amd64:0.2 |
| 21 | +# common runtime packages go here |
| 22 | +# build-setup <- runtime |
| 23 | +# dev packages, tools, utilities, etc. go here |
| 24 | +# builder <- build-setup |
| 25 | +# ./configure <things> && make && make install |
| 26 | +# post-build <- builder |
| 27 | +# install any common python modules here |
| 28 | +# FINAL <- runtime |
| 29 | +# pip package installation goes here + ENTRYPOINT |
| 30 | +# |
| 31 | +############################################################################### |
| 32 | + |
| 33 | +FROM gcr.io/google-containers/debian-base-amd64:0.2 as runtime |
| 34 | + |
| 35 | +ENV PATH /usr/local/bin:$PATH |
| 36 | + |
| 37 | +# http://bugs.python.org/issue19846 |
| 38 | +# > At the moment, setting "LANG=C" on a Linux system *fundamentally breaks Python 3*, and that's not OK. |
| 39 | + |
| 40 | +ENV LANG C.UTF-8 |
| 41 | + |
| 42 | +COPY ./init-functions /lib/lsb/ |
| 43 | + |
| 44 | +RUN set -ex \ |
| 45 | + && apt-mark unhold apt gnupg libcap2 libsemanage1 passwd libbz2-1.0\ |
| 46 | + && runDeps='curl gnupg libsqlite3-0 zlib1g libexpat1 bash tcpdump procps less binutils libbz2-1.0 netcat-openbsd' \ |
| 47 | + && apt-get -qq update; apt-get install -y $runDeps \ |
| 48 | + && find /usr -type f -name "*.so" -exec strip --strip-unneeded {} + \ |
| 49 | + && apt-get remove binutils --purge -y -qq \ |
| 50 | + && find /var/lib/apt/lists \ |
| 51 | + /usr/share/man \ |
| 52 | + /usr/share/doc \ |
| 53 | + /var/log \ |
| 54 | + -type f -exec rm -f {} + \ |
| 55 | + && rm -rf /root/.gnupg \ |
| 56 | + && mkdir -p /root/.gnupg \ |
| 57 | + && chmod 700 /root/.gnupg |
| 58 | + |
| 59 | +LABEL stage RUNTIME |
| 60 | + |
| 61 | +############################################################################### |
| 62 | +FROM runtime as build-setup |
| 63 | + |
| 64 | +ADD gnupg/pubring.gpg gnupg/trustdb.gpg /root/.gnupg/ |
| 65 | + |
| 66 | +RUN set -ex \ |
| 67 | + && mkdir -p /root/.gnupg \ |
| 68 | + && chmod 700 /root/.gnupg \ |
| 69 | + && buildDeps='libsqlite3-dev zlib1g-dev libexpat1-dev libssl-dev xz-utils dpkg-dev binutils libbz2-dev libreadline-dev' \ |
| 70 | + && apt-get -qq update; apt-get -qq -y install ${buildDeps} |
| 71 | + |
| 72 | +ARG PYTHON_VERSION |
| 73 | + |
| 74 | +RUN curl -L -o /python.tar.xz "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz" \ |
| 75 | + && curl -L -o /python.tar.xz.asc "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc" \ |
| 76 | + && gpg --keyserver ha.pool.sks-keyservers.net --refresh-keys 2>&1 | egrep -v 'requesting key|not changed' \ |
| 77 | + && gpg --batch --verify /python.tar.xz.asc /python.tar.xz \ |
| 78 | + && mkdir -p /usr/src/python \ |
| 79 | + && tar -xJC /usr/src/python --strip-components=1 -f /python.tar.xz |
| 80 | + |
| 81 | + |
| 82 | +LABEL stage BUILD-SETUP |
| 83 | +LABEL version ${PYTHON_VERSION} |
| 84 | + |
| 85 | +############################################################################### |
| 86 | +FROM build-setup as builder |
| 87 | + |
| 88 | +ARG BUILD_ARGS |
| 89 | +ARG PYTHON_VERSION |
| 90 | +ENV LANG C.UTF-8 |
| 91 | + |
| 92 | +RUN set -ex \ |
| 93 | + && cd /usr/src/python \ |
| 94 | + && gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \ |
| 95 | + && [ $(( ` echo $PYTHON_VERSION | cut -d"." -f1 ` )) -lt 3 ] && BUILD_ARGS="" \ |
| 96 | + ; ./configure \ |
| 97 | + --build="$gnuArch" \ |
| 98 | + --enable-loadable-sqlite-extensions \ |
| 99 | + --enable-shared \ |
| 100 | + --with-system-expat \ |
| 101 | + --with-system-ffi \ |
| 102 | + --without-ensurepip ${BUILD_ARGS} \ |
| 103 | + && make -j $(( 1 * $( egrep '^processor[[:space:]]+:' /proc/cpuinfo | wc -l ) )) \ |
| 104 | + && make install |
| 105 | + |
| 106 | +RUN set -ex \ |
| 107 | + find /usr/local -type f -name "*.so" -exec strip --strip-unneeded {} + \ |
| 108 | + & ldconfig \ |
| 109 | + & find /usr/local -depth \ |
| 110 | + \( \ |
| 111 | + \( -type d -a \( -name test -o -name tests -o -name __pycache__ \) \) \ |
| 112 | + -o \ |
| 113 | + \( -type f -a \( -name '*.pyc' -o -name '*.pyo' \) \) \ |
| 114 | + -o \ |
| 115 | + \( -name "idle*" \) \ |
| 116 | + \) -exec rm -rf '{}' + \ |
| 117 | + && find /var/lib/apt/lists \ |
| 118 | + /usr/share/man \ |
| 119 | + /usr/share/doc \ |
| 120 | + /var/log \ |
| 121 | + -type f -exec rm -f {} + |
| 122 | + |
| 123 | +# make some useful symlinks that are expected to exist |
| 124 | +RUN ["/bin/bash", "-c", "if [[ $( echo ${PYTHON_VERSION} | cut -d'.' -f1 ) == '3' ]]; then cd /usr/local/bin && ln -sf pydoc3 pydoc && ln -sf python3 python && ln -sf python3-config python-config; fi"] |
| 125 | + |
| 126 | +LABEL stage BUILDER |
| 127 | +LABEL version ${PYTHON_VERSION} |
| 128 | + |
| 129 | +############################################################################### |
| 130 | +FROM builder as post-build |
| 131 | + |
| 132 | +# if this is called "PIP_VERSION", pip explodes with "ValueError: invalid truth value '<VERSION>'" |
| 133 | +ENV PYTHON_PIP_VERSION 9.0.1 |
| 134 | + |
| 135 | + |
| 136 | +COPY ./ipython_config.py / |
| 137 | + |
| 138 | +RUN set -ex; ldconfig |
| 139 | +RUN set -ex; curl -sL -o get-pip.py 'https://bootstrap.pypa.io/get-pip.py'; |
| 140 | +RUN set -ex; python get-pip.py \ |
| 141 | + --disable-pip-version-check \ |
| 142 | + --no-cache-dir \ |
| 143 | + "pip==$PYTHON_PIP_VERSION"; pip --version |
| 144 | + |
| 145 | +RUN mkdir -p $HOME/.ipython/profile_default ; |
| 146 | +RUN mv ipython_config.py $HOME/.ipython/profile_default/. ; |
| 147 | +RUN pip install 'ipython<6' ipdb |
| 148 | + |
| 149 | +RUN set -ex; \ |
| 150 | + find /usr/local -depth \ |
| 151 | + \( \ |
| 152 | + \( -type d -a \( -name test -o -name tests -o -name __pycache__ \) \) \ |
| 153 | + -o \ |
| 154 | + \( -type f -a \( -name '*.pyc' -o -name '*.pyo' -o -name '*.exe' \) \) \ |
| 155 | + \) -exec rm -rf '{}' +; |
| 156 | + |
| 157 | +ARG PYTHON_VERSION |
| 158 | +LABEL stage POST-BUILD |
| 159 | +LABEL version ${PYTHON_VERSION} |
| 160 | + |
| 161 | +############################################################################### |
| 162 | +FROM runtime |
| 163 | + |
| 164 | +COPY --from=post-build /usr/local /usr/local |
| 165 | +COPY --from=post-build /root /root |
| 166 | +RUN /sbin/ldconfig |
| 167 | + |
| 168 | +LABEL stage FINAL |
| 169 | +ARG PYTHON_VERSION |
| 170 | +LABEL version ${PYTHON_VERSION} |
| 171 | + |
| 172 | +CMD ["ipython"] |
0 commit comments