tezvyn:

How do you structure a Dockerfile to leverage layer caching for dependencies?

AI-drafted, machine-checkedSource: docs.docker.combeginner

Tests Docker layer invalidation and cache-aware instruction ordering. Copy requirements.txt and run pip install before source code so deps cache independently. Red flag: copying everything at once or installing deps after code, busting cache every build.

WHAT THIS TESTS: Whether you understand that Docker builds images in layers and that each instruction creates a cacheable layer. Interviewers want to see if you can order Dockerfile instructions by change frequency to minimize rebuild time. In ML projects, dependency installation is often the slowest step, so avoiding redundant pip installs is critical for developer productivity.

A GOOD ANSWER COVERS: First, copy only the dependency manifest, requirements.txt, by itself. Second, run pip install or equivalent to build a layer that contains all Python packages. Third, copy the application source code in a separate later step. Fourth, mention that because layers are cached based on the checksum of inputs, changing a Python file only invalidates layers from the final COPY onward, leaving the heavy dependency layer intact. Optionally, mention using a .dockerignore file to prevent unnecessary files from being copied and busting cache.

COMMON WRONG ANSWERS: Copying the entire project with COPY . /app before running pip install means any code change invalidates the dependency layer. Running pip install in the same RUN command as code copying. Using ADD instead of COPY without reason. Forgetting to mention .dockerignore and letting large datasets or model artifacts trigger cache busts. Suggesting multi-stage builds without first solving the basic layer ordering problem.

LIKELY FOLLOW-UPS: How would you handle both requirements.txt and a new pyproject.toml? What if you need to install system dependencies like gcc before pip install? How does BuildKit improve caching beyond classic Docker? When would you use a multi-stage build to shrink the final image size? How do you handle large ML base images like CUDA that already contain heavy libraries?

ONE CONCRETE EXAMPLE: A Dockerfile for a FastAPI ML service might look like this: FROM python:3.11-slim; WORKDIR /app; COPY requirements.txt .; RUN pip install --no-cache-dir -r requirements.txt; COPY src/ ./src/; CMD uvicorn src.main:app --host 0.0.0.0. If you update main.py, Docker reuses the cached layer from RUN pip install and only reruns the final COPY and CMD. On a typical ML project with 2GB of dependencies, this saves 3 to 5 minutes per build.

Read the original → docs.docker.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.