Create a boilerplate for a Dockerized Flask application with Gunicorn.
- User
Create a boilerplate for a Dockerized Flask application with Gunicorn.
- Assistant
Creating Dockerized Flask App Structure
The user wants a boilerplate for a Dockerized Flask application with Gunicorn. It should be straightforward yet comprehensive, including a Dockerfile, docker-compose, app.py, requirements.txt, gunicorn config, and possibly entrypoint.sh and .dockerignore. I need to consider using Python 3.10 or 3.11 and ensure best practices like a non-root user. The structure will be conventional, with project files organized for clarity. Gunicorn will serve the app using "wsgi:app." I'll keep it simple yet functional!Configuring Flask with Gunicorn
I need to setup the environment variable FLASK_ENV or configure Gunicorn workers in
gunicorn.conf.py. I plan to use multiprocessing for setting the number of workers based on CPU count. The essential configurations will include binding to 0.0.0.0:8000 and setting appropriate log levels.For the Docker setup, I’ll use a Python 3.11-slim base image, install necessary packages, and manage dependencies via requirements.txt. My app.py will define the Flask app, while wsgi.py will import it for Gunicorn. I have to keep naming conflicts in mind, but it should work.Implementing Flask Application Structure
I’ll start by implementing the
app.pyfile, which will initialize a Flask app with two routes: one for health status and another for a welcome message. I'll then create awsgi.pyfile that imports the app instance.The user wants a boilerplate, so I'll keep my response concise and focus on providing a minimal structure. I’ll include the necessary commands for building and running the Docker container, along with a file tree and labeled code blocks, maintaining a clear yet helpful context without excessive detail. Let’s get started! Below is a minimal boilerplate for a Dockerized Flask application served with Gunicorn.
Project structure
flask-gunicorn-app/ ├─ app.py ├─ wsgi.py ├─ requirements.txt ├─ gunicorn.conf.py ├─ Dockerfile └─ .dockerignore
app.pyfrom flask import Flask, jsonify def create_app() -> Flask: app = Flask(__name__) @app.route("/health") def health(): return jsonify(status="ok") @app.route("/") def index(): return "Hello from Flask + Gunicorn in Docker!" return app # Gunicorn will import this app = create_app()
wsgi.pyfrom app import app # Expose "app" for Gunicorn ("wsgi:app")
requirements.txtflask==3.0.3 gunicorn==23.0.0(Use versions appropriate for your project if different.)
gunicorn.conf.pyimport multiprocessing bind = "0.0.0.0:8000" workers = multiprocessing.cpu_count() * 2 + 1 timeout = 30 loglevel = "info" accesslog = "-" errorlog = "-"
DockerfileFROM python:3.11-slim # Prevent Python from writing .pyc files and buffering stdout/stderr ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 WORKDIR /app # System deps (optional; keep minimal) RUN apt-get update && \ apt-get install -y --no-install-recommends build-essential && \ rm -rf /var/lib/apt/lists/* # Install Python dependencies COPY requirements.txt . RUN pip install --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # Copy application code COPY . . # Create non-root user RUN useradd -m appuser USER appuser EXPOSE 8000 # Start Gunicorn CMD ["gunicorn", "-c", "gunicorn.conf.py", "wsgi:app"]
.dockerignore__pycache__/ *.pyc *.pyo *.pyd *.log .env .venv/ venv/ .git/ .gitignore Dockerfile docker-compose.yml
Build and run
From the project root:
# Build image docker build -t flask-gunicorn-app . # Run container docker run -p 8000:8000 flask-gunicorn-appThen open:
http://localhost:8000/andhttp://localhost:8000/health.If you want a version with
docker-compose.ymlas well, say so and I’ll add it.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.