This repository consists of a Python project that uses Docker Compose to run a PostgreSQL database and a Flask API. The API is used to interact with the database and perform CRUD operations.
- A version of Docker installed
This project uses environment variables to configure the database connection. To set up the environment variables, create a .env
file in the root directory of the project with the following content:
## .env
## web container
DBNAME=your_db_name
USER=your_db_user
PASSWORD=your_db_password
HOST=host.docker.internal # as the database is running in a container, the host is the host machine
PORT=5432 # default port for PostgreSQL
## db container
POSTGRES_USER=your_db_user
POSTGRES_PASSWORD=your_db_password
POSTGRES_DB=your_db_name
In db/init.sql
, you can find the SQL script that creates a table in the database. You can modify this script to create your own tables. This script is executed when the database container is started.
To start the containers, run the following command in the root directory of the project:
docker-compose --env-file .env up --build -d
To stop the containers, run the following command:
docker-compose stop
In case you want to stop and remove the containers, run the following command:
docker-compose down -v
The API is available at http://localhost:5000
. You can use a tool like Postman to interact with the API.
GET /
: "Hello, Docker Compose with PostgreSQL!"GET /data
: List of results from a table in the database