Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KrustyHack/08c37c4bec76762ae2d2fe7e83244c77 to your computer and use it in GitHub Desktop.
Save KrustyHack/08c37c4bec76762ae2d2fe7e83244c77 to your computer and use it in GitHub Desktop.
Simple Python Docker file with Gunicorn/Uvicorn
FROM python:3.12-bookworm AS builder
RUN pip install poetry
ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache
WORKDIR /app
COPY pyproject.toml poetry.lock ./
RUN touch README.md
RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install --no-root
FROM python:3.12-slim AS runtime
ENV PORT=8000
ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH"
COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
COPY src ./app
WORKDIR /app
CMD exec gunicorn api:app --workers 1 --threads 8 --timeout 0 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:$PORT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment