Skip to content

Commit f8111ce

Browse files
committed
add dockerfile for github
1 parent a4c898f commit f8111ce

4 files changed

Lines changed: 221 additions & 0 deletions

File tree

.dockerignore

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# 依赖
2+
node_modules
3+
npm-debug.log*
4+
yarn-debug.log*
5+
yarn-error.log*
6+
.pnpm-debug.log*
7+
8+
# 本地环境变量文件
9+
.env*
10+
.env.local
11+
.env.development.local
12+
.env.test.local
13+
.env.production.local
14+
15+
# 构建输出
16+
.next/
17+
out/
18+
build/
19+
dist/
20+
21+
# 运行时数据
22+
pids
23+
*.pid
24+
*.seed
25+
*.pid.lock
26+
27+
# 覆盖率目录
28+
coverage/
29+
*.lcov
30+
31+
# nyc 测试覆盖率
32+
.nyc_output
33+
34+
# 依赖锁定文件(保留 pnpm-lock.yaml)
35+
package-lock.json
36+
yarn.lock
37+
38+
# 可选的 npm 缓存目录
39+
.npm
40+
41+
# 可选的 eslint 缓存
42+
.eslintcache
43+
44+
# 可选的 REPL 历史
45+
.node_repl_history
46+
47+
# 输出的 npm 包
48+
*.tgz
49+
50+
# Yarn 完整性文件
51+
.yarn-integrity
52+
53+
# 临时文件夹
54+
tmp/
55+
temp/
56+
57+
# 编辑器目录和文件
58+
.vscode/
59+
.idea/
60+
*.swp
61+
*.swo
62+
*~
63+
64+
# OS 生成的文件
65+
.DS_Store
66+
.DS_Store?
67+
._*
68+
.Spotlight-V100
69+
.Trashes
70+
ehthumbs.db
71+
Thumbs.db
72+
73+
# Git
74+
.git
75+
.gitignore
76+
77+
# Docker
78+
Dockerfile*
79+
docker-compose*
80+
.dockerignore
81+
82+
# 文档
83+
README.md
84+
*.md
85+
86+
# 测试
87+
__tests__/
88+
*.test.js
89+
*.test.ts
90+
*.test.tsx
91+
*.spec.js
92+
*.spec.ts
93+
*.spec.tsx
94+
95+
# TypeScript
96+
*.tsbuildinfo
97+
next-env.d.ts

.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: Build and Push Docker Image
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- master
8+
pull_request:
9+
branches:
10+
- main
11+
- master
12+
workflow_dispatch:
13+
14+
env:
15+
REGISTRY: ghcr.io
16+
IMAGE_NAME: ${{ github.repository }}
17+
18+
jobs:
19+
build-and-push:
20+
runs-on: ubuntu-latest
21+
permissions:
22+
contents: read
23+
packages: write
24+
25+
steps:
26+
- name: Checkout repository
27+
uses: actions/checkout@v4
28+
29+
- name: Set up Docker Buildx
30+
uses: docker/setup-buildx-action@v3
31+
32+
- name: Log in to Container Registry
33+
uses: docker/login-action@v3
34+
with:
35+
registry: ${{ env.REGISTRY }}
36+
username: ${{ github.actor }}
37+
password: ${{ secrets.GITHUB_TOKEN }}
38+
39+
- name: Extract metadata
40+
id: meta
41+
uses: docker/metadata-action@v5
42+
with:
43+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
44+
tags: |
45+
type=ref,event=branch
46+
type=ref,event=pr
47+
type=sha,prefix={{branch}}-
48+
type=raw,value=latest,enable={{is_default_branch}}
49+
50+
- name: Build and push Docker image
51+
uses: docker/build-push-action@v5
52+
with:
53+
context: .
54+
platforms: linux/amd64,linux/arm64
55+
push: true
56+
tags: ${{ steps.meta.outputs.tags }}
57+
labels: ${{ steps.meta.outputs.labels }}
58+
cache-from: type=gha
59+
cache-to: type=gha,mode=max
60+
61+
- name: Generate artifact attestation
62+
uses: actions/attest-build-provenance@v1
63+
with:
64+
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
65+
subject-digest: ${{ steps.build.outputs.digest }}
66+
push-to-registry: true

Dockerfile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 使用官方 Node.js 运行时作为基础镜像
2+
FROM node:18-alpine AS base
3+
4+
# 安装依赖阶段
5+
FROM base AS deps
6+
# 检查 https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine 了解为什么可能需要 libc6-compat
7+
RUN apk add --no-cache libc6-compat
8+
WORKDIR /app
9+
10+
# 安装依赖基于首选包管理器
11+
COPY package.json pnpm-lock.yaml* ./
12+
RUN corepack enable pnpm && pnpm i --frozen-lockfile
13+
14+
# 构建阶段
15+
FROM base AS builder
16+
WORKDIR /app
17+
COPY --from=deps /app/node_modules ./node_modules
18+
COPY . .
19+
20+
# 如果使用 Next.js < 13.5,取消注释下一行并删除上面的行
21+
# ENV NEXT_TELEMETRY_DISABLED 1
22+
23+
RUN corepack enable pnpm && pnpm run build
24+
25+
# 生产镜像,复制所有文件并运行 next
26+
FROM base AS runner
27+
WORKDIR /app
28+
29+
ENV NODE_ENV production
30+
# 取消注释下一行以禁用遥测在运行时
31+
# ENV NEXT_TELEMETRY_DISABLED 1
32+
33+
RUN addgroup --system --gid 1001 nodejs
34+
RUN adduser --system --uid 1001 nextjs
35+
36+
COPY --from=builder /app/public ./public
37+
38+
# 设置正确的权限为预渲染缓存
39+
RUN mkdir .next
40+
RUN chown nextjs:nodejs .next
41+
42+
# 自动利用输出跟踪来减少镜像大小
43+
# https://nextjs.org/docs/advanced-features/output-file-tracing
44+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
45+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
46+
47+
USER nextjs
48+
49+
EXPOSE 3000
50+
51+
ENV PORT 3000
52+
# 设置主机名为 localhost
53+
ENV HOSTNAME "0.0.0.0"
54+
55+
# server.js 由 next build 为独立输出创建
56+
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
57+
CMD ["node", "server.js"]

next.config.ts

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

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

0 commit comments

Comments
 (0)