Skip to content

Commit bf99659

Browse files
authored
Merge pull request #22 from CubeCoders/python
feat(python): switch to upstream only
2 parents 4aefe82 + 5071c1c commit bf99659

File tree

3 files changed

+137
-19
lines changed

3 files changed

+137
-19
lines changed

python/3.11/Dockerfile

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
1-
# Python image for AMP containers using Python 3.11
1+
# Python image for AMP containers using Python 3.11 from upstream
22
# ghcr.io/cubecoders/amp:python-3.11
33

4+
FROM python:3.11-slim-bookworm AS py311
5+
6+
ENV DEBIAN_FRONTEND="noninteractive"
7+
8+
ARG PYTHON_VERSION="3.11"
9+
10+
COPY scripts/python/find-deps.sh /usr/local/bin/find-deps.sh
11+
RUN chmod +x /usr/local/bin/find-deps.sh
12+
13+
RUN set -eux; \
14+
/usr/local/bin/find-deps.sh ${PYTHON_VERSION} >/tmp/${PYTHON_VERSION}-run-deps.txt
15+
416
FROM ghcr.io/cubecoders/amp:debian
517
LABEL org.opencontainers.image.licenses=MIT
618

719
ENV PIP_DISABLE_PIP_VERSION_CHECK="1"
820
ENV DEBIAN_FRONTEND="noninteractive"
921

10-
# System Python (3.11) from Debian
22+
ARG PYTHON_VERSION="3.11"
23+
24+
# Drop in upstream Python 3.11
25+
COPY --from=py311 /usr/local/ /usr/local/
26+
27+
COPY --from=py311 /tmp/${PYTHON_VERSION}-run-deps.txt /tmp/${PYTHON_VERSION}-run-deps.txt
28+
1129
RUN set -eux; \
1230
apt-get update; \
13-
apt-get install -o APT::Keep-Downloaded-Packages="false" -y --no-install-recommends \
14-
python3 python3-venv python3-pip; \
31+
# Purge gdb from base image to remove conflicting Python libs
32+
apt-get purge gdb -y --autoremove; \
33+
\
34+
sort -u /tmp/${PYTHON_VERSION}-run-deps.txt \
35+
| awk 'NF && $1 !~ /^#/' \
36+
| xargs -r apt-get install -o APT::Keep-Downloaded-Packages="false" -y --no-install-recommends; \
1537
apt-get clean; \
16-
rm -rf /var/lib/apt/lists/*
38+
rm -rf /var/lib/apt/lists/* /tmp/${PYTHON_VERSION}-run-deps.txt; \
39+
\
40+
python${PYTHON_VERSION} -m pip install --no-cache-dir --upgrade pip setuptools wheel; \
41+
for shim in python python3 python${PYTHON_VERSION} pip pip3 pip${PYTHON_VERSION}; do \
42+
ln -sf "/usr/local/bin/${shim}" "/usr/bin/${shim}"; \
43+
done

python/3/Dockerfile

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,84 @@
1-
# Python image for AMP containers including Python 3.10, 3.11 (default) and 3.12
1+
# Python image for AMP containers including Python 3.10, 3.11 (default) and 3.12 from upstream
22
# ghcr.io/cubecoders/amp:python-3
33

44
FROM python:3.10-slim-bookworm AS py310
5+
6+
ENV DEBIAN_FRONTEND="noninteractive"
7+
8+
ARG PYTHON_VERSION="3.10"
9+
10+
COPY scripts/python/find-deps.sh /usr/local/bin/find-deps.sh
11+
RUN chmod +x /usr/local/bin/find-deps.sh
12+
13+
RUN set -eux; \
14+
/usr/local/bin/find-deps.sh ${PYTHON_VERSION} >/tmp/${PYTHON_VERSION}-run-deps.txt
15+
16+
FROM python:3.11-slim-bookworm AS py311
17+
18+
ENV DEBIAN_FRONTEND="noninteractive"
19+
20+
ARG PYTHON_VERSION="3.11"
21+
22+
COPY scripts/python/find-deps.sh /usr/local/bin/find-deps.sh
23+
RUN chmod +x /usr/local/bin/find-deps.sh
24+
25+
RUN set -eux; \
26+
/usr/local/bin/find-deps.sh ${PYTHON_VERSION} >/tmp/${PYTHON_VERSION}-run-deps.txt
27+
528
FROM python:3.12-slim-bookworm AS py312
629

7-
FROM ghcr.io/cubecoders/amp:python-3.11
30+
ENV DEBIAN_FRONTEND="noninteractive"
31+
32+
ARG PYTHON_VERSION="3.12"
33+
34+
COPY scripts/python/find-deps.sh /usr/local/bin/find-deps.sh
35+
RUN chmod +x /usr/local/bin/find-deps.sh
36+
37+
RUN set -eux; \
38+
/usr/local/bin/find-deps.sh ${PYTHON_VERSION} >/tmp/${PYTHON_VERSION}-run-deps.txt
39+
40+
FROM ghcr.io/cubecoders/amp:debian
841
LABEL org.opencontainers.image.licenses=MIT
942

43+
ENV PIP_DISABLE_PIP_VERSION_CHECK="1"
1044
ENV DEBIAN_FRONTEND="noninteractive"
1145

46+
ARG PYTHON_DEFAULT="3.11"
47+
ARG PYTHON_NON_DEFAULT="3.10 3.12"
48+
1249
# Drop in upstream Python 3.10
13-
COPY --from=py310 /usr/local/bin/python3.10 /usr/local/bin/
14-
COPY --from=py310 /usr/local/bin/pip3.10 /usr/local/bin/
15-
COPY --from=py310 /usr/local/lib/python3.10/ /usr/local/lib/python3.10/
16-
COPY --from=py310 /usr/local/lib/libpython3.10* /usr/local/lib/
50+
COPY --from=py310 /usr/local/ /usr/local/
51+
52+
COPY --from=py310 /tmp/3.10-run-deps.txt /tmp/3.10-run-deps.txt
1753

1854
# Drop in upstream Python 3.12
19-
COPY --from=py312 /usr/local/bin/python3.12 /usr/local/bin/
20-
COPY --from=py312 /usr/local/bin/pip3.12 /usr/local/bin/
21-
COPY --from=py312 /usr/local/lib/python3.12/ /usr/local/lib/python3.12/
22-
COPY --from=py312 /usr/local/lib/libpython3.12* /usr/local/lib/
55+
COPY --from=py312 /usr/local/ /usr/local/
56+
57+
COPY --from=py312 /tmp/3.12-run-deps.txt /tmp/3.12-run-deps.txt
58+
59+
# Drop in upstream Python 3.11 (default last)
60+
COPY --from=py311 /usr/local/ /usr/local/
61+
62+
COPY --from=py311 /tmp/3.11-run-deps.txt /tmp/3.11-run-deps.txt
2363

24-
# Clean up
2564
RUN set -eux; \
26-
python3.10 -m pip --no-cache-dir install --upgrade pip setuptools wheel; \
27-
python3.12 -m pip --no-cache-dir install --upgrade pip setuptools wheel; \
28-
for shim in python3.10 python3.12 pip3.10 pip3.12; do \
65+
apt-get update; \
66+
# Purge gdb from base image to remove conflicting Python libs
67+
apt-get purge gdb -y --autoremove; \
68+
\
69+
sort -u /tmp/*-run-deps.txt \
70+
| awk 'NF && $1 !~ /^#/' \
71+
| xargs -r apt-get install -o APT::Keep-Downloaded-Packages="false" -y --no-install-recommends; \
72+
apt-get clean; \
73+
rm -rf /var/lib/apt/lists/* /tmp/*-run-deps.txt; \
74+
\
75+
for v in ${PYTHON_NON_DEFAULT}; do \
76+
python${v} -m pip install --no-cache-dir --upgrade pip setuptools wheel; \
77+
for shim in python${v} pip${v}; do \
78+
[ -x "/usr/local/bin/${shim}" ] && ln -sf "/usr/local/bin/${shim}" "/usr/bin/${shim}"; \
79+
done; \
80+
done; \
81+
python${PYTHON_DEFAULT} -m pip install --no-cache-dir --upgrade pip setuptools wheel; \
82+
for shim in python python3 python${PYTHON_DEFAULT} pip pip3 pip${PYTHON_DEFAULT}; do \
2983
ln -sf "/usr/local/bin/${shim}" "/usr/bin/${shim}"; \
3084
done

scripts/python/find-deps.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
VER="$1"
6+
7+
export DEBIAN_FRONTEND=noninteractive
8+
apt-get update 1>&2
9+
apt-get install -o APT::Keep-Downloaded-Packages="false" -y --no-install-recommends binutils dpkg findutils libc-bin 1>&2
10+
11+
# build scan list only from existing files
12+
scan_paths=""
13+
for p in /usr/local/bin/python3 \
14+
/usr/local/lib/libpython*.so* \
15+
/usr/local/lib/python*/lib-dynload/*.so; do
16+
[ -e "$p" ] && scan_paths="$scan_paths $p"
17+
done
18+
19+
# DT_NEEDED sonames (no execution)
20+
neededs=$(
21+
objdump -p $scan_paths 2>/dev/null | awk '/^ NEEDED /{print $2}' | sort -u
22+
)
23+
24+
# Resolve SONAMEs to real files by scanning common lib dirs
25+
so_files=$(
26+
for so in $neededs; do
27+
for d in \
28+
/lib/*-linux-gnu /usr/lib/*-linux-gnu \
29+
/lib /usr/lib; do
30+
f="$d/$so"
31+
[ -e "$f" ] && printf '%s\n' "$f"
32+
done
33+
done | sort -u
34+
)
35+
36+
# Map files -> packages
37+
printf '%s\n' $so_files | xargs -r dpkg -S | cut -d: -f1 | sort -u

0 commit comments

Comments
 (0)