Skip to content

Commit c3d581b

Browse files
author
Punitkumar756
committed
feat: Implement Docker support for InPactAI with multi-stage builds for backend and frontend
- Added Dockerfile for backend with multi-stage build and production optimizations. - Created Dockerfile for frontend with multi-stage build and nginx serving. - Introduced docker-compose files for development and production environments. - Added health checks and volume mounts for hot reloading during development. - Documented Docker architecture, implementation, and usage in new markdown files. - Included Makefile for simplified command execution. - Added validation scripts for environment configuration. - Updated nginx configuration for API proxying and gzip compression. - Created verification scripts for setup validation on Linux/Mac and Windows.
1 parent a3be437 commit c3d581b

25 files changed

Lines changed: 1869 additions & 184 deletions

.github/workflows/docker-build.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Docker Build and Test
2+
3+
on:
4+
push:
5+
branches: [ main, develop ]
6+
pull_request:
7+
branches: [ main, develop ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Docker Buildx
18+
uses: docker/setup-buildx-action@v3
19+
20+
- name: Build Backend
21+
run: |
22+
cd Backend
23+
docker build -t inpactai-backend:test .
24+
25+
- name: Build Frontend
26+
run: |
27+
cd Frontend
28+
docker build -t inpactai-frontend:test .
29+
30+
- name: Start services
31+
run: |
32+
docker compose up -d
33+
sleep 30
34+
35+
- name: Check backend health
36+
run: |
37+
curl -f http://localhost:8000/ || exit 1
38+
39+
- name: Check frontend health
40+
run: |
41+
curl -f http://localhost:5173/ || exit 1
42+
43+
- name: Show logs on failure
44+
if: failure()
45+
run: |
46+
docker compose logs
47+
48+
- name: Cleanup
49+
if: always()
50+
run: |
51+
docker compose down -v

Backend/.dockerignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
__pycache__
2+
*.pyc
3+
*.pyo
4+
*.pyd
5+
.Python
6+
*.so
7+
.env
8+
.venv
9+
env/
10+
venv/
11+
ENV/
12+
.git
13+
.gitignore
14+
.pytest_cache
15+
.coverage
16+
htmlcov/
17+
dist/
18+
build/
19+
*.egg-info/
20+
.DS_Store
21+
*.log

Backend/.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
user=postgres
2+
password=your_postgres_password
3+
host=your_postgres_host
4+
port=5432
5+
dbname=postgres
6+
GROQ_API_KEY=your_groq_api_key
7+
SUPABASE_URL=your_supabase_url
8+
SUPABASE_KEY=your_supabase_key
9+
GEMINI_API_KEY=your_gemini_api_key
10+
YOUTUBE_API_KEY=your_youtube_api_key
11+
REDIS_HOST=redis
12+
REDIS_PORT=6379

Backend/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM python:3.10-slim
2+
3+
WORKDIR /app
4+
5+
RUN apt-get update && apt-get install -y --no-install-recommends \
6+
gcc \
7+
libpq-dev \
8+
curl \
9+
&& rm -rf /var/lib/apt/lists/*
10+
11+
COPY requirements.txt .
12+
RUN pip install --no-cache-dir -r requirements.txt
13+
14+
COPY . .
15+
16+
EXPOSE 8000
17+
18+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]

Backend/Dockerfile.prod

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM python:3.10-slim AS builder
2+
3+
WORKDIR /app
4+
5+
RUN apt-get update && apt-get install -y --no-install-recommends \
6+
gcc \
7+
libpq-dev \
8+
&& rm -rf /var/lib/apt/lists/*
9+
10+
COPY requirements.txt .
11+
RUN pip install --no-cache-dir --user -r requirements.txt
12+
13+
FROM python:3.10-slim
14+
15+
WORKDIR /app
16+
17+
RUN apt-get update && apt-get install -y --no-install-recommends \
18+
libpq5 \
19+
&& rm -rf /var/lib/apt/lists/* \
20+
&& groupadd -r appuser && useradd -r -g appuser appuser
21+
22+
COPY --from=builder /root/.local /root/.local
23+
COPY . .
24+
25+
RUN chown -R appuser:appuser /app
26+
27+
USER appuser
28+
29+
ENV PATH=/root/.local/bin:$PATH
30+
31+
EXPOSE 8000
32+
33+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

Backend/app/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ async def lifespan(app: FastAPI):
4444
# Add CORS middleware
4545
app.add_middleware(
4646
CORSMiddleware,
47-
allow_origins=["http://localhost:5173"],
47+
allow_origins=[
48+
"http://localhost:5173",
49+
"http://frontend:5173",
50+
"http://127.0.0.1:5173"
51+
],
4852
allow_credentials=True,
4953
allow_methods=["*"],
5054
allow_headers=["*"],

DOCKER-ARCHITECTURE.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Docker Architecture Diagram
2+
3+
```
4+
┌─────────────────────────────────────────────────────────────────────┐
5+
│ Docker Host Machine │
6+
│ │
7+
│ ┌─────────────────────────────────────────────────────────────┐ │
8+
│ │ Docker Network: inpactai-network │ │
9+
│ │ │ │
10+
│ │ ┌──────────────────┐ ┌──────────────────┐ ┌────────┐│ │
11+
│ │ │ Frontend │ │ Backend │ │ Redis ││ │
12+
│ │ │ Container │ │ Container │ │ Container │
13+
│ │ │ │ │ │ │ ││ │
14+
│ │ │ Node 18-alpine │ │ Python 3.10-slim │ │ Redis 7││ │
15+
│ │ │ Vite Dev Server │◄───┤ FastAPI + uvicorn │ Alpine ││ │
16+
│ │ │ Port: 5173 │ │ Port: 8000 │◄───┤ Port: ││ │
17+
│ │ │ │ │ │ │ 6379 ││ │
18+
│ │ └──────────────────┘ └──────────────────┘ └────────┘│ │
19+
│ │ │ │ │ │ │
20+
│ │ │ Volume Mount │ Volume Mount │ │ │
21+
│ │ │ (Hot Reload) │ (Hot Reload) │ │ │
22+
│ │ ▼ ▼ ▼ │ │
23+
│ │ ┌──────────────┐ ┌─────────────┐ ┌──────────┐│ │
24+
│ │ │ ./Frontend │ │ ./Backend │ │redis_data││ │
25+
│ │ │ /app │ │ /app │ │ Volume ││ │
26+
│ │ └──────────────┘ └─────────────┘ └──────────┘│ │
27+
│ └─────────────────────────────────────────────────────────────┘ │
28+
│ │
29+
│ Port Mappings: │
30+
│ ┌─────────────┬──────────────┬────────────────────────────────┐ │
31+
│ │ Host:5173 │ ──────────► │ frontend:5173 (React + Vite) │ │
32+
│ │ Host:8000 │ ──────────► │ backend:8000 (FastAPI) │ │
33+
│ │ Host:6379 │ ──────────► │ redis:6379 (Cache) │ │
34+
│ └─────────────┴──────────────┴────────────────────────────────┘ │
35+
│ │
36+
│ Environment Files: │
37+
│ ┌────────────────────────────────────────────────────────────┐ │
38+
│ │ Backend/.env → Backend Container │ │
39+
│ │ Frontend/.env → Frontend Container │ │
40+
│ └────────────────────────────────────────────────────────────┘ │
41+
│ │
42+
└───────────────────────────────────────────────────────────────────────┘
43+
44+
User Browser
45+
46+
47+
http://localhost:5173 ──► Frontend Container ──► React UI
48+
49+
│ API Calls
50+
51+
http://backend:8000 ──► Backend Container ──► FastAPI
52+
53+
│ Cache/PubSub
54+
55+
redis:6379 ──► Redis Container
56+
57+
58+
Communication Flow:
59+
──────────────────────
60+
61+
1. User accesses http://localhost:5173
62+
└─► Docker routes to Frontend Container
63+
64+
2. Frontend makes API call to /api/*
65+
└─► Vite proxy forwards to http://backend:8000
66+
└─► Docker network resolves 'backend' to Backend Container
67+
68+
3. Backend connects to Redis
69+
└─► Uses REDIS_HOST=redis environment variable
70+
└─► Docker network resolves 'redis' to Redis Container
71+
72+
4. Backend connects to Supabase
73+
└─► Uses credentials from Backend/.env
74+
└─► External connection via internet
75+
76+
77+
Service Dependencies:
78+
─────────────────────
79+
80+
redis (no dependencies)
81+
82+
└─► backend (depends on redis)
83+
84+
└─► frontend (depends on backend)
85+
86+
87+
Health Checks:
88+
──────────────
89+
90+
Redis: redis-cli ping
91+
Backend: curl http://localhost:8000/
92+
Frontend: No health check (depends on backend health)
93+
94+
95+
Volume Mounts:
96+
──────────────
97+
98+
Development:
99+
./Backend:/app (Hot reload for Python)
100+
./Frontend:/app (Hot reload for Vite)
101+
/app/__pycache__ (Excluded)
102+
/app/node_modules (Excluded)
103+
104+
Production:
105+
redis_data:/data (Persistent Redis storage only)
106+
107+
108+
Build Process:
109+
──────────────
110+
111+
Development:
112+
1. Copy package files
113+
2. Install dependencies
114+
3. Copy source code
115+
4. Start dev server with hot reload
116+
117+
Production:
118+
Stage 1: Build
119+
1. Copy package files
120+
2. Install dependencies
121+
3. Copy source code
122+
4. Build optimized bundle
123+
124+
Stage 2: Serve
125+
1. Copy built artifacts
126+
2. Use minimal runtime (nginx for frontend)
127+
3. Serve optimized files
128+
129+
130+
Network Isolation:
131+
──────────────────
132+
133+
Internal Network (inpactai-network):
134+
- frontend ←→ backend (HTTP)
135+
- backend ←→ redis (TCP)
136+
137+
External Access:
138+
- Host machine → All containers (via port mapping)
139+
- Backend → Supabase (via internet)
140+
- Backend → External APIs (via internet)
141+
142+
143+
Security Model:
144+
───────────────
145+
146+
Development:
147+
- Root user in containers (for hot reload)
148+
- Source code mounted as volumes
149+
- Debug logging enabled
150+
151+
Production:
152+
- Non-root user in containers
153+
- No volume mounts (except data)
154+
- Production logging
155+
- Resource limits enforced
156+
- Optimized images
157+
```
158+
159+
## Quick Command Reference
160+
161+
```bash
162+
Start: docker compose up --build
163+
Stop: docker compose down
164+
Logs: docker compose logs -f
165+
Rebuild: docker compose up --build
166+
Clean: docker compose down -v
167+
```
168+
169+
## Service URLs
170+
171+
| Service | Internal | External |
172+
|---------|----------|----------|
173+
| Frontend | frontend:5173 | http://localhost:5173 |
174+
| Backend | backend:8000 | http://localhost:8000 |
175+
| Redis | redis:6379 | localhost:6379 |

0 commit comments

Comments
 (0)