|
| 1 | +# syntax=docker/dockerfile:1 |
| 2 | +ARG BASE_IMAGE=docker.io/ubuntu:noble-20251013 |
| 3 | +# hadolint ignore=DL3006 |
| 4 | +FROM $BASE_IMAGE |
| 5 | + |
| 6 | +# ensure local python is preferred over distribution python |
| 7 | +ENV PATH=/usr/local/bin:$PATH |
| 8 | + |
| 9 | +# runtime dependencies |
| 10 | +RUN set -eux; \ |
| 11 | + apt-get update; \ |
| 12 | + apt-get install -y --no-install-recommends \ |
| 13 | + ca-certificates \ |
| 14 | + netbase \ |
| 15 | + tzdata \ |
| 16 | + ; \ |
| 17 | + rm -rf /var/lib/apt/lists/* |
| 18 | + |
| 19 | +ENV PYTHON_VERSION=3.14.2 |
| 20 | +ENV PYTHON_SHA256=ce543ab854bc256b61b71e9b27f831ffd1bfd60a479d639f8be7f9757cf573e9 |
| 21 | + |
| 22 | +SHELL ["/bin/bash", "-o", "pipefail", "-c"] |
| 23 | +# hadolint ignore=DL3003,DL3047,SC2086 |
| 24 | +RUN set -eux; \ |
| 25 | + \ |
| 26 | + # Snapshot *existing* manual packages (Ubuntu marks many base packages manual) |
| 27 | + before="$(apt-mark showmanual | tr ' ' '\n' | sed '/^$/d' | sort -u)"; \ |
| 28 | + \ |
| 29 | + apt-get update; \ |
| 30 | + apt-get install -y --no-install-recommends \ |
| 31 | + dpkg-dev \ |
| 32 | + gcc \ |
| 33 | + gnupg \ |
| 34 | + libbluetooth-dev \ |
| 35 | + libbz2-dev \ |
| 36 | + libc6-dev \ |
| 37 | + libdb-dev \ |
| 38 | + libffi-dev \ |
| 39 | + libgdbm-dev \ |
| 40 | + liblzma-dev \ |
| 41 | + libncursesw5-dev \ |
| 42 | + libreadline-dev \ |
| 43 | + libsqlite3-dev \ |
| 44 | + libssl-dev \ |
| 45 | + libzstd-dev \ |
| 46 | + make \ |
| 47 | + tk-dev \ |
| 48 | + uuid-dev \ |
| 49 | + wget \ |
| 50 | + xz-utils \ |
| 51 | + zlib1g-dev \ |
| 52 | + ; \ |
| 53 | + \ |
| 54 | + wget -O python.tar.xz "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz"; \ |
| 55 | + echo "$PYTHON_SHA256 *python.tar.xz" | sha256sum -c -; \ |
| 56 | + mkdir -p /usr/src/python; \ |
| 57 | + tar --extract --directory /usr/src/python --strip-components=1 --file python.tar.xz; \ |
| 58 | + rm python.tar.xz; \ |
| 59 | + \ |
| 60 | + cd /usr/src/python; \ |
| 61 | + gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \ |
| 62 | + ./configure \ |
| 63 | + --build="$gnuArch" \ |
| 64 | + --enable-loadable-sqlite-extensions \ |
| 65 | + --enable-optimizations \ |
| 66 | + --enable-option-checking=fatal \ |
| 67 | + --enable-shared \ |
| 68 | + $(test "${gnuArch%%-*}" != 'riscv64' && echo '--with-lto') \ |
| 69 | + --with-ensurepip \ |
| 70 | + ; \ |
| 71 | + nproc="$(nproc)"; \ |
| 72 | + EXTRA_CFLAGS="$(dpkg-buildflags --get CFLAGS)"; \ |
| 73 | + LDFLAGS="$(dpkg-buildflags --get LDFLAGS)"; \ |
| 74 | + LDFLAGS="${LDFLAGS:--Wl},--strip-all"; \ |
| 75 | + arch="$(dpkg --print-architecture)"; arch="${arch##*-}"; \ |
| 76 | +# https://docs.python.org/3.12/howto/perf_profiling.html |
| 77 | +# https://github.com/docker-library/python/pull/1000#issuecomment-2597021615 |
| 78 | + case "$arch" in \ |
| 79 | + amd64|arm64) \ |
| 80 | + # only add "-mno-omit-leaf" on arches that support it |
| 81 | + # https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/x86-Options.html#index-momit-leaf-frame-pointer-2 |
| 82 | + # https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/AArch64-Options.html#index-momit-leaf-frame-pointer |
| 83 | + EXTRA_CFLAGS="${EXTRA_CFLAGS:-} -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer"; \ |
| 84 | + ;; \ |
| 85 | + i386) \ |
| 86 | + # don't enable frame-pointers on 32bit x86 due to performance drop. |
| 87 | + ;; \ |
| 88 | + *) \ |
| 89 | + # other arches don't support "-mno-omit-leaf" |
| 90 | + EXTRA_CFLAGS="${EXTRA_CFLAGS:-} -fno-omit-frame-pointer"; \ |
| 91 | + ;; \ |
| 92 | + esac; \ |
| 93 | + make -j "$nproc" \ |
| 94 | + "EXTRA_CFLAGS=${EXTRA_CFLAGS:-}" \ |
| 95 | + "LDFLAGS=${LDFLAGS:-}" \ |
| 96 | + ; \ |
| 97 | +# https://github.com/docker-library/python/issues/784 |
| 98 | +# prevent accidental usage of a system installed libpython of the same version |
| 99 | + rm python; \ |
| 100 | + make -j "$nproc" \ |
| 101 | + "EXTRA_CFLAGS=${EXTRA_CFLAGS:-}" \ |
| 102 | + "LDFLAGS=${LDFLAGS:--Wl},-rpath='\$\$ORIGIN/../lib'" \ |
| 103 | + python \ |
| 104 | + ; \ |
| 105 | + make install; \ |
| 106 | + \ |
| 107 | + cd /; \ |
| 108 | + rm -rf /usr/src/python; \ |
| 109 | + \ |
| 110 | + find /usr/local -depth \ |
| 111 | + \( \ |
| 112 | + \( -type d -a \( -name test -o -name tests -o -name idle_test \) \) \ |
| 113 | + -o \( -type f -a \( -name '*.pyc' -o -name '*.pyo' -o -name 'libpython*.a' \) \) \ |
| 114 | + \) -exec rm -rf '{}' + \ |
| 115 | + ; \ |
| 116 | + \ |
| 117 | + ldconfig; \ |
| 118 | + \ |
| 119 | + # Identify runtime-shared lib providers and mark them manual (so purge keeps them) |
| 120 | + find /usr/local -type f -executable -not \( -name '*tkinter*' \) -exec ldd '{}' ';' \ |
| 121 | + | awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next } ; gsub("^/(usr/)?", "", so); printf "*%s\n", so }' \ |
| 122 | + | sort -u \ |
| 123 | + | xargs -rt dpkg-query --search \ |
| 124 | +# https://manpages.debian.org/bookworm/dpkg/dpkg-query.1.en.html#S (we ignore diversions and it'll be really unusual for more than one package to provide any given .so file) |
| 125 | + | awk 'sub(":$", "", $1) { print $1 }' \ |
| 126 | + | sort -u \ |
| 127 | + | xargs -r apt-mark manual \ |
| 128 | + ; \ |
| 129 | + \ |
| 130 | + # Compute *new* manuals introduced by our build, then flip only those back to auto |
| 131 | + after="$(apt-mark showmanual | tr ' ' '\n' | sed '/^$/d' | sort -u)"; \ |
| 132 | + new_manual="$(comm -13 <(printf '%s\n' "${before}") <(printf '%s\n' "${after}"))"; \ |
| 133 | + if [ -n "${new_manual}" ]; then \ |
| 134 | + printf '%s\n' "${new_manual}" | xargs -r apt-mark auto; \ |
| 135 | + fi; \ |
| 136 | + \ |
| 137 | + # Remove build deps that are no longer needed; clean caches |
| 138 | + apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ |
| 139 | + rm -rf /var/lib/apt/lists/*; \ |
| 140 | + \ |
| 141 | + export PYTHONDONTWRITEBYTECODE=1; \ |
| 142 | + python3 --version; \ |
| 143 | + pip3 --version |
| 144 | + |
| 145 | +# make some useful symlinks that are expected to exist ("/usr/local/bin/python" and friends) |
| 146 | +SHELL ["/bin/bash", "-o", "pipefail", "-c"] |
| 147 | +RUN set -eux; \ |
| 148 | + for src in idle3 pip3 pydoc3 python3 python3-config; do \ |
| 149 | + dst="$(echo "$src" | tr -d 3)"; \ |
| 150 | + [ -s "/usr/local/bin/$src" ]; \ |
| 151 | + [ ! -e "/usr/local/bin/$dst" ]; \ |
| 152 | + ln -svT "$src" "/usr/local/bin/$dst"; \ |
| 153 | + done |
| 154 | + |
| 155 | +CMD ["python3"] |
0 commit comments