ferrybig

Node & Python from source Dockerfile

Oct 24th, 2024
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. FROM debian:bookworm-slim
  2. # ensure local python is preferred over distribution python
  3. ENV PATH /usr/local/bin:$PATH
  4.  
  5. # cannot remove LANG even though https://bugs.python.org/issue19846 is fixed
  6. # last attempted removal of LANG broke many users:
  7. # https://github.com/docker-library/python/pull/570
  8. ENV LANG C.UTF-8
  9.  
  10. # runtime dependencies
  11. RUN set -eux; \
  12. apt-get update; \
  13. apt-get install -y --no-install-recommends \
  14. ca-certificates \
  15. netbase \
  16. tzdata \
  17. ; \
  18. rm -rf /var/lib/apt/lists/*
  19.  
  20. ENV GPG_KEY A035C8C19219BA821ECEA86B64E628F8D684696D
  21. ENV PYTHON_VERSION 3.11.10
  22. ENV PYTHON_SHA256 07a4356e912900e61a15cb0949a06c4a05012e213ecd6b4e84d0f67aabbee372
  23.  
  24. RUN set -eux; \
  25. \
  26. savedAptMark="$(apt-mark showmanual)"; \
  27. apt-get update; \
  28. apt-get install -y --no-install-recommends \
  29. dpkg-dev \
  30. gcc \
  31. gnupg \
  32. libbluetooth-dev \
  33. libbz2-dev \
  34. libc6-dev \
  35. libdb-dev \
  36. libffi-dev \
  37. libgdbm-dev \
  38. liblzma-dev \
  39. libncursesw5-dev \
  40. libreadline-dev \
  41. libsqlite3-dev \
  42. libssl-dev \
  43. make \
  44. tk-dev \
  45. uuid-dev \
  46. wget \
  47. xz-utils \
  48. zlib1g-dev \
  49. ; \
  50. \
  51. wget -O python.tar.xz "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz"; \
  52. echo "$PYTHON_SHA256 *python.tar.xz" | sha256sum -c -; \
  53. wget -O python.tar.xz.asc "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc"; \
  54. GNUPGHOME="$(mktemp -d)"; export GNUPGHOME; \
  55. gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys "$GPG_KEY"; \
  56. gpg --batch --verify python.tar.xz.asc python.tar.xz; \
  57. gpgconf --kill all; \
  58. rm -rf "$GNUPGHOME" python.tar.xz.asc; \
  59. mkdir -p /usr/src/python; \
  60. tar --extract --directory /usr/src/python --strip-components=1 --file python.tar.xz; \
  61. rm python.tar.xz; \
  62. \
  63. cd /usr/src/python; \
  64. gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
  65. ./configure \
  66. --build="$gnuArch" \
  67. --enable-loadable-sqlite-extensions \
  68. --enable-optimizations \
  69. --enable-option-checking=fatal \
  70. --enable-shared \
  71. --with-lto \
  72. --with-ensurepip \
  73. ; \
  74. nproc="$(nproc)"; \
  75. EXTRA_CFLAGS="$(dpkg-buildflags --get CFLAGS)"; \
  76. LDFLAGS="$(dpkg-buildflags --get LDFLAGS)"; \
  77. LDFLAGS="${LDFLAGS:--Wl},--strip-all"; \
  78. make -j "$nproc" \
  79. "EXTRA_CFLAGS=${EXTRA_CFLAGS:-}" \
  80. "LDFLAGS=${LDFLAGS:-}" \
  81. ; \
  82. # https://github.com/docker-library/python/issues/784
  83. # prevent accidental usage of a system installed libpython of the same version
  84. rm python; \
  85. make -j "$nproc" \
  86. "EXTRA_CFLAGS=${EXTRA_CFLAGS:-}" \
  87. "LDFLAGS=${LDFLAGS:--Wl},-rpath='\$\$ORIGIN/../lib'" \
  88. python \
  89. ; \
  90. make install; \
  91. \
  92. cd /; \
  93. rm -rf /usr/src/python; \
  94. \
  95. find /usr/local -depth \
  96. \( \
  97. \( -type d -a \( -name test -o -name tests -o -name idle_test \) \) \
  98. -o \( -type f -a \( -name '*.pyc' -o -name '*.pyo' -o -name 'libpython*.a' \) \) \
  99. \) -exec rm -rf '{}' + \
  100. ; \
  101. \
  102. ldconfig; \
  103. \
  104. apt-mark auto '.*' > /dev/null; \
  105. apt-mark manual $savedAptMark; \
  106. find /usr/local -type f -executable -not \( -name '*tkinter*' \) -exec ldd '{}' ';' \
  107. | awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); printf "*%s\n", so }' \
  108. | sort -u \
  109. | xargs -r dpkg-query --search \
  110. | cut -d: -f1 \
  111. | sort -u \
  112. | xargs -r apt-mark manual \
  113. ; \
  114. apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
  115. rm -rf /var/lib/apt/lists/*; \
  116. \
  117. export PYTHONDONTWRITEBYTECODE=1; \
  118. python3 --version; \
  119. \
  120. pip3 install \
  121. --disable-pip-version-check \
  122. --no-cache-dir \
  123. --no-compile \
  124. 'setuptools==65.5.1' \
  125. wheel \
  126. ; \
  127. pip3 --version
  128.  
  129. # make some useful symlinks that are expected to exist ("/usr/local/bin/python" and friends)
  130. RUN set -eux; \
  131. for src in idle3 pip3 pydoc3 python3 python3-config; do \
  132. dst="$(echo "$src" | tr -d 3)"; \
  133. [ -s "/usr/local/bin/$src" ]; \
  134. [ ! -e "/usr/local/bin/$dst" ]; \
  135. ln -svT "$src" "/usr/local/bin/$dst"; \
  136. done
  137. RUN groupadd --gid 1000 node \
  138. && useradd --uid 1000 --gid node --shell /bin/bash --create-home node
  139.  
  140. ENV NODE_VERSION 20.18.0
  141.  
  142. RUN ARCH= OPENSSL_ARCH= && dpkgArch="$(dpkg --print-architecture)" \
  143. && case "${dpkgArch##*-}" in \
  144. amd64) ARCH='x64' OPENSSL_ARCH='linux-x86_64';; \
  145. ppc64el) ARCH='ppc64le' OPENSSL_ARCH='linux-ppc64le';; \
  146. s390x) ARCH='s390x' OPENSSL_ARCH='linux*-s390x';; \
  147. arm64) ARCH='arm64' OPENSSL_ARCH='linux-aarch64';; \
  148. armhf) ARCH='armv7l' OPENSSL_ARCH='linux-armv4';; \
  149. i386) ARCH='x86' OPENSSL_ARCH='linux-elf';; \
  150. *) echo "unsupported architecture"; exit 1 ;; \
  151. esac \
  152. && set -ex \
  153. # libatomic1 for arm
  154. && apt-get update && apt-get install -y ca-certificates curl wget gnupg dirmngr xz-utils libatomic1 --no-install-recommends \
  155. && rm -rf /var/lib/apt/lists/* \
  156. # use pre-existing gpg directory, see https://github.com/nodejs/docker-node/pull/1895#issuecomment-1550389150
  157. && export GNUPGHOME="$(mktemp -d)" \
  158. # gpg keys listed at https://github.com/nodejs/node#release-keys
  159. && for key in \
  160. 4ED778F539E3634C779C87C6D7062848A1AB005C \
  161. 141F07595B7B3FFE74309A937405533BE57C7D57 \
  162. 74F12602B6F1C4E913FAA37AD3A89613643B6201 \
  163. DD792F5973C6DE52C432CBDAC77ABFA00DDBF2B7 \
  164. CC68F5A3106FF448322E48ED27F5E38D5B0A215F \
  165. 8FCCA13FEF1D0C2E91008E09770F7A9A5AE15600 \
  166. 890C08DB8579162FEE0DF9DB8BEAB4DFCF555EF4 \
  167. C82FA3AE1CBEDC6BE46B9360C43CEC45C17AB93C \
  168. 108F52B48DB57BB0CC439B2997B01419BD92F80A \
  169. A363A499291CBBC940DD62E41F10027AF002F8B0 \
  170. ; do \
  171. gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys "$key" || \
  172. gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$key" ; \
  173. done \
  174. && curl -fsSLO --compressed "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-$ARCH.tar.xz" \
  175. && curl -fsSLO --compressed "https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt.asc" \
  176. && gpg --batch --decrypt --output SHASUMS256.txt SHASUMS256.txt.asc \
  177. && gpgconf --kill all \
  178. && rm -rf "$GNUPGHOME" \
  179. && grep " node-v$NODE_VERSION-linux-$ARCH.tar.xz\$" SHASUMS256.txt | sha256sum -c - \
  180. && tar -xJf "node-v$NODE_VERSION-linux-$ARCH.tar.xz" -C /usr/local --strip-components=1 --no-same-owner \
  181. && rm "node-v$NODE_VERSION-linux-$ARCH.tar.xz" SHASUMS256.txt.asc SHASUMS256.txt \
  182. # Remove unused OpenSSL headers to save ~34MB. See this NodeJS issue: https://github.com/nodejs/node/issues/46451
  183. && find /usr/local/include/node/openssl/archs -mindepth 1 -maxdepth 1 ! -name "$OPENSSL_ARCH" -exec rm -rf {} \; \
  184. && apt-mark auto '.*' > /dev/null \
  185. && find /usr/local -type f -executable -exec ldd '{}' ';' \
  186. | awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); print so }' \
  187. | sort -u \
  188. | xargs -r dpkg-query --search \
  189. | cut -d: -f1 \
  190. | sort -u \
  191. | xargs -r apt-mark manual \
  192. && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
  193. && ln -s /usr/local/bin/node /usr/local/bin/nodejs \
  194. # smoke tests
  195. && node --version \
  196. && npm --version
  197.  
  198. ENV YARN_VERSION 1.22.22
  199.  
  200. RUN set -ex \
  201. && savedAptMark="$(apt-mark showmanual)" \
  202. && apt-get update && apt-get install -y ca-certificates curl wget gnupg dirmngr --no-install-recommends \
  203. && rm -rf /var/lib/apt/lists/* \
  204. # use pre-existing gpg directory, see https://github.com/nodejs/docker-node/pull/1895#issuecomment-1550389150
  205. && export GNUPGHOME="$(mktemp -d)" \
  206. && for key in \
  207. 6A010C5166006599AA17F08146C2130DFD2497F5 \
  208. ; do \
  209. gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys "$key" || \
  210. gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$key" ; \
  211. done \
  212. && curl -fsSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz" \
  213. && curl -fsSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz.asc" \
  214. && gpg --batch --verify yarn-v$YARN_VERSION.tar.gz.asc yarn-v$YARN_VERSION.tar.gz \
  215. && gpgconf --kill all \
  216. && rm -rf "$GNUPGHOME" \
  217. && mkdir -p /opt \
  218. && tar -xzf yarn-v$YARN_VERSION.tar.gz -C /opt/ \
  219. && ln -s /opt/yarn-v$YARN_VERSION/bin/yarn /usr/local/bin/yarn \
  220. && ln -s /opt/yarn-v$YARN_VERSION/bin/yarnpkg /usr/local/bin/yarnpkg \
  221. && rm yarn-v$YARN_VERSION.tar.gz.asc yarn-v$YARN_VERSION.tar.gz \
  222. && apt-mark auto '.*' > /dev/null \
  223. && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; } \
  224. && find /usr/local -type f -executable -exec ldd '{}' ';' \
  225. | awk '/=>/ { so = $(NF-1); if (index(so, "/usr/local/") == 1) { next }; gsub("^/(usr/)?", "", so); print so }' \
  226. | sort -u \
  227. | xargs -r dpkg-query --search \
  228. | cut -d: -f1 \
  229. | sort -u \
  230. | xargs -r apt-mark manual \
  231. && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
  232. # smoke test
  233. && yarn --version \
  234. && rm -rf /tmp/*
Advertisement
Add Comment
Please, Sign In to add comment