Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
63 lines (47 loc) · 1.96 KB
/
Copy pathDockerfile
File metadata and controls
63 lines (47 loc) · 1.96 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
# ==========================================
# Stage 1: Build dependencies
# ==========================================
FROM python:3.11-slim AS builder
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
# Install build dependencies (required for compiling wheel files like psycopg/cryptography packages)
RUN apt-get update \
&& apt-get install --no-install-recommends -y build-essential \
&& rm -rf /var/lib/apt/lists/*
# Copy requirements and compile them to wheels
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip \
&& pip wheel --no-cache-dir --wheel-dir /app/wheels -r requirements.txt
# ==========================================
# Stage 2: Runtime image
# ==========================================
FROM python:3.11-slim AS runtime
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
FLASK_DEBUG=0 \
PORT=8000 \
WEB_CONCURRENCY=1
# Install runtime utilities only (curl is needed for the container healthcheck)
RUN apt-get update \
&& apt-get install --no-install-recommends -y curl \
&& rm -rf /var/lib/apt/lists/*
# Copy compiled wheels from builder stage and install them instantly without compiling
COPY --from=builder /app/wheels /wheels
COPY --from=builder /app/requirements.txt .
RUN pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir --no-index --find-links=/wheels -r requirements.txt \
&& rm -rf /wheels
# Copy application files
COPY . .
# Set up non-root user for security (principle of least privilege)
RUN addgroup --system appuser \
&& adduser --system --ingroup appuser appuser \
&& chown -R appuser:appuser /app
USER appuser
EXPOSE 8000
# Continuous health monitoring of the application using the Flask diagnostic endpoint
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
CMD curl -f http://localhost:$PORT/healthz || exit 1
CMD ["sh", "-c", "exec gunicorn 'switchbot_dashboard:create_app()' --config gunicorn.conf.py"]