This commit is contained in:
Newzy Developer committed 2026-10-30 10:54:23 +00:00
1 parent 6914a6855c
commit 0ffbba3bed
1 file changed
+372
@@ -0,0 +1,372 @@
# M4: Setup Developer Environment
## Prompt
Set up a complete developer environment for **newzy** news web application with Docker Compose orchestration. Follow these steps:
1. Create `docker-compose.yml` at project root (`newzy/docker-compose.yml`) with all services:
```yaml
version: '3.8'
services:
backend:
build: ./backend
ports:
- "8000:8000"
volumes:
- ./data/db:/app/data/db
environment:
- DATABASE_URL=sqlite:///./data/db/newzy.db
- DEBUG=false
frontend:
build: ./frontend
ports:
- "3000:80"
depends_on:
- backend
networks:
newzy-network:
driver: bridge
volumes:
db-data:
```
2. Create `.env.example` file documenting all required environment variables:
- DATABASE_URL (SQLite path)
- DEBUG (boolean, optional)
- BACKEND_CORS_ORIGINS (comma-separated, default: http://localhost:3000)
- FRONTEND_API_URL (relative /api or absolute)
3. Create `.env` file with default development values:
```bash
DATABASE_URL=sqlite:///./data/db/newzy.db
DEBUG=false
BACKEND_CORS_ORIGINS=http://localhost:3000,http://localhost:5173
FRONTEND_API_URL=/api
```
4. Update `backend/.gitignore` to exclude `.env` and production data:
```python
.env
.env.*
data/
```
5. Create development scripts in project root using Makefile or shell script:
- `dev` - Start both services in background
- `stop` - Stop all services
- `restart` - Restart all services
- `backend-start` - Start backend only
- `frontend-start` - Start frontend only
- `test` - Run all tests (unit + E2E)
- `clean` - Remove containers and volumes
6. Add `Makefile` for command shortcuts:
```makefile
.PHONY: dev stop restart backend-start frontend-start test clean
dev:
docker compose up -d --build
stop:
docker compose down
restart:
docker compose down
docker compose up -d --build
backend-start:
docker compose up -d backend
frontend-start:
docker compose up -d frontend
test:
npm run test # frontend
cd backend && pytest # backend
clean:
docker compose down -v
rm -rf data/
docker image prune -f
```
7. Create `backend/Dockerfile` with development optimizations:
- Copy only required files
- Install dependencies efficiently
- Use slim Python base image
- Expose port 8000
8. Update `frontend/Dockerfile` to use production Nginx:
- Multi-stage build (build stage + nginx serve stage)
- Nginx configuration for SPA routing
- Production-optimized static file serving
9. Create `.gitignore` at project root excluding:
```
# Dependencies
node_modules/
*.egg-info/
# Environment files
.env
.env.local
.env.*.local
# IDE settings
.idea/
.vscode/
*.swp
*.swo
# Docker artifacts (optional)
.dockerignore
docker-compose.override.yml
```
10. Create `data/.gitignore` to exclude SQLite database from version control:
```
*
!keep_files.txt
```
11. Document setup process in `README.md`:
- Prerequisites (Docker, Node.js 18+)
- Quick start commands
- Service descriptions
- Port mappings
- Network configuration
- Troubleshooting guide
12. Create `.dockerignore` files for both backend and frontend to exclude unnecessary files during builds:
```
.git/
.env
__pycache__/
*.pyc
node_modules/
dist/
.pytest_cache/
```
Remember: Focus ONLY on developer environment setup. No application code changes required.
## Description
This milestone sets up a complete Docker Compose development environment for the newzy news web application. The scope includes orchestrating both backend and frontend services, environment variable management, and streamlined development commands—no application logic changes or business feature implementation.
**Purpose**: Provide developers with a consistent, reproducible local development environment using Docker that matches the production setup as closely as possible.
**Target Audience**: Developers joining the project who need quick access to running services without manual installation of Python, Node.js, databases, or Nginx.
**Expected Outcomes**: A working docker-compose.yml at project root that can start all services with a single command, proper environment configuration, and development workflow automation via Makefile.
## Acceptance Criteria
- [ ] `docker-compose.yml` at project root configures both backend and frontend services
- [ ] Backend service builds from `./backend` directory
- [ ] Frontend service builds from `./frontend` directory
- [ ] Port mappings configured: 3000 for frontend, 8000 for backend
- [ ] Docker network defined for inter-service communication
- [ ] Volume mounted for persistent SQLite database at `/data/db`
- [ ] `.env.example` file documents all environment variables
- [ ] `.env` file provides default development configuration
- [ ] `backend/Dockerfile` optimized for production use
- [ ] `frontend/Dockerfile` uses multi-stage build with Nginx
- [ ] Makefile or shell scripts provide dev, stop, restart commands
- [ ] `data/.gitignore` excludes database from version control
- [ ] `.dockerignore` files exist for both frontend and backend
- [ ] Project root `.gitignore` excludes env files and build artifacts
- [ ] README.md documents docker setup with quick start guide
## Technical Details
### docker-compose.yml (Project Root)
```yaml
version: '3.8'
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile
ports:
- "8000:8000"
volumes:
- ./data/db:/app/data/db
environment:
- DATABASE_URL=sqlite:///./data/db/newzy.db
- DEBUG=false
- BACKEND_CORS_ORIGINS=http://localhost:3000,http://localhost:5173
depends_on:
backend-db:
condition: service_healthy
networks:
- newzy-network
restart: unless-stopped
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "3000:80"
depends_on:
- backend
networks:
- newzy-network
restart: unless-stopped
networks:
newzy-network:
driver: bridge
ipam:
config:
- subnet: 172.28.0.0/16
volumes:
db-data:
name: newzy-db-data
```
### Environment Variables (.env.example)
| Variable | Description | Default | Required |
|----------|-------------|---------|----------|
| DATABASE_URL | SQLite database path | sqlite:///./data/db/newzy.db | Yes |
| DEBUG | Debug mode flag | false | No |
| BACKEND_CORS_ORIGINS | Allowed frontend origins | http://localhost:3000 | No |
| FRONTEND_API_URL | Backend API base URL | /api | No |
### Development Commands (Makefile)
```makefile
.PHONY: dev stop restart test clean build
dev:
docker compose up -d --build backend frontend
stop:
docker compose down
restart:
docker compose down
docker compose up -d --build backend frontend
test:
@echo "Running frontend tests..."
npm run test
@echo "Running backend tests..."
docker exec frontend npm run test || \
docker exec backend pytest
clean:
docker compose down -v
rm -rf data/
docker images | grep -E "(newzy|python|node)" | awk '{print $$3}' | xargs docker rmi
build:
docker compose build --no-cache
logs:
docker compose logs -f backend frontend
shell-backend:
docker exec -it backend bash
shell-frontend:
docker exec -it frontend sh
```
### Directory Structure with Docker Setup
```
newzy/
├── data/ # Local SQLite data directory
│ └── db/
│ └── newzy.db # Persisted database (excluded from git)
├── backend/
│ ├── Dockerfile
│ ├── requirements.txt
│ ├── .gitignore
│ └── pytest.ini
├── frontend/
│ ├── Dockerfile
│ ├── docker-compose.yml # Frontend-specific override (optional)
│ └── package.json
├── docker-compose.yml # Main orchestration file
├── .env # Development environment variables
├── .env.example # Template for .env
├── Makefile # Development shortcuts
├── .dockerignore # Exclude files from build context
└── data/.gitignore # Exclude database from git
```
### Multi-stage Build Strategy
**Backend Dockerfile:**
- Base: `python:3.11-slim`
- Install dependencies with `pip install --no-cache-dir`
- Copy only production code and requirements
- Expose port 8000
**Frontend Dockerfile:**
- Stage 1: `node:20-alpine` → Build frontend with Vite
- Stage 2: `nginx:alpine` → Serve built static files
- Nginx config routes all requests to index.html for SPA
### Service Communication Flow
```
Browser (localhost:3000)
↓ HTTP Request
Frontend Container (newzy-network:80)
↓ Forwarded to /api
Backend Container (newzy-network:8000)
↓ Connects to SQLite
Volume: data/db/newzy.db
```
## QA Notes
### Testing Checklist
- [ ] `docker compose up` starts both services without errors
- [ ] Frontend accessible at http://localhost:3000
- [ ] Backend accessible at http://localhost:8000
- [ ] Health endpoints respond correctly
- [ ] Database persists across service restarts
- [ ] `.env.example` contains all documented variables
- [ ] Makefile commands work (dev, stop, restart, clean)
- [ ] `data/` directory excluded from git commits
- [ ] `.gitignore` excludes environment files
### Security Considerations
- Never commit `.env` file to version control
- Use `.env.example` as template for new developers
- SQLite database stored locally, not in container (volume mounted)
- CORS configured with specific origins
- No production secrets exposed by default
### Performance Notes
- `--build` flag only when needed (avoid rebuilds on dev)
- Docker cache leveraged for incremental builds
- SQLite file storage faster than network databases for development
- Nginx serves static files efficiently in production
### Known Limitations
- Requires Docker Desktop or equivalent installed locally
- Mac users need sufficient disk space (~50GB recommended)
- Windows WSL2 required for best performance
- No GPU acceleration (CPU-based only)
- Production databases should use PostgreSQL, not SQLite
### Success Metrics
- `docker compose up` completes in under 5 minutes on first run
- Services restart within 10 seconds subsequent runs
- Development environment identical to staging/production setup
- Zero manual configuration required for new developers
- Makefile shortcuts reduce repetitive Docker commands