Multi-stage Docker build with Poetry and venv
https://stackoverflow.com/a/57886655/1355754
Last active
March 30, 2024 05:25
-
-
Save cjolowicz/6bc8e0be40b3cb171abdd9f49d2aa333 to your computer and use it in GitHub Desktop.
Multi-stage Docker build with Poetry and venv
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 | |
set -e | |
. /venv/bin/activate | |
while ! flask db upgrade | |
do | |
echo "Retry..." | |
sleep 1 | |
done | |
exec gunicorn --bind 0.0.0.0:5000 --forwarded-allow-ips='*' wsgi: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.6-alpine3.11 as base | |
ENV PYTHONFAULTHANDLER=1 \ | |
PYTHONHASHSEED=random \ | |
PYTHONUNBUFFERED=1 | |
WORKDIR /app | |
FROM base as builder | |
ENV PIP_DEFAULT_TIMEOUT=100 \ | |
PIP_DISABLE_PIP_VERSION_CHECK=1 \ | |
PIP_NO_CACHE_DIR=1 \ | |
POETRY_VERSION=1.0.5 | |
RUN apk add --no-cache gcc libffi-dev musl-dev postgresql-dev | |
RUN pip install "poetry==$POETRY_VERSION" | |
RUN python -m venv /venv | |
COPY pyproject.toml poetry.lock ./ | |
RUN poetry export -f requirements.txt | /venv/bin/pip install -r /dev/stdin | |
COPY . . | |
RUN poetry build && /venv/bin/pip install dist/*.whl | |
FROM base as final | |
RUN apk add --no-cache libffi libpq | |
COPY --from=builder /venv /venv | |
COPY docker-entrypoint.sh wsgi.py ./ | |
CMD ["./docker-entrypoint.sh"] |
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
import your_app | |
app = your_app.create_app() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment