Next.js and Flask Docker Application
This project demonstrates a web application built with Next.js for the frontend, Flask for the backend, and Docker for containerization.
The Flask backend uses SQLAlchemy for database interactions and connects to a PostgreSQL database.
- Project Structure
- Setup and Installation
- Running the Application
- API Endpoints
- Environment Variables
- Docker Compose Configuration
- Volume Configuration
- Screenshot of Application
.
├── backend
│ ├── app.py
│ ├── Dockerfile
│ └── requirements.txt
├── frontend
│ ├── pages
│ ├── public
│ ├── styles
│ ├── Dockerfile
│ └── package.json
├── docker-compose.yml
└── README.md
Clone the repository:
git clone https://github.com/yourusername/your-repo-name.git
cd your-repo-name
Navigate to the backend directory and install dependencies:
cd backend
pip install -r requirements.txt
Navigate to the frontend directory and install dependencies:
cd ../frontend
npm install
To run the application, you will use Docker Compose to build and start all services.
Build and start the services:
docker-compose up --build
Access the application:
Frontend: http://localhost:3000 Backend API: http://localhost:4000/api/flask/users
URL: /api/flask/users
Method: POST
Payload:
{
"name": "John Doe",
"email": "[email protected]"
}
URL: /api/flask/users
Method: GET
URL: /api/flask/users/<id>
Method: GET
URL: /api/flask/users/<id>
Method: PUT
{
"name": "John Doe Updated",
"email": "[email protected]"
}
URL: /api/flask/users/<id>
Method: DELETE
Frontend (Next.js)
NEXT_PUBLIC_API_URL: URL of the Flask API (e.g., http://localhost:4000)
Backend (Flask)
DATABASE_URL: Connection URL for the PostgreSQL database (e.g., postgresql://postgres:postgres@db:5432/postgres)
Database (PostgreSQL)
POSTGRES_USER: Username for PostgreSQL (e.g., postgres)
POSTGRES_PASSWORD: Password for PostgreSQL (e.g., postgres)
POSTGRES_DB: Database name (e.g., postgres)
Here’s the docker-compose.yml configuration:
version: '3'
services:
nextapp:
container_name: nextapp
image: nextapp:1.0.0
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- 3000:3000
environment:
- NEXT_PUBLIC_API_URL=http://localhost:4000
depends_on:
- flaskapp
flaskapp:
container_name: flaskapp
image: flaskapp:1.0.0
build:
context: ./backend
dockerfile: Dockerfile
ports:
- 4000:4000
environment:
- DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres
depends_on:
- db
db:
container_name: db
image: postgres:13
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata: {}
pgdata: Used to persist PostgreSQL data across container restarts.