Interview Prep/Real-World Interview/Write-a-Dockerfile tasks

Shrink a 1.2 GB Python image. What do you change?

AdvancedHands-onWrite-a-Dockerfile tasks
Answer
FROM python:3.12 AS build
WORKDIR /app
COPY requirements.txt .
RUN pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt

FROM python:3.12-slim
WORKDIR /app
COPY --from=build /wheels /wheels
RUN pip install --no-cache-dir /wheels/* && rm -rf /wheels
COPY . .
USER 10001
CMD ["python", "app.py"]

Key trick → Compile wheels in the full image (which has gcc and headers), then install them into -slim, so the compilers never ship. Use --no-cache-dir, a .dockerignore, and dive to find big layers.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Dockerfile tasks