-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
84 lines (65 loc) · 2.55 KB
/
Copy pathDockerfile
File metadata and controls
84 lines (65 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Base Image
FROM python:3.11-slim
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# What it does:
# Uses an official Python image with Python 3.11 preinstalled.
# slim = a stripped-down Debian Linux with only essentials.
# Why it matters:
# ✅ Smaller image size → faster builds → faster deploys → less bandwidth
# ✅ Still compatible with apt-get
# ❌ Not Alpine (which causes many Python dependency nightmares)
# Mental model: Give me a tiny Linux computer that already has Python 3.11 inside.
# Working directory
WORKDIR /app
# What it does:
# Creates /app folder inside the container
# Sets it as the default directory for:
# COPY
# RUN
# CMD
# everything
# Why it matters:
# Keeps your container clean and organized
# No files scattered in / like a junk drawer
# Mental model: “From now on, pretend the container lives inside /app.”
# System deps
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# What it does:
# apt-get update → Updates Linux package list
# apt-get install -y build-essential
# → Installs: gcc, g++
# make (needed to compile Python packages like numpy, cryptography, etc.)
# rm -rf /var/lib/apt/lists/*
# → Deletes cached package list to reduce image size
# Why it matters:
# Some Python packages are not pure Python → they compile C/C++
# Without this: ❌ pip install fails with cryptic errors
# Mental model: “Install a compiler so Python packages can build their muscles.”
# Copy only requirements first
COPY requirements.txt .
# Copies only requirements.txt into container
# If your code changes but req.txt stays same, then docker reuses the pip install layer and build becomes 10x faster
# Mental mode: Install dependencies only when dependency list changes.
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Creates log folder
RUN mkdir -p /app/logs
# Copy app code
COPY . .
# Copies everything from your project folder into /app
# Expose port
EXPOSE 5000
# The container listens on port 8000
# Mental mode: Hey Platform, traffic comes in on 8000
# Run the app using gunicorn (production server)
CMD ["gunicorn", "app:app", "--workers", "1", "--threads", "1", "--timeout", "120", "--bind", "0.0.0.0:5000"]
# Breakdown:
# gunicorn: Production WSGI server
# -w 2 : 2 worker processes
# -b 0.0.0.0:8000: listen on all interfaces, port 8000
# app:app: app = Flask(__name__)