feat: Containerize the application with Docker and Docker Compose, including a PostgreSQL database and a deployment script.

This commit is contained in:
Moh Dzulfikri Maulana
2026-03-09 00:13:31 +07:00
parent bdd61d11d3
commit c7b022d502
6 changed files with 139 additions and 1 deletions

12
.gitignore vendored
View File

@@ -1,3 +1,13 @@
node_modules
.next
.env
.env
.DS_Store
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
dist
build

3
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"postman.settings.dotenv-detection-notification-visibility": false
}

55
Dockerfile Normal file
View File

@@ -0,0 +1,55 @@
# Stage 1: Install dependencies
FROM node:20-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install pnpm
RUN npm install -g pnpm
# Copy package files
COPY package.json pnpm-lock.yaml ./
# Copy prisma directory to generate client
COPY prisma ./prisma/
# Install dependencies
RUN pnpm install --frozen-lockfile
# Stage 2: Build the app
FROM node:20-alpine AS builder
WORKDIR /app
RUN npm install -g pnpm
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Generate Prisma Client
RUN pnpm prisma generate
# Build Next.js
# Disable telemetry during build
ENV NEXT_TELEMETRY_DISABLED 1
RUN pnpm build
# Stage 3: Runner
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Copy necessary files
COPY --from=builder /app/public ./public
COPY --from=builder /app/prisma ./prisma
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 4000
ENV PORT 4000
ENV HOSTNAME "0.0.0.0"
CMD ["node", "server.js"]

23
deploy.sh Executable file
View File

@@ -0,0 +1,23 @@
#!/bin/bash
# Load environment variables from .env if it exists
if [ -f .env ]; then
export $(grep -v '^#' .env | xargs)
fi
# Name of the database container
DB_CONTAINER_NAME="portfolio-db"
# Check if the database container is already running
if [ "$(docker ps -q -f name=${DB_CONTAINER_NAME})" ]; then
echo "✅ Database container '${DB_CONTAINER_NAME}' is already running."
echo "🚀 Deploying/Updating only the application..."
docker-compose up -d --no-recreate db
docker-compose up -d app
else
echo "⚠️ Database container '${DB_CONTAINER_NAME}' not found or stopped."
echo "🏗️ Deploying full stack (Database + App)..."
docker-compose up -d
fi
echo "✨ Deployment script finished!"

46
docker-compose.yml Normal file
View File

@@ -0,0 +1,46 @@
version: '3.8'
services:
db:
image: postgres:15-alpine
container_name: portfolio-db
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER:-fikri}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-fikri}
POSTGRES_DB: ${POSTGRES_DB:-fullstack-portfolio}
ports:
- "5429:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-fikri} -d ${POSTGRES_DB:-fullstack-portfolio}"]
interval: 5s
timeout: 5s
retries: 5
app:
build:
context: .
dockerfile: Dockerfile
container_name: portfolio-app
restart: always
ports:
- "4000:4000"
environment:
DATABASE_URL: postgresql://${POSTGRES_USER:-fikri}:${POSTGRES_PASSWORD:-fikri}@db:5432/${POSTGRES_DB:-fullstack-portfolio}?schema=public
JWT_SECRET: ${JWT_SECRET}
R2_TOKEN: ${R2_TOKEN}
R2_ACCESS_KEY: ${R2_ACCESS_KEY}
R2_SECRET_KEY: ${R2_SECRET_KEY}
R2_ENDPOINT: ${R2_ENDPOINT}
R2_BUCKET_NAME: ${R2_BUCKET_NAME}
IMAGE_URL: ${IMAGE_URL}
depends_on:
db:
condition: service_healthy
command: >
sh -c "npx prisma db push && node server.js"
volumes:
postgres_data:

View File

@@ -1,6 +1,7 @@
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: 'standalone',
turbopack: {
root: __dirname,
},