Docker interview question · Most asked
Write a production Dockerfile for a Python web app: small, non-root and cache-friendly.
Hands-on Asked in DevOps Engineer · Site Reliability Engineer (SRE) · Cloud Engineer · Backend Engineer · Full Stack Engineer interviews
Summary
Use a multi-stage build: install dependencies into a virtual environment in a builder stage, copy only that and the application into a slim runtime image, order layers so dependency installs are cached, run as a non-root user, and start the server in exec form so it receives signals.
Longer Answer
Also mention: pin the base image (by digest in high-security setups), a .dockerignore to keep secrets and .git
out of the build context, HEALTHCHECK (or Kubernetes probes), and scanning the image in CI.
Example
# syntax=docker/dockerfile:1
FROM python:3.13-slim AS build
WORKDIR /app
RUN python -m venv /venv
ENV PATH=/venv/bin:$PATH
COPY requirements.txt .
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
FROM python:3.13-slim
RUN useradd --create-home --uid 10001 app
COPY --from=build /venv /venv
WORKDIR /app
COPY --chown=app:app . .
ENV PATH=/venv/bin:$PATH PYTHONUNBUFFERED=1
USER app
EXPOSE 8000
CMD ["gunicorn", "-k", "uvicorn.workers.UvicornWorker", "-b", "0.0.0.0:8000", "app.main:app"]
Key Points
- Multi-stage build.
- Dependencies before code for caching.
- Non-root user.
- Exec-form CMD for signals.
- .dockerignore and image scanning.
Explanation
What interviewers look for
A correct, secure Dockerfile and the reasons for each line.
More Docker questions
- How does Docker build caching work, and how do you order a Dockerfile to use it well?
- ARG or ENV: what is the difference, and how do you pass secrets into a build safely?
- What is a container, and how does it differ from a virtual machine?
- What is the difference between an image and a container, and how do image layers work?
- What are Dockerfile best practices for production images?
- What are multi-stage builds, and why use them?
- CMD or ENTRYPOINT: what is the difference, and what are exec and shell forms?
- How do you secure Docker containers and images?