Using Gunicorn & Docker for Flask APIs in Production

Posted by


Flask is a popular Python web framework that is commonly used to create web applications and APIs. When developing APIs with Flask, it is important to consider how to deploy and run them in a production environment. In this tutorial, we will discuss how to use Gunicorn and Docker to deploy Flask APIs in a production setting.

Gunicorn is a WSGI (Web Server Gateway Interface) server that is commonly used to serve Python web applications, including Flask applications. Gunicorn is known for its performance and stability, making it a popular choice for deploying Python applications in production. Docker, on the other hand, is a containerization platform that allows you to package and deploy applications in a consistent and reproducible way.

To deploy a Flask API using Gunicorn and Docker, follow these steps:

Step 1: Create a Flask API
Before we can deploy our Flask API, we need to create it. Start by creating a new Flask project and defining your API endpoints. Here is a simple example of a Flask API that returns a "Hello, World!" message:

from flask import Flask

app = Flask(__name__)

@app.route('/hello')
def hello():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run()

Save this code in a file named app.py in your project directory.

Step 2: Install Gunicorn
Next, we need to install Gunicorn in our project. You can do this using pip:

pip install gunicorn

Step 3: Run Gunicorn
To run our Flask API using Gunicorn, we need to create a WSGI entry point for our application. Create a file named wsgi.py in your project directory, and add the following code:

from app import app

if __name__ == "__main__":
    app.run()

Next, we can start Gunicorn by running the following command:

gunicorn -w 4 -b 0.0.0.0:5000 wsgi:app

This command starts Gunicorn with four worker processes and binds it to port 5000. Your Flask API should now be running using Gunicorn.

Step 4: Create a Dockerfile
To containerize our Flask API using Docker, we need to create a Dockerfile in our project directory. Add the following code to your Dockerfile:

FROM python:3.9

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "wsgi:app"]

This Dockerfile starts with a base Python image, installs our project dependencies, copies our project files into the container, and runs Gunicorn to start our Flask API.

Step 5: Build and Run the Docker Image
Finally, we can build and run our Docker image. Start by creating a requirements.txt file in your project directory, listing any dependencies your Flask API may have. Then, build your Docker image using the following command:

docker build -t my-flask-api .

This command builds a Docker image tagged as my-flask-api using the Dockerfile in your project directory. Once the image is built, you can run it using the following command:

docker run -p 5000:5000 my-flask-api

This command starts a Docker container running your Flask API, which is accessible at http://localhost:5000. You can now access your Flask API running in a Docker container.

In conclusion, using Gunicorn and Docker to deploy Flask APIs in production is a common and reliable approach. By following the steps outlined in this tutorial, you can package and deploy your Flask API in a production environment efficiently and consistently. Happy coding!

0 0 votes
Article Rating

Leave a Reply

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@mohamedeisa3700
16 hours ago

i wanna contact u for some help

@michaelvela2835
16 hours ago

Great video cleared up a lot of confusion I had about Flask and Gunicorn.

@AshishKumar-ft6wv
16 hours ago

can you drop the source code link

@313zain6
16 hours ago

Great info

4
0
Would love your thoughts, please comment.x
()
x