Skip to content

Instantly share code, notes, and snippets.

@awnion
Last active July 3, 2023 18:31
Show Gist options
  • Save awnion/9366c3230d04de89de2b477e54cf3d91 to your computer and use it in GitHub Desktop.
Save awnion/9366c3230d04de89de2b477e54cf3d91 to your computer and use it in GitHub Desktop.
Python clean slim app docker file
# syntax=docker/dockerfile:1.5.2
##
## Python clean slim app docker file
##
FROM python:3.11.4-slim-bullseye as builder
## RUN apt install here
RUN --mount=type=cache,target=/root/.cache/pip \
python -m pip install -U pip && python -m pip install hatch
WORKDIR /app
COPY . .
RUN --mount=type=cache,target=/root/.cache/pip \
hatch build -t wheel \
&& python -m venv /app/.venv \
&& /app/.venv/bin/python -m pip install /app/dist/*.whl
FROM python:3.11.4-slim-bullseye as prepare
## RUN apt install here
RUN groupadd -g 999 app \
&& useradd -r -u 999 -g app app
COPY --from=builder /app /app
WORKDIR /app/workdir
RUN chown -R app:app /app
FROM scratch # (optional) just to squash
EXPOSE 8000
USER 999
ENV PATH="/app/.venv/bin:$PATH"
ENTRYPOINT []
CMD ["python", "-m", "app.main"] # your app name
WORKDIR /app/workdir
COPY --from=prepare / /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment