1- #syntax=docker/dockerfile:1
1+ # syntax=docker/dockerfile:1.20-labs
2+
23
34ARG ALPINE_VERSION=3.20
45ARG PYTHON_VERSION=3.12
@@ -19,7 +20,7 @@ ENV BUILD_ROOT=$BUILD_ROOT \
1920 _apk_add="/usr/bin/env apk add --root $BUILD_ROOT --no-cache" \
2021 _apk_del="/usr/bin/env apk del --root $BUILD_ROOT --purge" \
2122 _sh="chroot $BUILD_ROOT sh" \
22- _ln="chroot $BUILD_ROOT ln" \
23+ _ln="chroot $BUILD_ROOT /bin/ ln" \
2324 _chroot="chroot $BUILD_ROOT"
2425
2526ADD --chmod=0755 chroot-apk.sh /usr/local/bin/chroot-apk
@@ -28,10 +29,7 @@ ADD --chmod=0755 chroot-ln.sh /usr/local/bin/chroot-ln
2829ADD --chmod=0755 remove-py-if-pyc-exists.sh /usr/local/bin/remove-py-if-pyc-exists
2930ADD --chmod=0755 chroot-exec.sh /usr/local/bin/chroot-exec
3031RUN \
31- --mount=type=cache,id=pip-cache-${TARGETARCH}${TARGETVARIANT},sharing=shared,target=/root/.cache/pip \
3232 set -eu ; \
33- python -m pip install -U pip setuptools ; \
34- # Add to buildroot:
3533 $_sys_apk_add \
3634 dash \
3735 # TLS certs
@@ -41,19 +39,16 @@ RUN \
4139 # be imported from. This makes the stdlib immutable.
4240 zip \
4341 ; \
44- # remove all ``__pycache__`` directories
45- find /usr/local/lib/python$PYTHON_VERSION -type d -name '__pycache__' -print0 | xargs -0 rm -rf ; \
46- # compile all py to an adjacent pyc and remove the original, leaving only the bytecode
47- python -m compileall -q -b /usr/local/lib/python$PYTHON_VERSION ; \
48- find -type f -name '*.py' -exec sh -c "remove-py-if-pyc-exists -q {}" \; ;\
4942 # make the new root:
5043 mkdir -p \
5144 $CACHE_ROOT/ \
5245 $BUILD_ROOT/etc \
5346 $BUILD_ROOT/bin \
5447 $BUILD_ROOT/usr/local/lib/python$PYTHON_VERSION/site-packages \
5548 $BUILD_ROOT/usr/local/bin \
49+ $BUILD_ROOT/proc \
5650 ; \
51+ cp -R /dev $BUILD_ROOT/dev ; \
5752 # copy the apk related confs
5853 cp -R /etc/apk $BUILD_ROOT/etc/apk ; \
5954 $_apk_add --initdb ; \
@@ -62,22 +57,46 @@ RUN \
6257 alpine-release \
6358 musl \
6459 libffi \
65- coreutils-env \
6660 ; \
67- $_apk_add --no-scripts \
61+ cp -p /bin/busybox $BUILD_ROOT/bin/busybox ; \
62+ chroot $BUILD_ROOT /bin/busybox busybox ln -sf /bin/busybox /bin/ln
63+
64+ RUN --security=insecure \
65+ set -eu ; \
66+ mount --bind /proc /$BUILD_ROOT/proc ; \
67+ $_apk_add \
68+ busybox \
6869 dash \
6970 dash-binsh \
70- ; \
71- $_apk_add \
71+ ; \
72+ T=$(mktemp -d) ; \
73+ if [ -f $BUILD_ROOT/lib/apk/db/scripts.tar.gz ]; then \
74+ tar -C "$T" -xzpf $BUILD_ROOT/lib/apk/db/scripts.tar.gz ; \
75+ rm -f $BUILD_ROOT/lib/apk/db/scripts.tar.gz ; \
76+ find "$T" -name 'busybox-*' -delete ; \
77+ tar -C "$T" -cpvzf $BUILD_ROOT/lib/apk/db/scripts.tar.gz . ; \
78+ rm -rf "$T" ; \
79+ fi ; \
80+ tar -C "$BUILD_ROOT" -cpf - etc/apk bin/ln bin/busybox var/cache/apk usr/share/apk | tar -C "$CACHE_ROOT" -xpf - ; \
81+ rm -rf $BUILD_ROOT/bin/ln $BUILD_ROOT/bin/busybox $BUILD_ROOT/etc/apk $BUILD_ROOT/var/cache/apk $BUILD_ROOT/usr/share/apk && \
82+ chroot-apk add \
7283 ca-certificates \
7384 # needed for update-ca-certificates to work:
74- run-parts \
75- # install the runtime dependencies for python
85+ run-parts
86+
87+ RUN \
88+ --mount=type=cache,id=pip-cache-${TARGETARCH}${TARGETVARIANT},sharing=shared,target=/root/.cache/pip \
89+ set -eu ; \
90+ chroot-apk add \
91+ coreutils-env \
7692 $(apk info -R .python-rundeps | grep -vE ':$') \
7793 ; \
78- cp -p /bin/busybox $BUILD_ROOT/bin/busybox ; \
79- ls -lt $BUILD_ROOT/bin/busybox ; \
80- chroot $BUILD_ROOT /bin/busybox ln -sf /bin/busybox /bin/ln ; \
94+ python -m pip install -U pip setuptools ; \
95+ # remove all ``__pycache__`` directories
96+ find /usr/local/lib/python$PYTHON_VERSION -type d -name '__pycache__' -print0 | xargs -0 rm -rf ; \
97+ # compile all py to an adjacent pyc and remove the original, leaving only the bytecode
98+ python -m compileall -q -b /usr/local/lib/python$PYTHON_VERSION ; \
99+ find -type f -name '*.py' -exec sh -c "remove-py-if-pyc-exists -q {}" \; ;\
81100 # copy dash into the container so we can use it as the default bin/sh
82101 # tar -C / -cpf - $(\
83102 # apk info -L \
@@ -98,10 +117,8 @@ RUN \
98117 touch $BUILD_ROOT/usr/local/lib/python$PYTHON_VERSION/ensurepip.py ; \
99118 rm $BUILD_ROOT/usr/local/lib/python$PYTHON_VERSION/lib-dynload/_tkinter* ; \
100119 ) && \
101- $_ln -sf /usr/local/bin/python$PYTHON_VERSION /usr/local/bin/python3 && \
102- $_ln -sf /usr/local/bin/python$PYTHON_VERSION /usr/local/bin/python && \
103- tar -C "$BUILD_ROOT" -cpf - etc/apk bin/ln bin/busybox var/cache/apk usr/share/apk | tar -C "$CACHE_ROOT" -xpf - ; \
104- rm -rf $BUILD_ROOT/bin/ln $BUILD_ROOT/bin/busybox $BUILD_ROOT/etc/apk $BUILD_ROOT/var/cache/apk $BUILD_ROOT/usr/share/apk && \
120+ chroot-ln -sf /usr/local/bin/python$PYTHON_VERSION /usr/local/bin/python3 && \
121+ chroot-ln -sf /usr/local/bin/python$PYTHON_VERSION /usr/local/bin/python && \
105122 # regenerate the ca-certs!
106123 chroot-exec update-ca-certificates && \
107124 chroot-pip --optimize install --force-reinstall setuptools
@@ -115,6 +132,7 @@ ARG BUILD_ROOT='/dest'
115132ENV BUILD_ROOT=$BUILD_ROOT \
116133 PYTHON_VERSION=$PYTHON_VERSION \
117134 ALPINE_VERSION=$ALPINE_VERSION
135+ COPY --from=buildroot /dev /dev
118136COPY --from=buildroot $BUILD_ROOT /
119137LABEL \
120138 org.opencontainers.image.authors="distroless-python image developers <
[email protected] >" \
0 commit comments