Docker & Deployment
Docker containerization, deployment configuration, dan production environment setup.
Phase 4: Infrastructure
"Jangan install database langsung di Mac. Gunakan Docker."
Docker
Docker digunakan untuk containerization dan deployment. Opsional untuk local development, tapi wajib untuk production deployment.
Install
brew install --cask dockerSetelah install, buka Docker Desktop dari Applications untuk menyelesaikan setup.
Verifikasi
docker --version # Docker version 28.x.x
docker compose version # Docker Compose version v2.x.xDockerfile
Project ini sudah menyediakan Dockerfile dengan multi-stage build yang optimal:
graph LR
A[Stage 1: deps\nInstall dependencies] --> B[Stage 2: builder\nBuild Next.js]
B --> C[Stage 3: runner\nProduction image]
style A fill:#e3f2fd,stroke:#1565c0
style B fill:#fff3e0,stroke:#e65100
style C fill:#e8f5e9,stroke:#2e7d32Stage 1: Dependencies
FROM node:18-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json pnpm-lock.yaml* ./
RUN corepack enable pnpm && pnpm i --frozen-lockfileStage 2: Builder
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN corepack enable pnpm && pnpm run buildStage 3: Runner (Production)
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
# Non-root user for security
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Copy only necessary files
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
CMD ["node", "server.js"]Key Points
- Base image:
node:18-alpine(minimal size) - Output: Next.js standalone (tanpa node_modules di production)
- Security: Berjalan sebagai user
nextjs, bukan root - Port: 3000
Docker Commands
Build & Run
# Build image
make docker-build
# atau: docker build -t news-faisal .
# Run container
make docker-run
# atau: docker run -p 3000:3000 --env-file .env news-faisal
# Buka http://localhost:3000Management
# Lihat running containers
docker ps
# Lihat logs
docker logs <container-id>
# Stop container
make docker-stop
# atau: docker stop <container-id>
# Hapus image
docker rmi news-faisalEnvironment Variables di Docker
Saat menjalankan container, pass environment variables:
docker run -p 3000:3000 --env-file .env news-faisaldocker run -p 3000:3000 \
-e AUTH_SECRET="your-secret" \
-e NEXT_PUBLIC_APP_URL="https://news.faisalaffan.com" \
news-faisalDomain & Hosting
| Environment | URL | Type |
|---|---|---|
| Production | https://news.faisalaffan.com | Live site |
| Development | http://localhost:3000 | Local |
Next.js Config
Server Actions dibatasi ke allowed origins:
experimental: {
serverActions: {
allowedOrigins: ['news.faisalaffan.com', 'localhost:3000'],
},
}Remote Images
Domain yang diizinkan untuk next/image:
images: {
remotePatterns: [
{ protocol: 'https', hostname: 'images.unsplash.com' },
],
}Makefile Commands
Semua Docker commands sudah tersedia di Makefile:
| Command | Description |
|---|---|
make docker-build | Build Docker image |
make docker-run | Run container dengan .env |
make docker-stop | Stop semua news-faisal containers |
Cara Teardown
Command di bawah akan menghapus container dan data. Pastikan tidak ada data penting sebelum menjalankan.
Edit on GitHub
Last updated on