Optimize Dockerfile with multi-stage build
Build and Push (main) / build (push) Canceled after 1m34s
Build and Push (main) / build (push) Canceled after 1m34s
This commit is contained in:
+16
-3
@@ -1,4 +1,4 @@
|
|||||||
FROM python:3.12-slim
|
FROM python:3.12-slim AS builder
|
||||||
|
|
||||||
# Prevent .pyc files and enable unbuffered stdout/stderr
|
# Prevent .pyc files and enable unbuffered stdout/stderr
|
||||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||||
@@ -6,14 +6,27 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
|
|||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Install build tools for C extensions (e.g. TgCrypto), then clean up
|
# Install build tools in builder stage only
|
||||||
RUN apt-get update && \
|
RUN apt-get update && \
|
||||||
apt-get install -y --no-install-recommends gcc libc6-dev && \
|
apt-get install -y --no-install-recommends gcc libc6-dev && \
|
||||||
rm -rf /var/lib/apt/lists/*
|
rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Install deps first — layer cache reuse on code-only changes
|
# Install deps first — layer cache reuse on code-only changes
|
||||||
COPY requirements.txt .
|
COPY requirements.txt .
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||||
|
pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
# Runtime stage - clean slate
|
||||||
|
FROM python:3.12-slim
|
||||||
|
|
||||||
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||||
|
PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy only installed packages from builder
|
||||||
|
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
|
||||||
|
COPY --from=builder /usr/local/bin /usr/local/bin
|
||||||
|
|
||||||
# Copy source
|
# Copy source
|
||||||
COPY main.py config.py ./
|
COPY main.py config.py ./
|
||||||
|
|||||||
Reference in New Issue
Block a user