Skip to content

Commit 99ace78

Browse files
committed
build: make /dev/fd in chroot
1 parent f6c3a13 commit 99ace78

File tree

6 files changed

+79
-23
lines changed

6 files changed

+79
-23
lines changed

.github/workflows/development.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ jobs:
104104
with:
105105
platforms: ${{ env.TARGET_PLATFORMS }}
106106
driver-opts: network=host
107+
buildkitd-flags: '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host'
108+
107109
-
108110
id: image_env
109111
run: |
@@ -137,6 +139,7 @@ jobs:
137139
DOCKER_BUILD_SUMMARY: false
138140
DOCKER_BUILD_RECORD_UPLOAD: false
139141
with:
142+
allow: network.host,security.insecure # this is for the bind mount
140143
push: true
141144
platforms: ${{ env.TARGET_PLATFORMS }}
142145
context: "."
@@ -168,6 +171,7 @@ jobs:
168171
DOCKER_BUILD_SUMMARY: false
169172
DOCKER_BUILD_RECORD_UPLOAD: false
170173
with:
174+
allow: network.host,security.insecure # due to using a file that has a sec contxt
171175
push: true
172176
context: "."
173177
platforms: ${{ env.TARGET_PLATFORMS }}

.github/workflows/main.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
- '3.20'
3232
- '3.21'
3333
- '3.22'
34+
- '3.23'
3435
os:
3536
- 'ubuntu-latest'
3637
exclude:
@@ -39,8 +40,16 @@ jobs:
3940
alpine: '3.21'
4041
- python: '3.8'
4142
alpine: '3.22'
43+
- python: '3.8'
44+
alpine: '3.23'
45+
46+
- python: '3.9'
47+
alpine: '3.23'
48+
4249
- python: '3.14'
4350
alpine: '3.20'
51+
- python: '3.15'
52+
alpine: '3.20'
4453

4554
runs-on: ${{ matrix.os }}
4655
steps:
@@ -341,6 +350,7 @@ jobs:
341350
- '3.20'
342351
- '3.21'
343352
- '3.22'
353+
- '3.23'
344354
os:
345355
- 'ubuntu-latest'
346356
exclude:
@@ -349,8 +359,17 @@ jobs:
349359
alpine: '3.21'
350360
- python: '3.8'
351361
alpine: '3.22'
362+
- python: '3.8'
363+
alpine: '3.23'
364+
365+
- python: '3.9'
366+
alpine: '3.23'
367+
352368
- python: '3.14'
353369
alpine: '3.20'
370+
- python: '3.15'
371+
alpine: '3.20'
372+
354373

355374
permissions:
356375
packages: write

Dockerfile.alpine

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#syntax=docker/dockerfile:1
1+
# syntax=docker/dockerfile:1.20-labs
2+
23

34
ARG ALPINE_VERSION=3.20
45
ARG 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

2526
ADD --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
2829
ADD --chmod=0755 remove-py-if-pyc-exists.sh /usr/local/bin/remove-py-if-pyc-exists
2930
ADD --chmod=0755 chroot-exec.sh /usr/local/bin/chroot-exec
3031
RUN \
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'
115132
ENV BUILD_ROOT=$BUILD_ROOT \
116133
PYTHON_VERSION=$PYTHON_VERSION \
117134
ALPINE_VERSION=$ALPINE_VERSION
135+
COPY --from=buildroot /dev /dev
118136
COPY --from=buildroot $BUILD_ROOT /
119137
LABEL \
120138
org.opencontainers.image.authors="distroless-python image developers <[email protected]>" \

chroot-apk.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ fini () {
2727
>&2 echo "Removing APK data from $BUILD_ROOT, storing in $CACHE_ROOT"
2828
extra='-v'
2929
fi
30+
local T="$(mktemp -d)"
31+
if [ -f $BUILD_ROOT/lib/apk/db/scripts.tar.gz ]; then
32+
tar -C "$T" -xzpf $BUILD_ROOT/lib/apk/db/scripts.tar.gz
33+
rm -f $BUILD_ROOT/lib/apk/db/scripts.tar.gz
34+
sed -i'' 's|^#!busybox sh|#!/usr/bin/dash|g' $(find "$T" -type f -print)
35+
sed -i'' 's|^#!/bin/sh|#!/usr/bin/dash|g' $(find "$T" -type f -print)
36+
sed -i'' 's|^#!/bin/busybox sh|#!/usr/bin/dash|g' $(find "$T" -type f -print)
37+
cat $(find "$T" -type f -print)
38+
tar -C "$T" -cpvzf $BUILD_ROOT/lib/apk/db/scripts.tar.gz .
39+
rm -rf "$T"
40+
fi
41+
42+
mkdir -p $BUILD_ROOT/var/cache/apk
3043
tar -C "$BUILD_ROOT" -cpf - etc/apk bin/ln bin/busybox var/cache/apk usr/share/apk | eval tar -C "$CACHE_ROOT" -xpf $extra -
3144
$_chroot /bin/ln -sf /usr/bin/dash /bin/sh.bak
3245
rm -rf $BUILD_ROOT/bin/ln $BUILD_ROOT/bin/busybox $BUILD_ROOT/etc/apk $BUILD_ROOT/var/cache/apk $BUILD_ROOT/usr/share/apk

chroot-exec.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ fini () {
2727
>&2 echo "Removing APK data from $BUILD_ROOT, storing in $CACHE_ROOT"
2828
extra=-v
2929
fi
30+
mkdir -p $BUILD_ROOT/var/cache/apk
3031
tar -C "$BUILD_ROOT" -cpf - etc/apk bin/ln bin/busybox var/cache/apk usr/share/apk | eval tar -C "$CACHE_ROOT" $extra -xpf -
3132
$_chroot /bin/ln -sf /usr/bin/dash /bin/sh.bak
3233
rm -rf $BUILD_ROOT/bin/ln $BUILD_ROOT/bin/busybox $BUILD_ROOT/etc/apk $BUILD_ROOT/var/cache/apk $BUILD_ROOT/usr/share/apk

chroot-ln.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ fini () {
2727
>&2 echo "Removing APK data from $BUILD_ROOT, storing in $CACHE_ROOT"
2828
extra=-v
2929
fi
30+
mkdir -p $BUILD_ROOT/var/cache/apk
3031
tar -C "$BUILD_ROOT" -cpf - etc/apk bin/ln bin/busybox var/cache/apk usr/share/apk | eval tar -C "$CACHE_ROOT" $extra -xpf -
3132
$_chroot /bin/ln -sf /usr/bin/dash /bin/sh.bak
3233
rm -rf $BUILD_ROOT/bin/ln $BUILD_ROOT/bin/busybox $BUILD_ROOT/etc/apk $BUILD_ROOT/var/cache/apk $BUILD_ROOT/usr/share/apk

0 commit comments

Comments
 (0)