Skip to content

Commit cec5ef4

Browse files
authored
Merge pull request #77 from call-0f-code/fuck/local-dev
refactored local dev setup (made it easy)
2 parents 3bf578c + ae9b53a commit cec5ef4

6 files changed

Lines changed: 77 additions & 1166 deletions

File tree

‎.dockerignore‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Dependencies
22
node_modules
3-
bun.lockb
3+
bun.lock
44

55
# Build outputs
66
dist
@@ -9,8 +9,7 @@ build
99

1010
# Environment files
1111
.env
12-
.env.local
13-
.env.*.local
12+
.env.*
1413

1514
# Logs
1615
logs

‎Dockerfile‎

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,58 @@
1-
FROM oven/bun:1.3 AS base
2-
WORKDIR /var/www/api
1+
# ─── Stage 1: Dependencies ────────────────────────────────────────────────────
2+
FROM oven/bun:1-alpine AS deps
33

4-
# -----------------------------
5-
# deps stage - cache dependencies
6-
# -----------------------------
7-
FROM base AS deps
4+
WORKDIR /app
85

6+
# Copy package manifests
97
COPY package.json bun.lock* ./
10-
COPY prisma ./prisma
8+
9+
# Install production dependencies only
1110
RUN bun install --frozen-lockfile
12-
RUN bunx prisma generate
11+
# ─── Stage 2: Builder ─────────────────────────────────────────────────────────
12+
FROM oven/bun:1-alpine AS builder
1313

14-
# -----------------------------
15-
# build stage - compile TypeScript to JavaScript
16-
# -----------------------------
17-
FROM deps AS build
14+
WORKDIR /app
1815

16+
# Copy all source files
1917
COPY . .
20-
RUN bun build src/server.ts --target=bun --production --outdir dist
2118

22-
# -----------------------------
23-
# development stage
24-
# -----------------------------
25-
FROM deps AS development
26-
COPY . .
27-
EXPOSE 3000
28-
CMD ["bun", "src/server.ts"]
29-
30-
# -----------------------------
31-
# production stage
32-
# -----------------------------
19+
# Install all deps (including dev) for Prisma client generation
20+
RUN bun install --frozen-lockfile
3321

34-
35-
FROM oven/bun:1-slim AS production
22+
# prisma generate doesn't connect to the DB, but prisma.config.ts uses env("DATABASE_URL")
23+
# which throws at config-load time if the var is absent. Satisfy it with a dummy placeholder.
24+
# The real DATABASE_URL is injected at runtime via env_file / --env-file / -e flags.
25+
ARG DATABASE_URL=postgresql://placeholder:placeholder@localhost:5432/placeholder
26+
ENV DATABASE_URL=${DATABASE_URL}
3627

37-
WORKDIR /var/www/api
38-
RUN groupadd -g 1001 nodejs && useradd -u 1001 -g nodejs -m bunjs
28+
# Generate Prisma client
29+
RUN bunx prisma generate
3930

40-
COPY --from=build --chown=bunjs:nodejs /var/www/api/dist ./dist
41-
COPY --from=deps --chown=bunjs:nodejs /var/www/api/src/generated ./dist/generated
31+
# Unset the dummy URL so it doesn't leak into downstream stages via ENV
32+
ENV DATABASE_URL=
4233

34+
# ─── Stage 3: Production runner
35+
FROM oven/bun:1-alpine AS runner
4336

44-
RUN chown -R bunjs:nodejs /var/www/api
45-
USER bunjs
37+
WORKDIR /app
4638

4739
ENV NODE_ENV=production
48-
ENV PORT=3000
4940

50-
EXPOSE 3000
41+
# Create a non-root user for security
42+
RUN addgroup --system --gid 1001 cocgroup && \
43+
adduser --system --uid 1001 cocuser
44+
45+
# Copy production node_modules from deps stage
46+
COPY --from=deps --chown=cocuser:cocgroup /app/node_modules ./node_modules
47+
48+
# Copy application source
49+
COPY --chown=cocuser:cocgroup . .
5150

52-
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
53-
CMD wget -qO- http://localhost:3000/health || exit 1
51+
COPY --from=builder --chown=cocuser:cocgroup /app/src/generated/prisma ./src/generated/prisma
52+
53+
USER cocuser
54+
55+
EXPOSE 3000
5456

55-
CMD ["bun", "./dist/server.js"]
57+
# Run Prisma migrations then start the server
58+
CMD ["sh", "-c", "bunx prisma migrate deploy && bun src/server.ts"]

‎LOCAL_DEVELOPMENT.md‎

Lines changed: 0 additions & 54 deletions
This file was deleted.

‎docker-compose.yml‎

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,37 @@
1-
services:
2-
api:
3-
image: coc-api
4-
build:
5-
context: .
6-
dockerfile: Dockerfile
7-
target: development
8-
ports:
9-
- "127.0.0.1:3000:3000"
10-
env_file:
11-
- .env
12-
environment:
13-
NODE_ENV: development
14-
PORT: 3000
15-
DATABASE_URL: "postgresql://postgres:example@db:5432/coc?sslmode=disable"
16-
volumes:
17-
- ./:/app:cached
18-
- /app/node_modules
19-
depends_on:
20-
- db
21-
healthcheck:
22-
test: ["CMD", "curl", "-sf", "http://localhost:3000/health"]
23-
interval: 30s
24-
timeout: 3s
25-
retries: 3
1+
name: coc-api
262

27-
db:
28-
image: postgres:17
29-
environment:
30-
POSTGRES_USER: postgres
31-
POSTGRES_PASSWORD: example
32-
POSTGRES_DB: coc
33-
volumes:
34-
- pgdata:/var/lib/postgresql/data
35-
ports:
36-
- "127.0.0.1:5432:5432"
37-
healthcheck:
38-
test: ["CMD", "pg_isready", "-U", "postgres", "-d", "coc", "-h", "127.0.0.1", "-p", "5432"]
39-
interval: 5s
40-
timeout: 5s
41-
retries: 5
3+
services:
4+
api:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
target: builder # Use builder stage in dev (has devDeps + bun watch)
9+
container_name: coc-api-dev
10+
restart: unless-stopped
11+
ports:
12+
- "${PORT:-3000}:3000"
13+
env_file:
14+
- .env.local # Local dev env vars (Supabase connection strings, etc.)
15+
environment:
16+
NODE_ENV: development
17+
volumes:
18+
# Mount source for live-reload (everything except node_modules)
19+
- .:/app
20+
- /app/node_modules
21+
- /app/node_modules/.prisma
22+
command: >
23+
sh -c "bunx prisma generate &&
24+
bunx prisma migrate deploy &&
25+
bun --watch src/server.ts"
26+
healthcheck:
27+
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:3000/health"]
28+
interval: 30s
29+
timeout: 10s
30+
retries: 3
31+
start_period: 15s
32+
networks:
33+
- coc-network
4234

43-
volumes:
44-
pgdata:
45-
seed-data:
35+
networks:
36+
coc-network:
37+
driver: bridge

0 commit comments

Comments
 (0)