Skip to content

Instantly share code, notes, and snippets.

@darvid
Last active September 4, 2024 19:31
Show Gist options
  • Save darvid/6b32ce975d099382a6d323257f525b73 to your computer and use it in GitHub Desktop.
Save darvid/6b32ce975d099382a6d323257f525b73 to your computer and use it in GitHub Desktop.
โ˜๏ธ ๐Ÿ Amazon Linux 2023 + Python 3.12 (containers)

Problem

  • amazon/aws-lambda-python supports Python 3.12 (released Oct 2023), amazonlinux does not ๐Ÿ’ข
  • (currently) no way to install python3.12 in a standard way (i.e. via dnf and the official package repos, AFAICT)
  • solutions involving pyenv or manually building Python... ๐Ÿคฎ
  • multi-stage build copying the 3.12 (and potentially future versions) distribution from aws-lambda-python โžก๏ธ amazonlinux:2023? slightly less ๐Ÿคฎ
# syntax=docker/dockerfile:1.9.0-labs
ARG PYTHON_VERSION=3.12
FROM public.ecr.aws/lambda/python:${PYTHON_VERSION} AS lambda-python
ARG PYTHON_VERSION
ENV PYTHON_VERSION=${PYTHON_VERSION}
ARG PYTHON_VERSION
FROM public.ecr.aws/amazonlinux/amazonlinux:2023 AS al2023-python
ARG PYTHON_VERSION
ENV PYTHON_VERSION=${PYTHON_VERSION}
COPY --from=lambda-python \
/var/lang/bin/python${PYTHON_VERSION} \
/usr/bin/
COPY --from=lambda-python \
/var/lang/include/python${PYTHON_VERSION} \
/usr/include/python${PYTHON_VERSION}
COPY --from=lambda-python \
/var/lang/lib/python${PYTHON_VERSION}/lib-dynload \
/usr/lib64/python${PYTHON_VERSION}/lib-dynload
COPY --from=lambda-python \
/var/lang/lib/python${PYTHON_VERSION} \
/usr/lib64/python${PYTHON_VERSION}
COPY --from=lambda-python \
/var/lang/lib/libpython${PYTHON_VERSION}*.so* \
/usr/lib64/
WORKDIR /var/lang
RUN <<EOF
mkdir -p bin include lib
ln -s /usr/lib64/python${PYTHON_VERSION} ./lib/python${PYTHON_VERSION}
ln -s /usr/lib64/python${PYTHON_VERSION}/lib-dynload ./lib/python${PYTHON_VERSION}/lib-dynload
ln -s /usr/lib64/libpython${PYTHON_VERSION}*.so* ./lib/
ln -s /usr/bin/python${PYTHON_VERSION} ./bin/python${PYTHON_VERSION}
ln -s /usr/bin/python${PYTHON_VERSION} ./bin/python
ln -s /usr/include/python${PYTHON_VERSION} ./include/python${PYTHON_VERSION}
EOF
WORKDIR /
# example final stage using ``scratch``
ARG PYTHON_VERSION
FROM scratch
ARG PYTHON_VERSION
ENV PYTHON_VERSION=${PYTHON_VERSION}
COPY --from=build /sysroot /
COPY --from=build /var/lang ./var/lang
COPY --from=build \
/var/lang/include/python${PYTHON_VERSION} \
/usr/include/python${PYTHON_VERSION}
COPY --from=build /usr/lib64/python${PYTHON_VERSION} /usr/lib64/python${PYTHON_VERSION}
COPY --from=build /usr/lib64/libpython${PYTHON_VERSION}*.so* /usr/lib64/
COPY --from=build /usr/lib64/python${PYTHON_VERSION}/lib-dynload /usr/lib64/python${PYTHON_VERSION}/lib-dynload
COPY --from=build /usr/lib64/python${PYTHON_VERSION}/site-packages /usr/lib64/python${PYTHON_VERSION}/site-packages
COPY --from=build /usr/bin/python${PYTHON_VERSION} /usr/bin/python${PYTHON_VERSION}
WORKDIR /
ENTRYPOINT ["/var/lang/bin/python"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment