Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

How to deploy a Flask application in Python with Gunicorn

August 17, 2023
Alex Soto Bueno
Related topics:
ContainersKubernetesPython
Related products:
Red Hat OpenShift

    Flask is a popular web framework for building web applications in Python. It is considered a microframework because it provides only the bare essentials for web development, focusing on simplicity and extensibility. 

    Flask is one of the most popular frameworks for developing REST applications in Python. Flask also has a server to do local deployments and test the application, but no one would go with it to production. So we need another server to deploy the application in production, and one option is to use a WSGI (Web Server Gateway Interface) server like Gunicorn.

    In this tutorial, you will learn how to create a Flask application, configure it to Gunicorn, and containerize it.

    The application

    Let's create a simple Hello World application using Flask as a web framework.

    You’ll need to install Python 3 and pip.

    Setup

    Create a new file with the name requirements.txt with the dependencies required for the project:

    Flask
    
    Gunicorn

    Then update the pip dependencies and install the dependencies by running the following command:

    pip3 install --upgrade pip
    
    pip3 install -r requirements.txt

    Application

    Create a new file named app.py containing the Flask REST endpoint:

    from flask import Flask
    
    app = Flask(__name__)
    
    
    
    @app.route('/')
    
    def hello_world():
    
        return 'Hello, World!'

    Running

    Let's test the application locally:

    flask --app app run

    The output will be similar to the following:

     * Serving Flask app 'app'
    
     * Debug mode: off
    
    WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
    
     * Running on http://127.0.0.1:5000
    
    Press CTRL+C to quit

    If you send a request to the defined endpoint, you'll get the predefined output:

    curl localhost:5000/
    
    Hello, World!

    So far, so good, but as the log message says, we need another way to use a WSGI server for going to production. So, let's do it.

    WSGI

    WSGI stands for Web Server Gateway Interface. It is a specification for a standardized interface between web servers and web applications or frameworks written in Python. WSGI defines a contract that allows web servers to communicate with Python web applications, enabling them to work together seamlessly.

    This separation of concerns allows web servers to focus on handling low-level networking tasks, such as accepting incoming requests and managing connections. In contrast, web applications can focus on handling the application logic and generating responses.

    You can switch between server implementations with minimal application changes. In this case, we'll use Gunicorn.

    First, we configure the Gunicorn server by creating a new file named gunicorn_config.py, where we'll configure things like the number of workers and threads and open port or timeout.

    import os
    
    
    
    workers = int(os.environ.get('GUNICORN_PROCESSES', '2'))
    
    threads = int(os.environ.get('GUNICORN_THREADS', '4'))
    
    # timeout = int(os.environ.get('GUNICORN_TIMEOUT', '120'))
    
    bind = os.environ.get('GUNICORN_BIND', '0.0.0.0:8080')
    
    
    
    forwarded_allow_ips = '*'
    
    secure_scheme_headers = { 'X-Forwarded-Proto': 'https' }

    Now, the last thing to do is start the Gunicorn server with the application deployed.

    Let's run the following command in the terminal:

    gunicorn --config gunicorn_config.py app:app

    The first app is the name of the Python file containing the Flask application.

    The output shows the application up and running in Gunicorn:

    [2023-07-17 22:55:05 +0200] [15903] [INFO] Starting gunicorn 20.1.0
    
    [2023-07-17 22:55:05 +0200] [15903] [INFO] Listening at: http://0.0.0.0:8080 (15903)
    
    [2023-07-17 22:55:05 +0200] [15903] [INFO] Using worker: gthread
    
    [2023-07-17 22:55:05 +0200] [15904] [INFO] Booting worker with pid: 15904
    
    [2023-07-17 22:55:05 +0200] [15905] [INFO] Booting worker with pid: 15905

    If you re-access the service using port 8080, you'll get the Hello World again.

    curl localhost:8080/
    
    Hello, World!

    Now, with the application up and running locally, it's time to containerize it.

    Containerization

    Generally speaking, to containerize an application, you create a Dockerfile with all the instructions and dependencies required to run the application.

    Dockerfile

    The base image contains Python, so we only copy the application files, install the requirements to run the application, and use Gunicorn to start it.

    FROM python:3.7.3-slim
    
    COPY requirements.txt /
    
    RUN pip3 install --upgrade pip
    
    RUN pip3 install -r /requirements.txt
    
    
    
    COPY . /app
    
    WORKDIR /app
    
    
    
    EXPOSE 8080
    
    
    
    CMD ["gunicorn","--config", "gunicorn_config.py", "app:app"]

    Building the container

    We are going to use Podman Desktop to build the container. With Podman (or Docker) running, let's build the container running the following command:

    podman build -t hello-flask:1.0.0 .

    The output shows the building steps executed:

    STEP 1/8: FROM python:3.7.3-slim
    
    STEP 2/8: COPY requirements.txt /
    
    --> 7721820f5f2e
    
    STEP 3/8: RUN pip3 install --upgrade pip
    
    …
    
    STEP 8/8: CMD ["gunicorn","--config", "gunicorn_config.py", "app:app"]
    
    COMMIT hello-flask:1.0.0
    
    --> 7bf50a7a7936
    
    Successfully tagged localhost/hello-flask:1.0.0
    
    7bf50a7a7936d9b87af74b7d49a94c69d217712901d78ba170acb1f28f800d51

    [ Learn more: What is Podman Desktop? A developer's introduction ]

    Running the container

    To run the container, use Podman (or Docker):

    podman run -it --rm -p 8080:8080 hello-flask:1.0.0
    [2023-07-18 07:35:29 +0000] [1] [INFO] Starting gunicorn 21.0.1
    
    [2023-07-18 07:35:29 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
    
    [2023-07-18 07:35:29 +0000] [1] [INFO] Using worker: gthread
    
    [2023-07-18 07:35:29 +0000] [4] [INFO] Booting worker with pid: 4
    
    [2023-07-18 07:35:29 +0000] [5] [INFO] Booting worker with pid: 5

    Kubernetes

    If you want to try the application in Kubernetes, use the following YAML file:

    apiVersion: apps/v1
    
    kind: Deployment
    
    metadata:
    
      labels:
    
        app: hello-service
    
      name: hello-service
    
    spec:
    
      replicas: 1
    
      selector:
    
        matchLabels:
    
          app: hello-service
    
      template:
    
        metadata:
    
          labels:
    
            app: hello-service
    
        spec:
    
          containers:
    
          - name: hello-service
    
            image: quay.io/lordofthejars/hello-flask:1.0.0 
    
            ports:
    
              - containerPort: 8080
    
    ---
    
    apiVersion: v1
    
    kind: Service
    
    metadata:
    
      name: hello-service
    
      labels:
    
        app: hello-service    
    
    spec:
    
      ports:
    
      - name: http
    
        port: 8080
    
      selector:
    
        app: hello-service
    
      type: LoadBalancer

    Use any Kubernetes cluster (minikube, kind, etc.) or a cloud Kubernetes implementation like OpenShift for free in the Developer Sandbox for Red Hat OpenShift.

    Conclusion

    Flask is one of the most popular frameworks in Python to develop web applications, but when it comes to moving to production, you need a server like Gunicorn to deploy it.

    Containerizing the application is a simple process of putting these steps into a Dockerfile and choosing the right base image.

    Last updated: September 19, 2023

    Related Posts

    • Deploy and test Kubernetes containers using Podman Desktop

    • Pipenv and S2I: A better way to manage Python dependencies in containers

    • micropipenv: Installing Python dependencies in containerized applications

    • How to create Python binding for a Rust library

    • Add custom windows to GDB: Programming the TUI in Python

    Recent Posts

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    What’s up next?

    Read the Red Hat Universal Base Images e-book to discover why choosing a base image is strategically important for building cloud-native apps.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.