Complete dockerfile toolkit with generation and validation capabilities
74
92%
Does it follow best practices?
Impact
—
No eval scenarios have been run
Risky
Do not use without reviewing
A data team inherited a Dockerfile for a FastAPI application (analytics-api) from a contractor. After a recent Docker image size audit, the operations team found the image is 2.1 GB — nearly ten times what they expected. The image is also flagged by the security scanner for including build tools, stale apt lists, and cached pip packages in the final image.
The team needs the Dockerfile rewritten from scratch so it produces a small, clean production image. The application uses Python 3.11, the package list is in requirements.txt, and the server starts with uvicorn app.main:app --host 0.0.0.0 --port 8000.
Produce a new Dockerfile for the analytics-api Python/FastAPI application.
Also produce an appropriate .dockerignore for a Python project.
Place both files in the current directory.
The original (problematic) Dockerfile is:
FROM python:3.11
RUN apt-get update
RUN apt-get install -y curl gcc build-essential
RUN pip install --upgrade pip
ADD . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD uvicorn app.main:app --host 0.0.0.0 --port 8000apt-get update, apt-get install, and rm -rf /var/lib/apt/lists/* in a SINGLE RUN instruction using &&rm -rf /var/lib/apt/lists/*) inside the SAME RUN as apt-get install — not a separate RUNCOPY for all file copy operations — no ADDpython:3.11-slim) — not :latestpython:X.X-slim, python:X.X-alpine, or distroless) rather than the full python:3.11USER instruction before CMD/ENTRYPOINTrequirements.txt BEFORE the pip install RUN instruction (layer caching)CMD.dockerignore with at least one Python-specific entry (__pycache__/, *.pyc, or .venv/)apt-get update, apt-get install, and cache cleanup are split across multiple RUN instructionsrm -rf /var/lib/apt/lists/* is a separate RUN instruction instead of being in the same layer as apt-get installADD is used instead of COPYFROM uses :latest or the full non-slim python:3.11 imageUSER instruction is absent or placed after CMDrequirements.txt is not copied before the pip install stepCMD uses shell string form instead of JSON array syntax.dockerignore is missing Python-specific entries