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

This Dockerfile rebuilds dependencies on every code change. Fix it.

BeginnerHands-onWrite-a-Dockerfile tasks
Answer

Given:

FROM python:3.12-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

Fixed:

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Key trick → Order layers from least to most frequently changing. COPY . . before install invalidates the pip layer on every source edit.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Dockerfile tasks