faisalshah012003 commited on
Commit
805b062
·
verified ·
1 Parent(s): a13509e

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +46 -3
Dockerfile CHANGED
@@ -1,10 +1,53 @@
1
- FROM python:3.10
 
2
 
 
 
 
 
 
 
3
  RUN useradd user
4
- USER user
 
5
  ENV HOME=/home/user \
6
  PATH=/home/user/.local/bin:$PATH
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  WORKDIR $HOME/app
 
 
 
 
 
8
  COPY --chown=user ./ $HOME/app
9
- RUN pip install -r requirements.txt
 
 
10
  CMD ["python", "app.py"]
 
1
+ # Build stage
2
+ FROM python:3.10 as builder
3
 
4
+ # Install build dependencies
5
+ RUN apt-get update && apt-get install -y --no-install-recommends \
6
+ build-essential \
7
+ && rm -rf /var/lib/apt/lists/*
8
+
9
+ # Create user
10
  RUN useradd user
11
+
12
+ # Set up environment
13
  ENV HOME=/home/user \
14
  PATH=/home/user/.local/bin:$PATH
15
+
16
+ WORKDIR $HOME/app
17
+
18
+ # Copy requirements first for better caching
19
+ COPY --chown=user requirements.txt .
20
+
21
+ # Install Python dependencies to user directory
22
+ USER user
23
+ RUN pip install --user --no-cache-dir -r requirements.txt
24
+
25
+ # Production stage
26
+ FROM python:3.10-slim as production
27
+
28
+ # Install only runtime dependencies if needed
29
+ RUN apt-get update && apt-get install -y --no-install-recommends \
30
+ # Add any runtime dependencies here \
31
+ && rm -rf /var/lib/apt/lists/* \
32
+ && apt-get clean
33
+
34
+ # Create user
35
+ RUN useradd user
36
+
37
+ # Set up environment
38
+ ENV HOME=/home/user \
39
+ PATH=/home/user/.local/bin:$PATH \
40
+ PYTHONUNBUFFERED=1 \
41
+ PYTHONDONTWRITEBYTECODE=1
42
+
43
  WORKDIR $HOME/app
44
+
45
+ # Copy installed packages from builder stage
46
+ COPY --from=builder --chown=user /home/user/.local /home/user/.local
47
+
48
+ # Copy application code
49
  COPY --chown=user ./ $HOME/app
50
+
51
+ USER user
52
+
53
  CMD ["python", "app.py"]