dockerize properly

This commit is contained in:
Om Lanke
2025-09-25 14:25:29 +05:30
parent 618878940a
commit 1cf801e663
6 changed files with 56 additions and 32 deletions

1
.gitignore vendored
View File

@@ -9,6 +9,7 @@ node_modules
.pnp.js .pnp.js
# Local env files # Local env files
docker.env
.env .env
.env.local .env.local
.env.development.local .env.development.local

View File

@@ -15,21 +15,32 @@ COPY package.json pnpm-lock.yaml* pnpm-workspace.yaml* ./
COPY apps/admin/package.json ./apps/admin/ COPY apps/admin/package.json ./apps/admin/
COPY packages/db/package.json ./packages/db/ COPY packages/db/package.json ./packages/db/
COPY packages/ui/package.json ./packages/ui/ COPY packages/ui/package.json ./packages/ui/
COPY packages/eslint-config/package.json ./packages/eslint-config/
COPY packages/typescript-config/package.json ./packages/typescript-config/
# Install dependencies # Install dependencies (including devDependencies for build process)
RUN pnpm install --frozen-lockfile RUN pnpm install --frozen-lockfile
# Rebuild the source code only when needed # Rebuild the source code only when needed
FROM base AS builder FROM base AS builder
RUN apk update && apk add --no-cache libc6-compat
RUN corepack enable && corepack prepare pnpm@latest --activate
WORKDIR /app WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules COPY --from=deps /app/node_modules ./node_modules
COPY . . COPY . .
# Copy environment file for build process and create .env for dotenv-cli
COPY docker.env ./docker.env
COPY docker.env ./.env
# Set environment variables for build # Set environment variables for build
ENV NEXT_TELEMETRY_DISABLED 1 ENV NEXT_TELEMETRY_DISABLED 1
ENV NODE_ENV production ENV NODE_ENV production
# Build the application # Ensure devDependencies are available during build (needed for PostCSS plugins)
RUN pnpm install --frozen-lockfile
# Build the application with environment variables
RUN pnpm turbo run build --filter=admin... RUN pnpm turbo run build --filter=admin...
# Production image, copy all the files and run next # Production image, copy all the files and run next
@@ -53,9 +64,9 @@ RUN chown nextjs:nodejs .next
USER nextjs USER nextjs
EXPOSE 3001 EXPOSE 9001
ENV PORT 3001 ENV PORT 9001
ENV HOSTNAME "0.0.0.0" ENV HOSTNAME "0.0.0.0"
# server.js is created by next build from the standalone output # server.js is created by next build from the standalone output

View File

@@ -4,9 +4,9 @@
"type": "module", "type": "module",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "dotenv -e ../../.env -- next dev --turbopack -p 3001", "dev": "dotenv -e ../../.env -- next dev --turbopack -p 9001",
"build": "dotenv -e ../../.env -- next build", "build": "dotenv -e ../../.env -- next build",
"start": "dotenv -e ../../.env -- next start -p 3001", "start": "dotenv -e ../../.env -- next start -p 9001",
"lint": "next lint", "lint": "next lint",
"lint:fix": "next lint --fix", "lint:fix": "next lint --fix",
"typecheck": "tsc --noEmit" "typecheck": "tsc --noEmit"

View File

@@ -15,21 +15,32 @@ COPY package.json pnpm-lock.yaml* pnpm-workspace.yaml* ./
COPY apps/student/package.json ./apps/student/ COPY apps/student/package.json ./apps/student/
COPY packages/db/package.json ./packages/db/ COPY packages/db/package.json ./packages/db/
COPY packages/ui/package.json ./packages/ui/ COPY packages/ui/package.json ./packages/ui/
COPY packages/eslint-config/package.json ./packages/eslint-config/
COPY packages/typescript-config/package.json ./packages/typescript-config/
# Install dependencies # Install dependencies (including devDependencies for build process)
RUN pnpm install --frozen-lockfile RUN pnpm install --frozen-lockfile
# Rebuild the source code only when needed # Rebuild the source code only when needed
FROM base AS builder FROM base AS builder
RUN apk update && apk add --no-cache libc6-compat
RUN corepack enable && corepack prepare pnpm@latest --activate
WORKDIR /app WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules COPY --from=deps /app/node_modules ./node_modules
COPY . . COPY . .
# Copy environment file for build process and create .env for dotenv-cli
COPY docker.env ./docker.env
COPY docker.env ./.env
# Set environment variables for build # Set environment variables for build
ENV NEXT_TELEMETRY_DISABLED 1 ENV NEXT_TELEMETRY_DISABLED 1
ENV NODE_ENV production ENV NODE_ENV production
# Build the application # Ensure devDependencies are available during build (needed for PostCSS plugins)
RUN pnpm install --frozen-lockfile
# Build the application with environment variables
RUN pnpm turbo run build --filter=student... RUN pnpm turbo run build --filter=student...
# Production image, copy all the files and run next # Production image, copy all the files and run next
@@ -38,6 +49,7 @@ WORKDIR /app
ENV NODE_ENV production ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1 ENV NEXT_TELEMETRY_DISABLED 1
ENV ADMIN_DOMAIN "http://admin:9001"
RUN addgroup --system --gid 1001 nodejs RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs RUN adduser --system --uid 1001 nextjs
@@ -53,9 +65,9 @@ RUN chown nextjs:nodejs .next
USER nextjs USER nextjs
EXPOSE 3000 EXPOSE 9000
ENV PORT 3000 ENV PORT 9000
ENV HOSTNAME "0.0.0.0" ENV HOSTNAME "0.0.0.0"
# server.js is created by next build from the standalone output # server.js is created by next build from the standalone output

View File

@@ -4,9 +4,9 @@
"type": "module", "type": "module",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "dotenv -e ../../.env -- next dev --turbopack -p 3000", "dev": "dotenv -e ../../.env -- next dev --turbopack -p 9000",
"build": "dotenv -e ../../.env -- next build", "build": "dotenv -e ../../.env -- next build",
"start": "dotenv -e ../../.env -- next start -p 3000", "start": "dotenv -e ../../.env -- next start -p 9000",
"lint": "next lint", "lint": "next lint",
"lint:fix": "next lint --fix", "lint:fix": "next lint --fix",
"typecheck": "tsc --noEmit" "typecheck": "tsc --noEmit"

View File

@@ -7,21 +7,21 @@ services:
context: . context: .
dockerfile: apps/student/Dockerfile dockerfile: apps/student/Dockerfile
ports: ports:
- "3000:3000" - "9000:9000"
environment: environment:
- NODE_ENV=production - NODE_ENV=production
- PORT=3000 - PORT=9000
- HOSTNAME=0.0.0.0 - HOSTNAME=0.0.0.0
# Add your environment variables here # Add your environment variables here
# env_file: env_file:
# - .env - docker.env
restart: unless-stopped restart: unless-stopped
healthcheck: # healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/api/health"] # test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:9000/api/health"]
interval: 30s # interval: 30s
timeout: 10s # timeout: 10s
retries: 3 # retries: 3
start_period: 40s # start_period: 40s
# Admin application # Admin application
admin: admin:
@@ -29,21 +29,21 @@ services:
context: . context: .
dockerfile: apps/admin/Dockerfile dockerfile: apps/admin/Dockerfile
ports: ports:
- "3001:3001" - "9001:9001"
environment: environment:
- NODE_ENV=production - NODE_ENV=production
- PORT=3001 - PORT=9001
- HOSTNAME=0.0.0.0 - HOSTNAME=0.0.0.0
# Add your environment variables here # Add your environment variables here
# env_file: env_file:
# - .env - docker.env
restart: unless-stopped restart: unless-stopped
healthcheck: # healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3001/api/health"] # test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:9001/api/health"]
interval: 30s # interval: 30s
timeout: 10s # timeout: 10s
retries: 3 # retries: 3
start_period: 40s # start_period: 40s
# Database (if you need one) # Database (if you need one)
# postgres: # postgres: