-
Notifications
You must be signed in to change notification settings - Fork 0
A hands-on lab demonstrating how to run a **Flask web API** connected to a **Postgres database** using Docker and Docker Compose. This project shows how containers communicate over a user-defined Docker network, persist data with volumes, and expose a REST API.
License
CharismaIsland/Flask-Postgres-with-Docker
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Β | Β | |||
Β | Β | |||
Β | Β | |||
Β | Β | |||
Β | Β | |||
Β | Β | |||
Repository files navigation
# Flask + Postgres with Docker A hands-on lab demonstrating how to run a **Flask web API** connected to a **Postgres database** using Docker and Docker Compose. This project shows how containers communicate over a user-defined Docker network, persist data with volumes, and expose a REST API. --- ## π Features - Flask web API with endpoints: - `GET /` β health check - `GET /items` β list items from Postgres - `POST /items` β create an item - Postgres database with persistent storage - Private Docker network (`appnet`) for service-to-service communication - Docker Compose for one-command setup --- ## π Project Structure ``` flask-pg/ βββ app.py # Flask API βββ requirements.txt # Python dependencies βββ Dockerfile # App image definition βββ docker-compose.yml # Compose setup for app + DB ``` --- ## π οΈ Prerequisites - [Docker](https://docs.docker.com/get-docker/) installed - [Docker Compose](https://docs.docker.com/compose/) (v2 or higher) - `curl` installed (for testing) --- ## β‘ Quick Start (with Compose) 1. Clone this repo: ```bash git clone https://github.com/<your-username>/flask-pg.git cd flask-pg ``` 2. Build and start the stack: ```bash docker compose up -d --build ``` 3. Test the API: ```bash curl -s localhost:5001/ # Health check curl -s localhost:5001/items # List items (empty at first) curl -s -X POST localhost:5001/items -H "Content-Type: application/json" -d '{"name":"first item"}' curl -s localhost:5001/items # List again, now contains your item ``` 4. Stop the stack: ```bash docker compose down ``` --- ## π Manual Setup (without Compose) ### 1. Create a private Docker network ```bash docker network create appnet ``` ### 2. Run Postgres on the network ```bash docker run -d --name db --network appnet -e POSTGRES_USER=app -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=mydb -v pgdata:/var/lib/postgresql/data postgres:16-alpine ``` ### 3. Build the Flask app image ```bash docker build -t myflaskapp:latest . ``` ### 4. Run the Flask app on the same network ```bash docker run -d --name web --network appnet -p 5000:5000 -e DATABASE_URL=postgresql://app:secret@db:5432/mydb myflaskapp:latest ``` ### 5. Test the API ```bash curl -s localhost:5000/ curl -s localhost:5000/items curl -s -X POST localhost:5000/items -H "Content-Type: application/json" -d '{"name":"another item"}' ``` --- ## ποΈ Cleanup Remove all containers, volumes, and networks created during the lab: ```bash docker rm -f web db docker volume rm pgdata docker network rm appnet ``` Or, if youβre using Compose: ```bash docker compose down -v ``` ---
About
A hands-on lab demonstrating how to run a **Flask web API** connected to a **Postgres database** using Docker and Docker Compose. This project shows how containers communicate over a user-defined Docker network, persist data with volumes, and expose a REST API.
Resources
License
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published