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.
More on Write-a-Dockerfile tasks
Write a production Dockerfile for a Node.js API using a multi-stage build.IntermediateWrite a Dockerfile for a Go service that produces a tiny image (under 20 MB).IntermediateWrite a multi-stage Dockerfile for a Java Spring Boot app built with Maven.IntermediateWrite a Dockerfile for a static React site served by Nginx.BeginnerThis Dockerfile rebuilds dependencies on every code change. Fix it.BeginnerWrite a `.dockerignore` for a typical Node or Python project and explain why it matters.Beginner