This README file provides instructions on how to set up a Flask application with a PostgreSQL database using Docker containers. This setup includes creating a Docker network for communication between containers, a Docker volume for persisting PostgreSQL data, cloning the project from GitHub, and creating a virtual environment (venv) using a requirements file.
- Docker installed on your machine.
- Git installed on your machine.
- Python installed on your machine.
- Clone the Project from GitHub:
git clone https://github.com/haimgoldfisher/flask-docker-postgres.git
- Create a Virtual Environment and Install Dependencies:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
- Create Docker Network and Volume:
docker network create flask-gres-net
docker volume create postgres-vol
- Run PostgreSQL Container:
docker run -e POSTGRES_DB=postgresdb -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin --name my-postgres -v postgres-vol:/var/lib/postgresql/ --network flask-gres-net -p 5432:5432 postgres
- Enter PostgreSQL Container and Create Table:
docker exec -it my-postgres /bin/bash
psql -U admin -d postgresdb
- Inside the PostgreSQL container:
CREATE TABLE users(
id SERIAL PRIMARY KEY,
name VARCHAR(50)
);
- Build Flask App Image and Run Container:
docker build -t my-app:1.1 .
docker run -p 5555:5555 --network flask-gres-net my-app:1.1
Send some requests to check the app functionality and communication with PostgreSQL.
- Add a user:
curl -i -X POST -H 'Content-Type: application/json' -d '{"name": "haimon"}' http://127.0.0.1:5555/users
You can also add a user using 'Postman' App with the same params.
- Get all users:
curl -i -X GET http://127.0.0.1:5555/users