Melika Kheirieh commited on
Commit
be3a2bc
·
1 Parent(s): 797db01

build: optimize Dockerfile and ignore configs (.gitignore, .dockerignore)

Browse files
Files changed (3) hide show
  1. .dockerignore +20 -0
  2. .gitignore +41 -1
  3. Dockerfile +28 -9
.dockerignore ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .git
2
+ .gitignore
3
+ __pycache__/
4
+ *.pyc
5
+ *.pyo
6
+ *.pyd
7
+ *.pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
10
+ .venv/
11
+ .env
12
+ .env.*
13
+ data/
14
+ benchmarks/data/
15
+ dist/
16
+ build/
17
+ .wheels/
18
+ *.sqlite
19
+ *.db
20
+ tests/
.gitignore CHANGED
@@ -1,6 +1,46 @@
1
- .env
2
  __pycache__/
 
 
 
 
 
 
3
  .venv/
 
 
 
 
4
  .idea/
 
5
  .DS_Store
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  data/
 
 
 
 
 
 
 
 
 
1
+ # --- Python basics ---
2
  __pycache__/
3
+ *.py[cod]
4
+ *.pyo
5
+ *.pyd
6
+ *.so
7
+
8
+ # --- Virtual environments ---
9
  .venv/
10
+ env/
11
+ venv/
12
+
13
+ # --- IDEs & OS ---
14
  .idea/
15
+ .vscode/
16
  .DS_Store
17
+
18
+ # --- Environment & secrets ---
19
+ .env
20
+ .env.*
21
+ *.key
22
+ *.pem
23
+
24
+ # --- Test & lint caches ---
25
+ .pytest_cache/
26
+ .mypy_cache/
27
+ .ruff_cache/
28
+ .coverage
29
+ htmlcov/
30
+
31
+ # --- Build & packaging ---
32
+ build/
33
+ dist/
34
+ *.egg-info/
35
+ .wheels/
36
+
37
+ # --- Data & databases ---
38
  data/
39
+ *.sqlite
40
+ *.db
41
+
42
+ # --- Logs & temp files ---
43
+ logs/
44
+ *.log
45
+ tmp/
46
+ *.tmp
Dockerfile CHANGED
@@ -1,15 +1,19 @@
1
  # ---------- Stage 1: Build wheels ----------
2
  FROM python:3.12-slim AS builder
3
 
4
- # Set working directory for the build stage
 
 
 
5
  WORKDIR /build
6
 
7
- # Install system dependencies required to compile some Python packages
 
8
  RUN apt-get update && apt-get install -y --no-install-recommends \
9
- build-essential libpq-dev && \
10
- rm -rf /var/lib/apt/lists/*
11
 
12
- # Copy only requirements first (so Docker caching works efficiently)
13
  COPY requirements.txt .
14
 
15
  # Build all dependencies as wheel files inside /wheels
@@ -20,21 +24,36 @@ RUN pip install --upgrade pip && \
20
  # ---------- Stage 2: Runtime image ----------
21
  FROM python:3.12-slim AS runtime
22
 
23
- # Set working directory for the application
 
 
 
24
  WORKDIR /app
25
 
 
 
 
26
  # Copy prebuilt wheels from the builder stage
27
  COPY --from=builder /wheels /wheels
28
 
29
- # Install dependencies from prebuilt wheels (no need to compile again)
30
  COPY requirements.txt .
31
- RUN pip install --no-cache-dir --find-links=/wheels -r requirements.txt
 
32
 
33
  # Copy the actual application code
34
  COPY . .
35
 
36
- # Expose the FastAPI port
 
 
 
 
 
 
 
37
  EXPOSE 8000
38
 
39
  # Start FastAPI with Uvicorn
 
40
  CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers"]
 
1
  # ---------- Stage 1: Build wheels ----------
2
  FROM python:3.12-slim AS builder
3
 
4
+ ENV PIP_NO_CACHE_DIR=1 \
5
+ PYTHONDONTWRITEBYTECODE=1 \
6
+ PYTHONUNBUFFERED=1
7
+
8
  WORKDIR /build
9
 
10
+ # If you truly need to compile deps, keep build-essential.
11
+ # If you use psycopg[binary], you can usually drop libpq-dev safely.
12
  RUN apt-get update && apt-get install -y --no-install-recommends \
13
+ build-essential \
14
+ && rm -rf /var/lib/apt/lists/*
15
 
16
+ # Leverage Docker layer caching
17
  COPY requirements.txt .
18
 
19
  # Build all dependencies as wheel files inside /wheels
 
24
  # ---------- Stage 2: Runtime image ----------
25
  FROM python:3.12-slim AS runtime
26
 
27
+ ENV PIP_NO_CACHE_DIR=1 \
28
+ PYTHONDONTWRITEBYTECODE=1 \
29
+ PYTHONUNBUFFERED=1
30
+
31
  WORKDIR /app
32
 
33
+ # Create a non-root user (security best practice)
34
+ RUN useradd -m appuser
35
+
36
  # Copy prebuilt wheels from the builder stage
37
  COPY --from=builder /wheels /wheels
38
 
39
+ # Install dependencies from wheels
40
  COPY requirements.txt .
41
+ RUN pip install --no-cache-dir --find-links=/wheels -r requirements.txt && \
42
+ rm -rf /wheels
43
 
44
  # Copy the actual application code
45
  COPY . .
46
 
47
+ # Optional: healthcheck (needs curl)
48
+ # RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/*
49
+ # HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
50
+ # CMD curl -fsS http://localhost:8000/healthz || exit 1
51
+
52
+ # Drop privileges
53
+ USER appuser
54
+
55
  EXPOSE 8000
56
 
57
  # Start FastAPI with Uvicorn
58
+ # Tip: you can tweak workers via env in deployment (e.g., UVICORN_WORKERS=2)
59
  CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers"]