Skip to content

Commit 3879323

Browse files
Masterain98Copilot
andauthored
Improve Docker build (#23)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 438b941 commit 3879323

File tree

5 files changed

+62
-23
lines changed

5 files changed

+62
-23
lines changed

.dockerignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.git
2+
.gitignore
3+
__pycache__/
4+
*.pyc
5+
*.pyo
6+
*.pyd
7+
.build/
8+
dist/
9+
.wheels/
10+
.cache/
11+
.env
12+
*.log
13+
.mypy_cache/
14+
.pytest_cache/
15+
.vscode/
16+
.idea/

.env.example

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Docker Image Settings
22
IMAGE_NAME=uigf-api
33
CONTAINER_NAME=UIGF-API
4-
IMAGE_TAG=2.0
5-
EXTERNAL_PORT=3052
64

75
# App Settings
86
TOKEN=AppToken

Dockerfile

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
1-
#!/bin/sh
2-
# Build Stage
1+
# syntax=docker/dockerfile:1.6
2+
3+
# Build stage
34
FROM python:3.12 AS builder
45
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
1629
FROM ubuntu:24.04 AS runtime
1730
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"]

docker-compose.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ services:
66
context: .
77
dockerfile: Dockerfile
88
target: runtime
9-
image: ${IMAGE_NAME}:${IMAGE_TAG}
9+
image: ${IMAGE_NAME}:latest
1010
container_name: ${CONTAINER_NAME}-Server
11-
ports:
12-
- "${EXTERNAL_PORT}:8080"
1311
volumes:
1412
- ./cache:/app/cache
1513
- ./dict:/app/dict
16-
- ./.env:/app/.env
14+
- ./.env:/app/.env:ro
1715
restart: unless-stopped
1816
depends_on:
1917
- tunnel
@@ -33,4 +31,4 @@ services:
3331
restart: unless-stopped
3432
command: tunnel --no-autoupdate run
3533
environment:
36-
- TUNNEL_TOKEN=${TUNNEL_TOKEN}
34+
- TUNNEL_TOKEN=${TUNNEL_TOKEN}

requirements.build.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pyinstaller==6.10.0
2+
# setuptools is pinned below version 81 due to incompatibility with PyInstaller; see https://github.com/pyinstaller/pyinstaller/issues/7994
3+
setuptools<81

0 commit comments

Comments
 (0)