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

Write a Dockerfile for a Python Flask app that runs with gunicorn as a non-root user.

BeginnerHands-onWrite-a-Dockerfile tasks
Answer
FROM python:3.12-slim
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN useradd --system --uid 10001 appuser
USER 10001
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "app:app"]

Key trick → PYTHONUNBUFFERED=1 makes logs appear in docker logs immediately, and binding to 0.0.0.0 (not 127.0.0.1) makes the port reachable from outside the container.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Dockerfile tasks