feat: Add Docker support with standalone Next.js output.

This commit is contained in:
kuekhaoyang
2025-11-29 10:19:37 +08:00
parent 55240606a9
commit 78332bd0c2
5 changed files with 141 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
.git
+68
View File
@@ -0,0 +1,68 @@
FROM node:22-alpine AS base
# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN \
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT 3000
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"
CMD ["node", "server.js"]
+51
View File
@@ -521,6 +521,57 @@ npm run build
# 输出到 out/ 目录
```
### 🐳 Docker 部署
#### 1. 获取代码
```bash
git clone https://github.com/KuekHaoYang/KVideo.git
cd KVideo
```
#### 2. 构建镜像
```bash
docker build -t kvideo .
```
#### 3. 运行容器
```bash
docker-compose up -d
```
应用将在 `http://localhost:3000` 启动。
#### 3. 使用 Docker Hub 安装
如果你不想自己构建镜像,可以直接从 Docker Hub 拉取:
```bash
# 拉取镜像
docker pull kuekhaoyang/kvideo:latest
# 运行容器
docker run -d -p 3000:3000 --name kvideo kuekhaoyang/kvideo:latest
```
#### 4. 如何更新镜像
当有新版本发布时,可以通过以下命令更新:
```bash
# 1. 停止并删除旧容器
docker stop kvideo
docker rm kvideo
# 2. 拉取最新镜像
docker pull kuekhaoyang/kvideo:latest
# 3. 重新运行容器
docker run -d -p 3000:3000 --name kvideo kuekhaoyang/kvideo:latest
```
## 🤝 贡献
我们非常欢迎各种形式的贡献!请查看 [CONTRIBUTING.md](CONTRIBUTING.md) 了解详细的贡献指南。
+13
View File
@@ -0,0 +1,13 @@
version: '3'
services:
kvideo:
container_name: kvideo
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- 3000:3000
environment:
- NODE_ENV=production
+2
View File
@@ -10,6 +10,8 @@ const nextConfig: NextConfig = {
removeConsole: process.env.NODE_ENV === 'production',
},
output: 'standalone',
images: {
remotePatterns: [
// Douban images