|
1 | | -#!/bin/sh |
2 | | -# Build Stage |
| 1 | +# syntax=docker/dockerfile:1.6 |
| 2 | + |
| 3 | +# Build stage |
3 | 4 | FROM python:3.12 AS builder |
4 | 5 | WORKDIR /code |
5 | | -ADD . /code |
6 | | -RUN pip install "fastapi[standard]" |
7 | | -RUN pip install redis |
8 | | -RUN pip install sqlalchemy |
9 | | -RUN pip install pymysql |
10 | | -RUN pip install "sentry-sdk[fastapi]" |
11 | | -RUN pip install cryptography |
12 | | -RUN pip install pyinstaller |
13 | | -RUN pyinstaller -F main.py |
14 | | - |
15 | | -# Runtime |
| 6 | + |
| 7 | +# Use --mount=type=cache to cache pip downloads between builds |
| 8 | +COPY requirements.txt /code/requirements.txt |
| 9 | +COPY requirements.build.txt /code/requirements.build.txt |
| 10 | +RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ |
| 11 | + python -m pip install --upgrade pip wheel \ |
| 12 | + && pip wheel -r requirements.txt -w /wheels \ |
| 13 | + && pip wheel -r requirements.build.txt -w /wheels |
| 14 | + |
| 15 | +# Copy source code |
| 16 | +COPY . /code |
| 17 | + |
| 18 | +# Install dependencies from wheels cache (offline installation) |
| 19 | +RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ |
| 20 | + pip install --no-index --find-links=/wheels -r requirements.txt \ |
| 21 | + && pip install --no-index --find-links=/wheels -r requirements.build.txt |
| 22 | + |
| 23 | +# Run PyInstaller to create a single executable |
| 24 | +RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ |
| 25 | + pyinstaller -F main.py |
| 26 | + |
| 27 | + |
| 28 | +# Runtime stage |
16 | 29 | FROM ubuntu:24.04 AS runtime |
17 | 30 | WORKDIR /app |
18 | | -COPY --from=builder /code/dist/main . |
19 | | -EXPOSE 8000 |
20 | | -ENTRYPOINT ["./main"] |
| 31 | + |
| 32 | +# Install runner dependencies |
| 33 | +RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 34 | + ca-certificates \ |
| 35 | + && rm -rf /var/lib/apt/lists/* |
| 36 | + |
| 37 | +# Copy the built executable from the builder stage |
| 38 | +COPY --from=builder /code/dist/main /app/main |
| 39 | + |
| 40 | +# Health check |
| 41 | +# HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ |
| 42 | +# CMD curl -fsS http://localhost:8080/health || exit 1 |
| 43 | + |
| 44 | +ENTRYPOINT ["/app/main"] |
0 commit comments