Skip to main content

STEMBlock.ai - Local Testing with Docker

This guide provides comprehensive instructions for setting up and testing the STEMBlock.ai application locally using Docker.

๐Ÿ“‹ Prerequisitesโ€‹

Before you begin, ensure you have the following installed on your system:

  • Docker Desktop (version 20.10 or later)
  • Git (for cloning repositories if needed)
  • At least 4GB of available RAM for all containers

Verify Installationโ€‹

# Check Docker version
docker --version
# Output: Docker version 20.10.x or later

# Check Docker Compose version
docker compose version
# Output: Docker Compose version v2.x.x or later

๐Ÿ—๏ธ Project Structureโ€‹

The STEMBlock.ai application consists of two main components:

~/Documents/GitHub/
โ”œโ”€โ”€ stemblockai-backend/ # NestJS backend API
โ”‚ โ”œโ”€โ”€ Dockerfile
โ”‚ โ”œโ”€โ”€ docker-compose.yml
โ”‚ โ””โ”€โ”€ ...
โ””โ”€โ”€ stemblockai-frontend/ # Next.js frontend
โ”œโ”€โ”€ Dockerfile
โ”œโ”€โ”€ docker-compose.yml
โ””โ”€โ”€ ...

Step 1: Start the Backendโ€‹

The backend includes both the NestJS API and PostgreSQL database.

# Navigate to backend directory
cd ~/Documents/GitHub/stemblockai-backend

# Start backend and database
docker compose up -d

# View logs (optional)
docker compose logs -f

What this does:

  • Creates a PostgreSQL database container
  • Builds and starts the NestJS backend
  • Runs database migrations automatically
  • Exposes backend on http://localhost:3001

Step 2: Verify Backend is Runningโ€‹

# Check if containers are healthy
docker compose ps

# Test the backend API
curl http://localhost:3001/health
# Should return: {"status":"ok"}

Step 3: Start the Frontendโ€‹

# Navigate to frontend directory
cd ~/Documents/GitHub/stemblockai-frontend

# Start frontend
docker compose up -d

# View logs (optional)
docker compose logs -f

What this does:

  • Builds and starts the Next.js frontend
  • Connects to the backend API
  • Exposes frontend on http://localhost:3000

Step 4: Access the Applicationโ€‹

Open your browser and navigate to:


๐Ÿ“Š Environment Variablesโ€‹

Backend Environment Variablesโ€‹

The backend uses these environment variables (set in docker-compose.yml):

DATABASE_URL: postgresql://stemblock:stemblock_dev_password@postgres:5432/stemblock_db
JWT_SECRET: your-super-secret-jwt-key-change-in-production
JWT_EXPIRES_IN: 7d
PORT: 3001

Optional variables (for additional features):

# For AI evaluation features
OPENAI_API_KEY=your-openai-api-key

# For Google OAuth login
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret

To add these, create a .env file in the backend directory:

cd ~/Documents/GitHub/stemblockai-backend
cat > .env << EOF
OPENAI_API_KEY=sk-your-key-here
GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_SECRET=your-client-secret
EOF

Frontend Environment Variablesโ€‹

The frontend requires the backend API URL:

NEXT_PUBLIC_API_URL: http://localhost:3001

To customize, create a .env.local file:

cd ~/Documents/GitHub/stemblockai-frontend
echo "NEXT_PUBLIC_API_URL=http://localhost:3001" > .env.local

๐Ÿ”„ Docker Commands Referenceโ€‹

Starting Servicesโ€‹

# Start in detached mode (recommended)
docker compose up -d

# Start with live logs
docker compose up

# Start only specific service
docker compose up -d backend

Viewing Logsโ€‹

# View all logs
docker compose logs

# Follow logs in real-time
docker compose logs -f

# View logs for specific service
docker compose logs backend
docker compose logs postgres

# View last 100 lines
docker compose logs --tail=100

Stopping Servicesโ€‹

# Stop all services (keeps data)
docker compose stop

# Stop and remove containers (keeps data volumes)
docker compose down

# Stop and remove everything including volumes (โš ๏ธ deletes database data)
docker compose down -v

Rebuilding Containersโ€‹

When you make code changes, rebuild the containers:

# Rebuild and restart
docker compose up -d --build

# Force rebuild without cache
docker compose build --no-cache
docker compose up -d

Checking Statusโ€‹

# View running containers
docker compose ps

# Check container health
docker ps --format "table {{.Names}}\t{{.Status}}"

# View resource usage
docker stats

๐Ÿ—„๏ธ Database Managementโ€‹

Accessing the Databaseโ€‹

# Connect to PostgreSQL container
docker exec -it stemblock-postgres psql -U stemblock -d stemblock_db

# Example queries
\dt # List all tables
SELECT * FROM "User"; # View users
\q # Quit

Running Migrationsโ€‹

Migrations run automatically on container startup. To run manually:

# Enter backend container
docker exec -it stemblock-backend sh

# Run migrations
npx prisma migrate deploy

# Generate Prisma client (if needed)
npx prisma generate

# Exit container
exit

Resetting the Databaseโ€‹

cd ~/Documents/GitHub/stemblockai-backend

# Stop and remove all data
docker compose down -v

# Start fresh
docker compose up -d

# The database will be recreated with migrations applied

Database Backupโ€‹

# Create backup
docker exec stemblock-postgres pg_dump -U stemblock stemblock_db > backup.sql

# Restore from backup
docker exec -i stemblock-postgres psql -U stemblock stemblock_db < backup.sql

๐Ÿงช Testing the Applicationโ€‹

1. Create Test Usersโ€‹

The application requires users with different roles. You can create them via the registration page or directly in the database:

Via Registration UI:

  1. Go to http://localhost:3000/register
  2. Create accounts with different roles:
    • Student account
    • Coach account
    • Parent account
    • Admin account

Via Database:

docker exec -it stemblock-postgres psql -U stemblock -d stemblock_db

-- View existing users
SELECT id, email, role, "firstName", "lastName" FROM "User";

2. Test Workflowsโ€‹

Student Workflow:

  1. Login as student: http://localhost:3000/login
  2. Navigate to student dashboard
  3. View and submit assignments

Coach Workflow:

  1. Login as coach
  2. Create a class
  3. Add students to the class
  4. Create assignments
  5. Review submissions

Parent Workflow:

  1. Login as parent
  2. View children's progress
  3. Download PDF reports (requires backend implementation)

Admin Workflow:

  1. Login as admin
  2. Manage users
  3. Manage classes
  4. Reassign coaches

3. API Testingโ€‹

Test backend endpoints directly:

# Register a new user
curl -X POST http://localhost:3001/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "password123",
"firstName": "Test",
"lastName": "User",
"role": "STUDENT"
}'

# Login
curl -X POST http://localhost:3001/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "password123"
}'

# Use the returned accessToken for authenticated requests
export TOKEN="your-access-token-here"

# Get classes
curl http://localhost:3001/classes \
-H "Authorization: Bearer $TOKEN"

๐Ÿ› Troubleshootingโ€‹

Container Won't Startโ€‹

Problem: Container exits immediately

Solution:

# Check logs for errors
docker compose logs backend

# Check if port is already in use
lsof -i :3001 # Backend port
lsof -i :3000 # Frontend port
lsof -i :5432 # Database port

# Kill process if needed
kill -9 <PID>

Database Connection Errorโ€‹

Problem: Backend can't connect to database

Solution:

# Ensure postgres is healthy
docker compose ps

# Check postgres logs
docker compose logs postgres

# Restart services in order
docker compose down
docker compose up -d postgres
# Wait 10 seconds
docker compose up -d backend

Frontend Can't Connect to Backendโ€‹

Problem: API calls fail with network errors

Solution:

  1. Verify backend is running: curl http://localhost:3001/health
  2. Check NEXT_PUBLIC_API_URL in frontend container:
    docker exec stemblock-frontend env | grep NEXT_PUBLIC_API_URL
  3. Rebuild frontend if environment variable changed:
    cd ~/Documents/GitHub/stemblockai-frontend
    docker compose up -d --build

Out of Disk Spaceโ€‹

Problem: "No space left on device"

Solution:

# Remove unused images and containers
docker system prune -a

# Remove unused volumes (โš ๏ธ deletes data)
docker volume prune

Permission Issuesโ€‹

Problem: Permission denied errors

Solution:

# Fix file permissions
cd ~/Documents/GitHub/stemblockai-backend
sudo chown -R $USER:$USER .

cd ~/Documents/GitHub/stemblockai-frontend
sudo chown -R $USER:$USER .

๐Ÿ”ง Development Tipsโ€‹

Hot Reload (Optional)โ€‹

For development with hot reload, you can mount source code as volumes:

Backend (docker-compose.yml):

volumes:
- ./src:/app/src
- ./uploads:/app/uploads

Frontend: Already uses standalone mode - rebuild for changes.

Viewing Real-Time Logsโ€‹

# Multiple terminals approach
# Terminal 1: Backend logs
cd ~/Documents/GitHub/stemblockai-backend
docker compose logs -f

# Terminal 2: Frontend logs
cd ~/Documents/GitHub/stemblockai-frontend
docker compose logs -f

Accessing Container Shellโ€‹

# Backend
docker exec -it stemblock-backend sh

# Frontend
docker exec -it stemblock-frontend sh

# Database
docker exec -it stemblock-postgres sh

๐Ÿ“ Common Tasksโ€‹

Task: Add Test Dataโ€‹

# Connect to database
docker exec -it stemblock-postgres psql -U stemblock -d stemblock_db

# Create test users, classes, assignments
-- SQL commands here

Task: Reset Everythingโ€‹

# Stop and remove all containers and data
cd ~/Documents/GitHub/stemblockai-backend
docker compose down -v

cd ~/Documents/GitHub/stemblockai-frontend
docker compose down

# Remove network
docker network rm stemblock_network

# Start fresh
cd ~/Documents/GitHub/stemblockai-backend
docker compose up -d

cd ~/Documents/GitHub/stemblockai-frontend
docker compose up -d

Task: Update to Latest Codeโ€‹

# Pull latest changes
cd ~/Documents/GitHub/stemblockai-backend
git pull origin main

cd ~/Documents/GitHub/stemblockai-frontend
git pull origin main

# Rebuild and restart
cd ~/Documents/GitHub/stemblockai-backend
docker compose up -d --build

cd ~/Documents/GitHub/stemblockai-frontend
docker compose up -d --build

๐Ÿ“š Additional Resourcesโ€‹


๐Ÿ†˜ Getting Helpโ€‹

If you encounter issues:

  1. Check the logs: docker compose logs -f
  2. Verify all containers are healthy: docker compose ps
  3. Review this documentation
  4. Check the troubleshooting section
  5. Create an issue on GitHub with:
    • Error messages from logs
    • Steps to reproduce
    • Docker version
    • Operating system

โœ… Success Checklistโ€‹

Your local environment is ready when:

  • Backend container is running and healthy
  • Frontend container is running and healthy
  • Database container is running and healthy
  • http://localhost:3000 shows the login page
  • http://localhost:3001/health returns \{"status":"ok"\}
  • You can register and login as a user
  • Database migrations have been applied

Happy Testing! ๐Ÿš€