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

Members can save this page, take notes and track progress. See membership · Sign in

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

← All Docker questions

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake