Skip to content

Instantly share code, notes, and snippets.

@peterroelants
Last active May 2, 2021 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterroelants/e344ac416948296f7fcdc84a20ce6eb5 to your computer and use it in GitHub Desktop.
Save peterroelants/e344ac416948296f7fcdc84a20ce6eb5 to your computer and use it in GitHub Desktop.
Build minimal Python environment with Pydantic
# Build with:
# DOCKER_BUILDKIT=1 docker build --progress=plain --target pydantic_env --tag pydantic-test-env --file ./pydantic/pydantic.static.Dockerfile .
#
# Uses multi-stage builds.
#
# Stage 1: Temporarity builder
FROM python:3.9.2-slim-buster as builder
ARG PYDANTIC_VERSION=1.8.1
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
# Install python packages
RUN set -eux \
&& cd /tmp/ \
# Install build dependencies
&& apt-get update \
&& apt-get install --quiet --yes --no-install-recommends \
# `build-essential` contains gcc: https://packages.debian.org/buster/build-essential
build-essential \
&& apt-get clean \
# Install python packages with pip using compilation
&& python -m pip install --upgrade pip \
# Install Python build dependencies
&& pip install --no-cache-dir \
Cython==0.29.22 \
pip \
setuptools \
# Install PyDantic
&& pip install \
-vvv \
--no-cache-dir \
--no-binary pydantic \
--global-option=build_ext \
--global-option=-j8 \
pydantic==${PYDANTIC_VERSION} \
# Cleanup
# Clean Python build deps
&& pip uninstall --yes \
Cython \
setuptools \
# Cleanup build deps
&& apt-get purge --auto-remove --yes \
build-essential \
&& apt-get clean \
&& rm --recursive --force /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Stage 2: Final environent
FROM python:3.9.2-slim-buster as pydantic_env
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
# Copy Python env from builder to clean container image
COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
# Below is for testing:
#
# Some test files to make sure Pydantic works.
# Uncomment the next line if you have some tests.
# COPY --chown=555 ./pydantic/benchmark/ /test_benchmark/
# Ensure it works by running
# docker run pydantic-test-env python /test_benchmark/run.py
# Get size of pydantic env with:
# docker run pydantic-test-env /bin/bash -c 'du -sh /usr/local/lib/python3.9/site-packages/pydantic'
# List pydantic dir with sizes:
# docker run pydantic-test-env /bin/bash -c 'du -sh /usr/local/lib/python3.9/site-packages/pydantic/* | sort -h'
# Build with:
# docker buildx build --progress=plain --load --target pydantic_env --tag pydantic-test-env-alpine --file ./python.alpine.Dockerfile .
#
# Uses multi-stage builds.
#
# Stage 1: Common image
FROM python:3.9.4-alpine3.13 as common
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
# Stage 2: Temporarity builder
FROM common as builder
# Install python packages
RUN set -eux \
&& cd /tmp/ \
# Install build dependencies
&& apk add --no-cache --virtual .build-deps \
alpine-sdk \
&& echo "gcc version: $(gcc --version )" \
# Install python packages with pip using compilation
&& echo "python version: $(python --version )" \
&& echo "pip version: $(pip --version )" \
# Install Python build dependencies
&& pip install --no-cache-dir \
Cython==0.29.23 \
setuptools==56.0.0 \
# Install PyDantic
&& pip install \
--verbose \
--no-cache-dir \
--no-binary pydantic \
--global-option=build_ext \
pydantic==1.8.1 \
# Cleanup
# Clean Python build deps
&& pip uninstall --yes \
Cython \
setuptools \
# Cleanup
&& apk del .build-deps \
&& rm -rf /var/cache/apk/* /tmp/* /var/tmp/*
# Stage 3: Final environent
FROM common as pydantic_env
# Copy Python env from builder to clean container image
COPY --from=builder /usr/local/lib/python3.9/site-packages/ /usr/local/lib/python3.9/site-packages/
# Below is for testing:
#
# Some test files to make sure Pydantic works.
# Uncomment the next line if you have some tests.
# COPY --chown=555 ./benchmark/ /test_benchmark/
# Ensure it works by running
# docker run pydantic-test-env-alpine python /test_benchmark/run.py
# Get size of pydantic env with:
# docker run pydantic-test-env-alpine /bin/sh -c 'du -sh /usr/local/lib/python3.9/site-packages/pydantic'
# List pydantic dir with sizes:
# docker run pydantic-test-env-alpine /bin/sh -c 'du -sh /usr/local/lib/python3.9/site-packages/pydantic/* | sort'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment