fahmiaziz98 commited on
Commit
58a73bf
Β·
1 Parent(s): 7933a0e

refactor dockerfile 2

Browse files
Files changed (1) hide show
  1. Dockerfile +25 -6
Dockerfile CHANGED
@@ -1,40 +1,59 @@
1
- # Stage 1: Build dependencies
 
 
2
  FROM python:3.11.11-slim AS build
3
 
 
 
 
 
 
 
 
 
4
  RUN apt-get update && apt-get install -y \
5
  build-essential \
6
  libjpeg-dev \
7
  zlib1g-dev \
8
  && rm -rf /var/lib/apt/lists/*
9
 
 
10
  RUN useradd -m -u 1000 user
11
-
12
  USER user
13
- ENV PATH="/home/user/.local/bin:$PATH"
14
 
 
15
  WORKDIR /app
16
 
 
17
  COPY --chown=user requirements.txt .
18
-
19
  RUN pip install --no-cache-dir --user -r requirements.txt
20
 
21
- # Stage 2: Final image
 
 
22
  FROM python:3.11.11-slim
23
 
 
24
  RUN apt-get update && apt-get install -y \
25
  libjpeg-dev \
26
  zlib1g-dev \
27
  && rm -rf /var/lib/apt/lists/*
28
 
 
29
  RUN useradd -m -u 1000 user
30
  USER user
 
 
31
  ENV PATH="/home/user/.local/bin:$PATH"
32
 
33
  WORKDIR /app
34
 
 
35
  COPY --from=build /home/user/.local /home/user/.local
 
 
36
  COPY --chown=user . .
37
 
 
38
  EXPOSE 7860
39
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
40
-
 
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"]