From 78332bd0c2ce79b3f0b9101750eefc6ae1eeff17 Mon Sep 17 00:00:00 2001 From: kuekhaoyang Date: Sat, 29 Nov 2025 10:19:37 +0800 Subject: [PATCH] feat: Add Docker support with standalone Next.js output. --- .dockerignore | 7 +++++ Dockerfile | 68 ++++++++++++++++++++++++++++++++++++++++++++++ README.md | 51 ++++++++++++++++++++++++++++++++++ docker-compose.yml | 13 +++++++++ next.config.ts | 2 ++ 5 files changed, 141 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c550055 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +Dockerfile +.dockerignore +node_modules +npm-debug.log +README.md +.next +.git diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..29b3bd0 --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/README.md b/README.md index 78f863d..3186570 100644 --- a/README.md +++ b/README.md @@ -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) 了解详细的贡献指南。 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5fbdfcf --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +version: '3' + +services: + kvideo: + container_name: kvideo + build: + context: . + dockerfile: Dockerfile + restart: always + ports: + - 3000:3000 + environment: + - NODE_ENV=production diff --git a/next.config.ts b/next.config.ts index 7b36d92..314efe9 100644 --- a/next.config.ts +++ b/next.config.ts @@ -10,6 +10,8 @@ const nextConfig: NextConfig = { removeConsole: process.env.NODE_ENV === 'production', }, + output: 'standalone', + images: { remotePatterns: [ // Douban images