From 837ca687fc6580b727a2a73b2842183bcf81b1cb Mon Sep 17 00:00:00 2001 From: star7th Date: Thu, 10 Apr 2025 20:34:49 +0800 Subject: [PATCH] docker --- .github/workflows/docker.yml | 23 ++++++++++++++++ Dockerfile | 4 +-- Dockerfile-ARM | 51 ++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 Dockerfile-ARM diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 8857744..92afecd 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -41,6 +41,7 @@ jobs: type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=sha,format=short + type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }} - name: Build and push uses: docker/build-push-action@v4 @@ -50,4 +51,26 @@ jobs: tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Extract metadata for ARM Docker + id: meta-arm + if: github.ref == 'refs/heads/master' && github.event_name != 'pull_request' + uses: docker/metadata-action@v4 + with: + images: star7th/jisuxiang + tags: | + type=raw,value=arm-latest + + - name: Build and push ARM version + if: github.ref == 'refs/heads/master' && github.event_name != 'pull_request' + uses: docker/build-push-action@v4 + with: + context: . + file: ./Dockerfile-ARM + push: true + platforms: linux/arm64 + tags: ${{ steps.meta-arm.outputs.tags }} + labels: ${{ steps.meta-arm.outputs.labels }} + cache-from: type=gha cache-to: type=gha,mode=max \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index f61d282..9ef6695 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,8 +40,8 @@ USER nextjs EXPOSE 3000 -ENV PORT 3000 -ENV HOSTNAME "0.0.0.0" +ENV PORT=3000 +ENV HOSTNAME="0.0.0.0" # 启动应用 CMD ["node", "server.js"] \ No newline at end of file diff --git a/Dockerfile-ARM b/Dockerfile-ARM new file mode 100644 index 0000000..9b191d8 --- /dev/null +++ b/Dockerfile-ARM @@ -0,0 +1,51 @@ +# 构建阶段 - ARM版本 +FROM --platform=linux/arm64 node:20-alpine AS builder + +WORKDIR /app + +# 复制依赖文件 +COPY package.json package-lock.json* ./ + +# 安装依赖 +RUN npm ci + +# 复制源代码 +COPY . . + +# 构建应用 +RUN npm run build + +# 生产阶段 +FROM --platform=linux/arm64 node:20-alpine AS runner + +WORKDIR /app + +ENV NODE_ENV=production + +# 创建非root用户 +RUN addgroup --system --gid 1001 nodejs && \ + adduser --system --uid 1001 nextjs + +# 只复制生产必要的文件 +COPY --from=builder /app/next.config.js ./ +COPY --from=builder /app/public ./public +COPY --from=builder /app/.next/standalone ./ +COPY --from=builder /app/.next/static ./.next/static + +# 设置正确的权限 +RUN chown -R nextjs:nodejs /app + +# 使用非root用户运行 +USER nextjs + +EXPOSE 3000 + +# 修正ENV语法为新格式 +ENV PORT=3000 +ENV HOSTNAME="0.0.0.0" + +# ARM特定优化 +ENV NODE_OPTIONS="--max-old-space-size=2048" + +# 启动应用 +CMD ["node", "server.js"] \ No newline at end of file