/ fastapi-template / Dockerfile
Dockerfile
 1  FROM python:3.10-alpine AS builder
 2  
 3  RUN apk add --no-cache build-base gcc musl-dev libffi-dev postgresql14-dev git
 4  
 5  WORKDIR /build
 6  
 7  RUN pip install poetry
 8  
 9  COPY pyproject.toml /build/
10  COPY poetry.lock /build/
11  
12  RUN set -ex \
13      && virtualenv .venv \
14      && . .venv/bin/activate \
15      && poetry install -n --no-root --without dev
16  
17  COPY api/version.py /build/
18  COPY .git /build/.git/
19  RUN python version.py
20  
21  
22  FROM python:3.10-alpine
23  
24  LABEL org.opencontainers.image.source="https://github.com/Defelo/fastapi-template"
25  
26  WORKDIR /app
27  
28  RUN set -x \
29      && apk add --no-cache curl libpq \
30      && addgroup -g 1000 api \
31      && adduser -G api -u 1000 -s /bin/sh -D -H api
32  
33  USER api
34  
35  EXPOSE 8000
36  
37  COPY --from=builder /build/.venv/lib /usr/local/lib
38  COPY alembic /app/alembic
39  COPY alembic.ini /app/
40  COPY --from=builder /build/VERSION /app/
41  
42  COPY api /app/api/
43  
44  HEALTHCHECK --interval=20s --timeout=5s --retries=1 \
45      CMD curl -fI http://localhost:${PORT:-8000}/status
46  
47  CMD python -m alembic upgrade head && python -m api