Skip to content

Commit baa89d6

Browse files
authored
Reduce number of layers and optimize build of the base-python images (#321)
* Bind-mount Python patches instead of copying them to the image We don't need the patches in runtime but because they're copied, they are still persisted in a layer of the image. Use bind-mount to have them available only for the build. * Create symlinks in the Python build step There is no major benefit in splitting these two actions to two steps. * Define PIP_VERSION arg once it's needed By defining it early we're busting the cache on pip version upgrade, effectively making the split into two layers pointless. * Simplify pip install step using ensurepip Use ensurepip instead of fetching get-pip to install pip, to make the build step simpler. Note that --without-ensurepip in the Python configure args is still desired and correct, as it only prevents from running ensurepip after the build. The ensurepip and upgrade to the pinned version should be run in the same step to prevent layer bloat by modified files. * Use --default-pip instead of creating a symlink
1 parent 8017f91 commit baa89d6

19 files changed

+30
-78
lines changed

python/3.12/Dockerfile

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ FROM $BUILD_FROM
33

44
ARG \
55
PYTHON_VERSION \
6-
PIP_VERSION \
76
CERT_IDENTITY \
87
CERT_OIDC_ISSUER \
98
QEMU_CPU
@@ -14,8 +13,9 @@ ENV PATH=/usr/local/bin:$PATH
1413
# Set shell
1514
SHELL ["/bin/ash", "-o", "pipefail", "-c"]
1615

17-
COPY *.patch /usr/src/
18-
RUN set -ex \
16+
RUN \
17+
--mount=type=bind,source=./patches,target=/usr/src/patches \
18+
set -ex \
1919
&& export PYTHON_VERSION=${PYTHON_VERSION} \
2020
&& apk add --no-cache --virtual .fetch-deps \
2121
openssl \
@@ -67,7 +67,7 @@ RUN set -ex \
6767
# add build deps before removing fetch deps in case there's overlap
6868
&& apk del .fetch-deps .cosign \
6969
\
70-
&& for i in /usr/src/*.patch; do \
70+
&& for i in /usr/src/patches/*.patch; do \
7171
patch -d /usr/src/python -p 1 < "${i}"; done \
7272
&& cd /usr/src/python \
7373
&& gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \
@@ -102,32 +102,16 @@ RUN set -ex \
102102
-type d -a \( -name test -o -name tests \) \
103103
\) -exec rm -rf '{}' + \
104104
&& rm -rf /usr/src/python \
105-
&& rm -f /usr/src/*.patch
106-
107105
# make some useful symlinks that are expected to exist
108-
RUN cd /usr/local/bin \
106+
&& cd /usr/local/bin \
109107
&& ln -s idle3 idle \
110108
&& ln -s pydoc3 pydoc \
111109
&& ln -s python3 python \
112110
&& ln -s python3-config python-config
113111

112+
ARG PIP_VERSION
113+
114114
RUN set -ex; \
115-
\
116-
apk add --no-cache --virtual .fetch-deps openssl; \
117-
\
118-
curl -L -o get-pip.py 'https://bootstrap.pypa.io/get-pip.py'; \
119-
\
120-
apk del .fetch-deps; \
121-
\
122-
python get-pip.py \
123-
--disable-pip-version-check \
124-
--no-cache-dir \
125-
pip==${PIP_VERSION} \
126-
; \
127-
pip --version; \
128-
\
129-
find /usr/local -depth \
130-
\( \
131-
-type d -a \( -name test -o -name tests \) \
132-
\) -exec rm -rf '{}' +; \
133-
rm -f get-pip.py
115+
python -m ensurepip --upgrade --default-pip; \
116+
pip3 install --no-cache-dir --upgrade pip=="${PIP_VERSION}"; \
117+
pip --version
File renamed without changes.

0 commit comments

Comments
 (0)