Skip to content

Commit c9a7629

Browse files
authored
Merge pull request #214 from Chigybillionz/Add-Docker-Compose
[DevOps] Add Docker Compose for local development
2 parents 89c5335 + 3a4e366 commit c9a7629

7 files changed

Lines changed: 230 additions & 0 deletions

File tree

DEVELOPMENT.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Local Development Guide
2+
3+
This guide explains how to set up the StarkEd project for local development using Docker Compose.
4+
5+
## Prerequisites
6+
7+
- [Docker](https://docs.docker.com/get-docker/)
8+
- [Docker Compose](https://docs.docker.com/compose/install/)
9+
10+
## Services overview
11+
12+
The `docker-compose.yml` file sets up the following services:
13+
- **backend**: Node.js backend API (Port: 5000)
14+
- **frontend**: Next.js frontend (Port: 3000)
15+
- **postgres**: PostgreSQL database (Port: 5432)
16+
- **redis**: Redis cache (Port: 6379)
17+
- **ipfs**: IPFS node for decentralized storage (Ports: 4001, 5001, 8080)
18+
19+
## Getting Started
20+
21+
1. **Start all services**:
22+
```bash
23+
docker compose up -d --build
24+
```
25+
26+
2. **Seed the database**:
27+
Once the containers are running and healthy, run the migrations and seed the database with initial development data:
28+
```bash
29+
bash scripts/seed-dev.sh
30+
```
31+
32+
## Hot Reloading
33+
34+
Both the backend and frontend are configured with volume mounts. Changes you make to the source code on your host machine will be immediately reflected inside the running containers, triggering a hot reload for Next.js (frontend) and nodemon (backend).
35+
36+
## Health Checks
37+
38+
All containers are equipped with health checks. You can check the status of your services by running:
39+
```bash
40+
docker compose ps
41+
```
42+
43+
## Clean Environment
44+
45+
If you need to wipe all data (database, redis, ipfs) and start fresh, run:
46+
```bash
47+
docker compose down --volumes --remove-orphans
48+
```

backend/Dockerfile.dev

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM node:20-alpine
2+
3+
WORKDIR /app
4+
5+
COPY package*.json ./
6+
RUN npm install
7+
8+
CMD ["npm", "run", "dev"]

backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"migrate:up": "ts-node src/utils/migrate.ts up",
1414
"migrate:down": "ts-node src/utils/migrate.ts down",
1515
"migrate:status": "ts-node src/utils/migrate.ts status",
16+
"seed": "ts-node src/utils/seed.ts",
1617
"test": "jest",
1718
"test:watch": "jest --watch",
1819
"test:coverage": "jest --coverage",

backend/src/utils/seed.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Pool } from 'pg';
2+
import dotenv from 'dotenv';
3+
4+
dotenv.config();
5+
6+
const pool = new Pool({
7+
host: process.env.DB_HOST || 'localhost',
8+
port: parseInt(process.env.DB_PORT || '5432', 10),
9+
user: process.env.DB_USER || 'postgres',
10+
password: process.env.DB_PASSWORD || 'postgres',
11+
database: process.env.DB_NAME || 'starked_dev'
12+
});
13+
14+
async function runSeed() {
15+
const client = await pool.connect();
16+
try {
17+
console.log('Seeding development data...');
18+
// Create users table if not exists
19+
await client.query(`
20+
CREATE TABLE IF NOT EXISTS users (
21+
id SERIAL PRIMARY KEY,
22+
email VARCHAR(255) UNIQUE NOT NULL,
23+
name VARCHAR(255) NOT NULL,
24+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
25+
)
26+
`);
27+
28+
// Insert dummy user
29+
await client.query(`
30+
INSERT INTO users (email, name)
31+
VALUES ('admin@starked.edu', 'Admin User')
32+
ON CONFLICT (email) DO NOTHING
33+
`);
34+
35+
console.log('Development data seeded successfully.');
36+
} catch (error) {
37+
console.error('Error seeding data:', error);
38+
} finally {
39+
client.release();
40+
await pool.end();
41+
}
42+
}
43+
44+
runSeed();

docker-compose.yml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
version: '3.8'
2+
3+
services:
4+
backend:
5+
build:
6+
context: ./backend
7+
dockerfile: Dockerfile.dev
8+
ports:
9+
- "5000:5000"
10+
volumes:
11+
- ./backend:/app
12+
- /app/node_modules
13+
environment:
14+
- NODE_ENV=development
15+
- PORT=5000
16+
- DB_HOST=postgres
17+
- DB_PORT=5432
18+
- DB_USER=postgres
19+
- DB_PASSWORD=postgres
20+
- DB_NAME=starked_dev
21+
- REDIS_URL=redis://redis:6379
22+
- IPFS_URL=http://ipfs:5001
23+
depends_on:
24+
postgres:
25+
condition: service_healthy
26+
redis:
27+
condition: service_healthy
28+
healthcheck:
29+
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:5000/api/health"]
30+
interval: 10s
31+
timeout: 5s
32+
retries: 5
33+
start_period: 30s
34+
35+
frontend:
36+
build:
37+
context: ./frontend
38+
dockerfile: Dockerfile.dev
39+
ports:
40+
- "3000:3000"
41+
volumes:
42+
- ./frontend:/app
43+
- /app/node_modules
44+
- /app/.next
45+
environment:
46+
- NODE_ENV=development
47+
- NEXT_PUBLIC_API_URL=http://localhost:5000/api
48+
depends_on:
49+
- backend
50+
healthcheck:
51+
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000"]
52+
interval: 10s
53+
timeout: 5s
54+
retries: 5
55+
start_period: 30s
56+
57+
postgres:
58+
image: postgres:15
59+
ports:
60+
- "5432:5432"
61+
environment:
62+
- POSTGRES_USER=postgres
63+
- POSTGRES_PASSWORD=postgres
64+
- POSTGRES_DB=starked_dev
65+
volumes:
66+
- postgres_data:/var/lib/postgresql/data
67+
healthcheck:
68+
test: ["CMD-SHELL", "pg_isready -U postgres -d starked_dev"]
69+
interval: 5s
70+
timeout: 5s
71+
retries: 5
72+
73+
redis:
74+
image: redis:7
75+
ports:
76+
- "6379:6379"
77+
volumes:
78+
- redis_data:/data
79+
healthcheck:
80+
test: ["CMD", "redis-cli", "ping"]
81+
interval: 5s
82+
timeout: 5s
83+
retries: 5
84+
85+
ipfs:
86+
image: ipfs/kubo:latest
87+
ports:
88+
- "4001:4001"
89+
- "5001:5001"
90+
- "8080:8080"
91+
volumes:
92+
- ipfs_data:/data/ipfs
93+
healthcheck:
94+
test: ["CMD", "ipfs", "diag", "cmds"]
95+
interval: 10s
96+
timeout: 5s
97+
retries: 5
98+
start_period: 20s
99+
100+
volumes:
101+
postgres_data:
102+
redis_data:
103+
ipfs_data:

frontend/Dockerfile.dev

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM node:20-alpine
2+
3+
WORKDIR /app
4+
5+
COPY package*.json ./
6+
RUN npm install
7+
8+
CMD ["npm", "run", "dev"]

scripts/seed-dev.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
set -e
3+
4+
echo "Starting development seed process..."
5+
6+
# Ensure backend container is running
7+
if ! docker compose ps | grep -q backend; then
8+
echo "Backend container is not running. Please run 'docker compose up -d' first."
9+
exit 1
10+
fi
11+
12+
echo "Running database migrations..."
13+
docker compose exec backend npm run migrate:up
14+
15+
echo "Running database seed..."
16+
docker compose exec backend npm run seed
17+
18+
echo "Database seeded successfully!"

0 commit comments

Comments
 (0)