From 4d87d34f6e87d592c6bff8e2e7c0b0d90330ecee Mon Sep 17 00:00:00 2001 From: cfngc4594 Date: Tue, 25 Mar 2025 21:43:45 +0800 Subject: [PATCH] feat(docker): update optimized Dockerfile and compose for CN environment --- Dockerfile.cn | 72 ++++++++++++++++++++++++++++ compose.cn.yml | 83 +++++++++++++++++++++++++++++++++ docker/lsp/clangd/Dockerfile.cn | 25 ++++++++++ 3 files changed, 180 insertions(+) create mode 100644 Dockerfile.cn create mode 100644 compose.cn.yml create mode 100644 docker/lsp/clangd/Dockerfile.cn diff --git a/Dockerfile.cn b/Dockerfile.cn new file mode 100644 index 0000000..6fbdd06 --- /dev/null +++ b/Dockerfile.cn @@ -0,0 +1,72 @@ +# syntax=docker.io/docker/dockerfile:1 + +# 升级到 Node.js v20 或更高版本,以解决 `ReferenceError: File is not defined` 问题 +# 参考链接:https://github.com/vercel/next.js/discussions/56032 +FROM dockerp.com/node:22-alpine AS base + +# 仅在需要时安装依赖 +FROM base AS deps +# 查看 https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine 了解为什么可能需要安装 libc6-compat。 +RUN sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirror.nju.edu.cn/alpine#g' /etc/apk/repositories && apk add --no-cache libc6-compat +WORKDIR /app + +# 在安装依赖前复制 prisma 目录 +COPY prisma ./prisma + +# 根据使用的包管理工具安装依赖 +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* bun.lock* .npmrc* ./ +RUN \ + if [ -f yarn.lock ]; then yarn config set registry https://registry.npmmirror.com && yarn --frozen-lockfile; \ + elif [ -f package-lock.json ]; then npm config set registry https://registry.npmmirror.com && npm ci; \ + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm config set registry https://registry.npmmirror.com && pnpm i --frozen-lockfile; \ + elif [ -f bun.lock ] || [ -f bun.lockb ]; then npm config set registry https://registry.npmmirror.com && npm install -g bun && printf '[install]\nregistry = "https://registry.npmmirror.com/"\n' > bunfig.toml && bun install --frozen-lockfile; \ + else echo "未找到锁文件。" && exit 1; \ + fi + +# 仅在需要时重新构建源代码 +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +# Next.js 会收集完全匿名的遥测数据,以了解一般使用情况。 +# 了解更多信息:https://nextjs.org/telemetry +# 如果希望在构建时禁用遥测功能,请取消注释以下行。 +ENV NEXT_TELEMETRY_DISABLED=1 + +RUN \ + if [ -f yarn.lock ]; then yarn config set registry https://registry.npmmirror.com && yarn --frozen-lockfile; \ + elif [ -f package-lock.json ]; then npm config set registry https://registry.npmmirror.com && npm ci; \ + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm config set registry https://registry.npmmirror.com && pnpm i --frozen-lockfile; \ + elif [ -f bun.lock ] || [ -f bun.lockb ]; then npm config set registry https://registry.npmmirror.com && npm install -g bun && printf '[install]\nregistry = "https://registry.npmmirror.com/"\n' > bunfig.toml && bun run build; \ + else echo "未找到锁文件。" && exit 1; \ + fi + +# 生产环境镜像,复制所有文件并运行 Next.js +FROM base AS runner +WORKDIR /app + +# 安装 curl 工具,以确保健康检查正常运行 +RUN sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirror.nju.edu.cn/alpine#g' /etc/apk/repositories && apk add --no-cache curl + +ENV NODE_ENV=production +# 如果希望在运行时禁用遥测功能,请取消注释以下行。 +ENV NEXT_TELEMETRY_DISABLED=1 + +COPY --from=builder /app/public ./public + +# 自动利用输出跟踪功能以减少镜像大小 +# 参考: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 root + +EXPOSE 3000 + +ENV PORT=3000 + +# server.js 由 next build 在 standalone 输出模式下创建 +# 参考:https://nextjs.org/docs/pages/api-reference/config/next-config-js/output +ENV HOSTNAME="0.0.0.0" +CMD ["node", "server.js"] diff --git a/compose.cn.yml b/compose.cn.yml new file mode 100644 index 0000000..d95c3c6 --- /dev/null +++ b/compose.cn.yml @@ -0,0 +1,83 @@ +services: + judge4c: + build: + context: . + dockerfile: Dockerfile.cn + image: judge4c:latest + container_name: judge4c + restart: always + ports: + - "3000:3000" + networks: + - judge4c + depends_on: + postgres: + condition: service_healthy + lsp-c: + condition: service_healthy + lsp-cpp: + condition: service_healthy + healthcheck: + test: [ "CMD-SHELL", "curl --fail http://localhost:3000 || exit 1" ] + interval: 10s + timeout: 5s + retries: 5 + start_period: 10s + volumes: + - /var/run/docker.sock:/var/run/docker.sock + + lsp-c: + build: + context: ./docker/lsp/clangd + dockerfile: Dockerfile.cn + image: lsp-c:latest + container_name: lsp-c + restart: always + ports: + - "4594:3000" + networks: + - judge4c + healthcheck: + test: [ "CMD-SHELL", "nc -zv localhost 3000" ] + interval: 10s + timeout: 5s + retries: 5 + + lsp-cpp: + build: + context: ./docker/lsp/clangd + dockerfile: Dockerfile.cn + image: lsp-cpp:latest + container_name: lsp-cpp + restart: always + ports: + - "4595:3000" + networks: + - judge4c + healthcheck: + test: [ "CMD-SHELL", "nc -zv localhost 3000" ] + interval: 10s + timeout: 5s + retries: 5 + + postgres: + image: postgres:16-alpine + container_name: postgres + restart: always + environment: + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + - POSTGRES_DB=${POSTGRES_DB} + ports: + - "5432:5432" + networks: + - judge4c + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres -d postgres"] + interval: 5s + timeout: 2s + retries: 20 + +networks: + judge4c: + name: judge4c diff --git a/docker/lsp/clangd/Dockerfile.cn b/docker/lsp/clangd/Dockerfile.cn new file mode 100644 index 0000000..b9b0a81 --- /dev/null +++ b/docker/lsp/clangd/Dockerfile.cn @@ -0,0 +1,25 @@ +FROM dockerp.com/alpine:latest AS builder + +RUN sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirror.nju.edu.cn/alpine#g' /etc/apk/repositories && apk add --no-cache git npm + +WORKDIR /app + +RUN git clone https://gh-proxy.com/github.com/wylieconlon/jsonrpc-ws-proxy.git + +WORKDIR /app/jsonrpc-ws-proxy + +COPY servers.yml . + +RUN npm config set registry https://repo.nju.edu.cn/repository/npm/ && npm install && npm run prepare + +FROM dockerp.com/alpine:latest + +RUN sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirror.nju.edu.cn/alpine#g' /etc/apk/repositories && apk add --no-cache build-base clang-extra-tools nodejs + +WORKDIR /app/jsonrpc-ws-proxy + +COPY --from=builder /app/jsonrpc-ws-proxy . + +EXPOSE 3000 + +CMD ["node", "dist/server.js", "--port", "3000", "--languageServers", "servers.yml"]