Creating a RESTful API with Python, Flask, Postgres, Docker, and Docker Compose

Posted by

Build a CRUD Rest API in Python using Flask, Postgres, Docker and Docker compose

Build a CRUD Rest API in Python using Flask, Postgres, Docker and Docker compose

In this article, we will guide you through the process of building a CRUD (Create, Read, Update, Delete) Rest API using Python, Flask, Postgres, Docker, and Docker compose. This will allow you to create a scalable and efficient API for your applications.

Prerequisites

Before we start, you need to have the following installed on your system:

  • Python
  • Flask
  • Postgres
  • Docker
  • Docker compose

Step 1: Set up the environment

First, create a new directory for your project and navigate into it. Then, create a new file called app.py for the Flask application code, and a requirements.txt file for the Python dependencies.

Step 2: Install Flask and Postgres

Next, install Flask and Postgres using pip, and create a new virtual environment for your project. You can do this by running the following commands:


$ pip install flask
$ pip install psycopg2-binary
$ pip freeze > requirements.txt

Step 3: Set up the database

Create a new file called db.py and add the code to connect to the Postgres database using the psycopg2 library. You will also need to create a new database and table for your API data. Don’t forget to populate the table with some sample data using SQL queries.

Step 4: Create the API endpoints

In the app.py file, write the Python code for the CRUD operations using Flask. This will involve defining routes for each operation (Create, Read, Update, Delete) and writing the corresponding functions to handle the requests.

Step 5: Dockerize the application

Create a new file called Dockerfile and add the necessary commands to build a Docker image for your Flask application. Then, create a new file called docker-compose.yml to define the services for your application, including the Flask app, the Postgres database, and any other dependencies.

Step 6: Run the application

Finally, you can run your API using Docker compose. Simply run the following command in the terminal:


$ docker-compose up

Conclusion

With these steps, you have successfully built a CRUD Rest API in Python using Flask, Postgres, Docker, and Docker compose. This will allow you to create a scalable and efficient API for your applications. Happy coding!

0 0 votes
Article Rating
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@julesrubin8162
6 months ago

You really helped me for my DevOps class final project thank you !

@rojansapkotaofficial
6 months ago

Great One!

@Useroftherisingsun
6 months ago

Thank you sir, you saved me hours!!

@GauravSingh-ui3jr
6 months ago

API's all the endpoints are working fine, After containerizing it using docker the Database is created as per Table Plus. Test endpoint of the application is working fine. But I am getting error that no table exists. I tried multiple solutions from internet and chatgpt. Please help

@mrjmrezende
6 months ago

When I run "docker compose up –build flask_app" I received the message:
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed

the current application. To solve this, set up an application context

with app.app_context(). See the documentation for more information.

Any suggestions to resolve this? Sorry, I'm beginner

@ibiawosanya9233
6 months ago

Excellent tutorial, was a total beginner and now I created the Python Flask web application and Dockerized in less than 30 mins.

@susmitadey6735
6 months ago

I'm unable to create users. Help me.

@zaki9815
6 months ago

My friend, you can use command "docker compose logs -f" for debugging

@TheIaTaI
6 months ago

Is code suggestions being suggested by an extension?
Is there a similiar co-pilot in pycharm?

@francescociulla
6 months ago

Find Francesco: https://francescociulla.com

0:00 Intro. Architecture and project setup. Dependencies in requirements.txt

2:40 app file: Flask app, SQLAlchemy app, Model, test route

6:15: routes and CRUD controllers: Create, Read, Update, Delete

12:04 Dockerize the Flask application. Dockerfile

14:08 docker-compose.yml: flask app and Postgres services

18:00 Test Postgres container. test with Tableplus

19:45 Build the image (debug) and run the app service. Test endpoints

23:53 Bug fixing. Final test