Skip to content

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

Notifications You must be signed in to change notification settings

CharismaIsland/Flask-Postgres-with-Docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

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

No packages published