|
| 1 | +# |
| 2 | +#-------------------------------------------------------------------------- |
| 3 | +# python 3.12 alpine |
| 4 | +#-------------------------------------------------------------------------- |
| 5 | +# |
| 6 | + |
| 7 | +FROM alpine:3.21 |
| 8 | + |
| 9 | +# ensure local python is preferred over distribution python |
| 10 | +ENV PATH /usr/local/bin:$PATH |
| 11 | + |
| 12 | +# cannot remove LANG even though https://bugs.python.org/issue19846 is fixed |
| 13 | +# last attempted removal of LANG broke many users: |
| 14 | +# https://github.com/docker-library/python/pull/570 |
| 15 | +ENV LANG C.UTF-8 |
| 16 | + |
| 17 | +# runtime dependencies |
| 18 | +RUN set -eux; \ |
| 19 | + apk add --no-cache \ |
| 20 | + ca-certificates \ |
| 21 | + tzdata \ |
| 22 | + ; |
| 23 | + |
| 24 | +ENV GPG_KEY A035C8C19219BA821ECEA86B64E628F8D684696D |
| 25 | +ENV PYTHON_VERSION 3.11.12 |
| 26 | +ENV PYTHON_SHA256 849da87af4df137710c1796e276a955f7a85c9f971081067c8f565d15c352a09 |
| 27 | + |
| 28 | +RUN set -eux; \ |
| 29 | + \ |
| 30 | + apk add --no-cache --virtual .build-deps \ |
| 31 | + gnupg \ |
| 32 | + tar \ |
| 33 | + xz \ |
| 34 | + \ |
| 35 | + bluez-dev \ |
| 36 | + bzip2-dev \ |
| 37 | + dpkg-dev dpkg \ |
| 38 | + findutils \ |
| 39 | + gcc \ |
| 40 | + gdbm-dev \ |
| 41 | + libc-dev \ |
| 42 | + libffi-dev \ |
| 43 | + libnsl-dev \ |
| 44 | + libtirpc-dev \ |
| 45 | + linux-headers \ |
| 46 | + make \ |
| 47 | + ncurses-dev \ |
| 48 | + openssl-dev \ |
| 49 | + pax-utils \ |
| 50 | + readline-dev \ |
| 51 | + sqlite-dev \ |
| 52 | + tcl-dev \ |
| 53 | + tk \ |
| 54 | + tk-dev \ |
| 55 | + util-linux-dev \ |
| 56 | + xz-dev \ |
| 57 | + zlib-dev \ |
| 58 | + ; \ |
| 59 | + \ |
| 60 | + wget -O python.tar.xz "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz"; \ |
| 61 | + echo "$PYTHON_SHA256 *python.tar.xz" | sha256sum -c -; \ |
| 62 | + wget -O python.tar.xz.asc "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc"; \ |
| 63 | + GNUPGHOME="$(mktemp -d)"; export GNUPGHOME; \ |
| 64 | + gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys "$GPG_KEY"; \ |
| 65 | + gpg --batch --verify python.tar.xz.asc python.tar.xz; \ |
| 66 | + gpgconf --kill all; \ |
| 67 | + rm -rf "$GNUPGHOME" python.tar.xz.asc; \ |
| 68 | + mkdir -p /usr/src/python; \ |
| 69 | + tar --extract --directory /usr/src/python --strip-components=1 --file python.tar.xz; \ |
| 70 | + rm python.tar.xz; \ |
| 71 | + \ |
| 72 | + cd /usr/src/python; \ |
| 73 | + gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \ |
| 74 | + ./configure \ |
| 75 | + --build="$gnuArch" \ |
| 76 | + --enable-loadable-sqlite-extensions \ |
| 77 | + --enable-option-checking=fatal \ |
| 78 | + --enable-shared \ |
| 79 | + --with-lto \ |
| 80 | + --with-ensurepip \ |
| 81 | + ; \ |
| 82 | + nproc="$(nproc)"; \ |
| 83 | +# set thread stack size to 1MB so we don't segfault before we hit sys.getrecursionlimit() |
| 84 | +# https://github.com/alpinelinux/aports/commit/2026e1259422d4e0cf92391ca2d3844356c649d0 |
| 85 | + EXTRA_CFLAGS="-DTHREAD_STACK_SIZE=0x100000"; \ |
| 86 | + LDFLAGS="${LDFLAGS:--Wl},--strip-all"; \ |
| 87 | + make -j "$nproc" \ |
| 88 | + "EXTRA_CFLAGS=${EXTRA_CFLAGS:-}" \ |
| 89 | + "LDFLAGS=${LDFLAGS:-}" \ |
| 90 | + ; \ |
| 91 | +# https://github.com/docker-library/python/issues/784 |
| 92 | +# prevent accidental usage of a system installed libpython of the same version |
| 93 | + rm python; \ |
| 94 | + make -j "$nproc" \ |
| 95 | + "EXTRA_CFLAGS=${EXTRA_CFLAGS:-}" \ |
| 96 | + "LDFLAGS=${LDFLAGS:--Wl},-rpath='\$\$ORIGIN/../lib'" \ |
| 97 | + python \ |
| 98 | + ; \ |
| 99 | + make install; \ |
| 100 | + \ |
| 101 | + cd /; \ |
| 102 | + rm -rf /usr/src/python; \ |
| 103 | + \ |
| 104 | + find /usr/local -depth \ |
| 105 | + \( \ |
| 106 | + \( -type d -a \( -name test -o -name tests -o -name idle_test \) \) \ |
| 107 | + -o \( -type f -a \( -name '*.pyc' -o -name '*.pyo' -o -name 'libpython*.a' \) \) \ |
| 108 | + \) -exec rm -rf '{}' + \ |
| 109 | + ; \ |
| 110 | + \ |
| 111 | + find /usr/local -type f -executable -not \( -name '*tkinter*' \) -exec scanelf --needed --nobanner --format '%n#p' '{}' ';' \ |
| 112 | + | tr ',' '\n' \ |
| 113 | + | sort -u \ |
| 114 | + | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \ |
| 115 | + | xargs -rt apk add --no-network --virtual .python-rundeps \ |
| 116 | + ; \ |
| 117 | + apk del --no-network .build-deps; \ |
| 118 | + \ |
| 119 | + export PYTHONDONTWRITEBYTECODE=1; \ |
| 120 | + python3 --version; \ |
| 121 | + \ |
| 122 | + pip3 install \ |
| 123 | + --disable-pip-version-check \ |
| 124 | + --no-cache-dir \ |
| 125 | + --no-compile \ |
| 126 | + 'setuptools==65.5.1' \ |
| 127 | + # https://github.com/docker-library/python/issues/1023 |
| 128 | + 'wheel<0.46' \ |
| 129 | + ; \ |
| 130 | + pip3 --version |
| 131 | + |
| 132 | +# make some useful symlinks that are expected to exist ("/usr/local/bin/python" and friends) |
| 133 | +RUN set -eux; \ |
| 134 | + for src in idle3 pip3 pydoc3 python3 python3-config; do \ |
| 135 | + dst="$(echo "$src" | tr -d 3)"; \ |
| 136 | + [ -s "/usr/local/bin/$src" ]; \ |
| 137 | + [ ! -e "/usr/local/bin/$dst" ]; \ |
| 138 | + ln -svT "$src" "/usr/local/bin/$dst"; \ |
| 139 | + done |
| 140 | + |
| 141 | +CMD ["python3"] |
0 commit comments