fahmiaziz98 commited on
Commit
2bbdfee
Β·
1 Parent(s): 58a73bf

refactor dockerfile 3

Browse files
Files changed (1) hide show
  1. Dockerfile +22 -40
Dockerfile CHANGED
@@ -1,59 +1,41 @@
1
- # ────────────────────────────────
2
- # πŸ”¨ Stage 1: Build dependencies
3
- # ────────────────────────────────
4
  FROM python:3.11.11-slim AS build
5
 
6
- # ⏱️ Set environment variables for pip optimization during build
7
  ENV PIP_DEFAULT_TIMEOUT=100 \
8
  PYTHONUNBUFFERED=1 \
9
  PIP_DISABLE_PIP_VERSION_CHECK=1 \
10
- PIP_NO_CACHE_DIR=1 \
11
- PATH="/home/user/.local/bin:$PATH"
12
-
13
- # πŸ”§ Install system dependencies needed to build Python packages
14
- RUN apt-get update && apt-get install -y \
15
- build-essential \
16
- libjpeg-dev \
17
- zlib1g-dev \
18
- && rm -rf /var/lib/apt/lists/*
19
-
20
- # πŸ‘€ Create non-root user
21
- RUN useradd -m -u 1000 user
22
- USER user
23
 
24
- # πŸ“‚ Set working directory
25
  WORKDIR /app
26
 
27
- # πŸ“¦ Install Python dependencies to user space
28
- COPY --chown=user requirements.txt .
29
- RUN pip install --no-cache-dir --user -r requirements.txt
30
 
31
- # ────────────────────────────────
32
- # 🏁 Stage 2: Final image (runtime only)
33
- # ────────────────────────────────
34
- FROM python:3.11.11-slim
35
 
36
- # πŸ”§ Install runtime libraries (build tools tidak dibutuhkan)
37
- RUN apt-get update && apt-get install -y \
38
- libjpeg-dev \
39
- zlib1g-dev \
40
- && rm -rf /var/lib/apt/lists/*
41
 
42
- # πŸ‘€ Create same user again in runtime
43
- RUN useradd -m -u 1000 user
44
- USER user
45
 
46
- # πŸ“‚ Set runtime env
47
- ENV PATH="/home/user/.local/bin:$PATH"
 
 
 
 
 
 
 
48
 
49
  WORKDIR /app
50
 
51
- # 🚚 Copy only installed packages from build stage
52
- COPY --from=build /home/user/.local /home/user/.local
53
 
54
- # πŸ“¦ Copy app code
55
- COPY --chown=user . .
 
 
 
56
 
57
- # 🌐 Expose port and launch
58
  EXPOSE 7860
59
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
 
 
1
  FROM python:3.11.11-slim AS build
2
 
 
3
  ENV PIP_DEFAULT_TIMEOUT=100 \
4
  PYTHONUNBUFFERED=1 \
5
  PIP_DISABLE_PIP_VERSION_CHECK=1 \
6
+ PIP_NO_CACHE_DIR=1
 
 
 
 
 
 
 
 
 
 
 
 
7
 
 
8
  WORKDIR /app
9
 
10
+ COPY requirements.txt .
 
 
11
 
12
+ RUN pip install --upgrade pip && pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt
 
 
 
13
 
14
+ FROM python:3.11.11-slim AS final
 
 
 
 
15
 
16
+ ENV PATH="/home/appuser/.local/bin:$PATH"
 
 
17
 
18
+ RUN set -ex \
19
+ && addgroup --system --gid 1001 appgroup \
20
+ && adduser --system --uid 1001 --gid 1001 --home /home/appuser appuser \
21
+ && apt-get update \
22
+ && apt-get upgrade -y \
23
+ && apt-get install -y libjpeg-dev zlib1g-dev \
24
+ && apt-get autoremove -y \
25
+ && apt-get clean -y \
26
+ && rm -rf /var/lib/apt/lists/*
27
 
28
  WORKDIR /app
29
 
30
+ COPY --from=build /wheels /wheels
31
+ COPY requirements.txt .
32
 
33
+ RUN pip install --no-cache-dir --no-warn-script-location --no-index --find-links=/wheels -r requirements.txt
34
+
35
+ COPY . .
36
+
37
+ USER appuser
38
 
 
39
  EXPOSE 7860
40
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
41
+