Skip to content

Commit 271f3b0

Browse files
authored
Merge pull request #44 from jianyun8023/dev
Add Docker Support with Multi-stage Build
2 parents 53754e6 + bc0f767 commit 271f3b0

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

.dockerignore

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Dependencies
2+
node_modules
3+
npm-debug.log*
4+
yarn-debug.log*
5+
yarn-error.log*
6+
7+
# Build output
8+
.next
9+
out
10+
dist
11+
build
12+
13+
# Testing
14+
coverage
15+
.nyc_output
16+
17+
# Environment variables
18+
.env
19+
.env*.local
20+
.env.local
21+
.env.development.local
22+
.env.test.local
23+
.env.production.local
24+
25+
# Git
26+
.git
27+
.gitignore
28+
.gitattributes
29+
30+
# IDE
31+
.vscode
32+
.idea
33+
*.swp
34+
*.swo
35+
*~
36+
37+
# Operating System
38+
.DS_Store
39+
Thumbs.db
40+
41+
# Documentation
42+
README.md
43+
*.md
44+
!env.example
45+
46+
# CI/CD
47+
.github
48+
.gitlab-ci.yml
49+
.travis.yml
50+
51+
# Docker
52+
Dockerfile
53+
.dockerignore
54+
docker-compose*.yml
55+
56+
# Other
57+
*.log
58+
.cache
59+
.turbo
60+

.github/workflows/docker-build.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Docker Build and Push
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- master
8+
- dev
9+
tags:
10+
- 'v*'
11+
pull_request:
12+
branches:
13+
- main
14+
- master
15+
16+
env:
17+
REGISTRY: ghcr.io
18+
IMAGE_NAME: ${{ github.repository }}
19+
20+
jobs:
21+
build-and-push:
22+
runs-on: ubuntu-latest
23+
permissions:
24+
contents: read
25+
packages: write
26+
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/checkout@v4
30+
31+
- name: Set up Docker Buildx
32+
uses: docker/setup-buildx-action@v3
33+
34+
- name: Log in to GitHub Container Registry
35+
if: github.event_name != 'pull_request'
36+
uses: docker/login-action@v3
37+
with:
38+
registry: ${{ env.REGISTRY }}
39+
username: ${{ github.actor }}
40+
password: ${{ secrets.GITHUB_TOKEN }}
41+
42+
- name: Extract metadata (tags, labels)
43+
id: meta
44+
uses: docker/metadata-action@v5
45+
with:
46+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
47+
tags: |
48+
type=ref,event=branch
49+
type=ref,event=pr
50+
type=semver,pattern={{version}}
51+
type=semver,pattern={{major}}.{{minor}}
52+
type=semver,pattern={{major}}
53+
type=sha,prefix={{branch}}-
54+
type=raw,value=latest,enable={{is_default_branch}}
55+
56+
- name: Build and push Docker image
57+
uses: docker/build-push-action@v5
58+
with:
59+
context: .
60+
push: ${{ github.event_name != 'pull_request' }}
61+
tags: ${{ steps.meta.outputs.tags }}
62+
labels: ${{ steps.meta.outputs.labels }}
63+
cache-from: type=gha
64+
cache-to: type=gha,mode=max
65+
platforms: linux/amd64,linux/arm64
66+

Dockerfile

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Multi-stage Dockerfile for Next.js
2+
3+
# Stage 1: Install dependencies
4+
FROM node:20-alpine AS deps
5+
RUN apk add --no-cache libc6-compat
6+
WORKDIR /app
7+
8+
# Copy package files
9+
COPY package.json package-lock.json* ./
10+
11+
# Install dependencies
12+
RUN npm ci
13+
14+
# Stage 2: Build application
15+
FROM node:20-alpine AS builder
16+
WORKDIR /app
17+
18+
# Copy node_modules from deps stage
19+
COPY --from=deps /app/node_modules ./node_modules
20+
COPY . .
21+
22+
# Disable Next.js telemetry during build
23+
ENV NEXT_TELEMETRY_DISABLED=1
24+
25+
# Build Next.js application (standalone mode)
26+
RUN npm run build
27+
28+
# Stage 3: Production runtime
29+
FROM node:20-alpine AS runner
30+
WORKDIR /app
31+
32+
ENV NODE_ENV=production
33+
ENV NEXT_TELEMETRY_DISABLED=1
34+
35+
# Create non-root user for security
36+
RUN addgroup --system --gid 1001 nodejs
37+
RUN adduser --system --uid 1001 nextjs
38+
39+
# Copy necessary files
40+
COPY --from=builder /app/public ./public
41+
42+
# Copy standalone build output
43+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
44+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
45+
46+
USER nextjs
47+
48+
EXPOSE 3000
49+
50+
ENV PORT=3000
51+
ENV HOSTNAME="0.0.0.0"
52+
53+
# Start the application
54+
CMD ["node", "server.js"]
55+

next.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { NextConfig } from "next";
22

33
const nextConfig: NextConfig = {
44
/* config options here */
5+
output: 'standalone',
56
};
67

78
export default nextConfig;

0 commit comments

Comments
 (0)