Created
June 25, 2019 13:10
-
-
Save khvn26/4fefb830a39818e3870b4d90c8db76da to your computer and use it in GitHub Desktop.
Slim Python multistage build, hacky but simple and apparently correct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
# --root заставляет apk ставить пакеты под нужным префиксом | |
# Но при этом нужно восстановить его работоспособность: | |
# --initdb воссоздаёт бд | |
# --keys-dir и --repositories-file указывают куда смотреть и чем авторизовываться (логично) | |
apk --initdb \ | |
--root /build/ \ | |
--keys-dir /etc/apk/keys \ | |
--repositories-file /etc/apk/repositories \ | |
"$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
# Pip умеет передавать кастомные аргументы билд-системе | |
PATH=$PATH:/build/usr/bin | |
pip "$@" --global-option=build_ext \ | |
--global-option="-I/usr/include:/build/usr/include" \ | |
--global-option="-L/usr/lib:/build/usr/lib" \ | |
--prefix /build/usr/local/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM prefix-build-base as build | |
ENV PATH=$PATH:/build/usr/bin | |
COPY requirements.txt . | |
RUN apk add --no-cache git libxslt-dev libxml2-dev &&\ | |
# ^ здесь хачно: libxml and libxslt тащат свои билд-зависимости, не уважая наш префикс, | |
# но мы можем с этим справиться, поставив их глобально | |
# А теперь ставим всё в префикс: | |
bapk add --no-cache postgresql-dev libxslt-dev libxml2-dev &&\ | |
bpip install --no-cache-dir -r requirements.txt | |
# Финальный тонкий стейдж | |
FROM python:3.7-alpine3.8 | |
COPY --from=build /build/ / | |
ADD . /usr/src/app | |
WORKDIR /usr/src/app | |
# Всё! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3.7-alpine3.8 | |
COPY bapk.sh /usr/bin/bapk | |
COPY bpip.sh /usr/bin/bpip | |
RUN mkdir -p /build/usr/ # Это префикс для наших build-зависимостей |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment