You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TurboRepo+NextJS项目Docker部署后静态资源丢失求助

问题排查与解决方案

核心问题分析

开发模式正常但Docker部署后静态资源丢失,大概率是NextJS Standalone模式下的静态资源路径配置错误,以及TurboRepo构建时的资源输出路径不匹配导致的。

解决方案步骤

1. 启用NextJS Standalone模式

在apps/client/next.config.js中添加output: 'standalone'配置,这是Docker部署NextJS的必要设置:

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: 'standalone',
  // 保留你的其他原有配置
}

module.exports = nextConfig

2. 修正Dockerfile静态资源复制路径

原Dockerfile将.next/static复制到了public/_next/static,与NextJS Standalone模式的资源查找路径不符。修改runner阶段的静态资源复制命令:

# 替换原有的静态资源复制行
COPY --from=installer --chown=nextjs:nodejs /app/apps/client/.next/static ./.next/static

修改后的完整runner阶段:

FROM base AS runner
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs

# Automatically leverage output traces to reduce image size
COPY --from=installer --chown=nextjs:nodejs /app/apps/client/.next/standalone/apps/client .
COPY --from=installer --chown=nextjs:nodejs /app/apps/client/.next/standalone/node_modules node_modules
COPY --from=installer --chown=nextjs:nodejs /app/apps/client/public public
# 修正静态资源路径
COPY --from=installer --chown=nextjs:nodejs /app/apps/client/.next/static ./.next/static

ENV HOSTNAME="0.0.0.0"
EXPOSE 3000
ENV PORT 3000

CMD node server.js

3. 验证TurboRepo构建配置

检查turbo.json中的build pipeline是否正确包含NextJS构建输出:

{
  "$schema": "https://turbo.build/schema.json",
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": [".next/**", "!.next/cache/**"]
    },
    "dev": {
      "cache": false
    }
  }
}

同时确保apps/client/package.json中的build命令为:

{
  "scripts": {
    "build": "next build",
    // 其他脚本...
  }
}

4. 确认环境变量配置

确保.env文件中NEXT_PUBLIC_前缀的公开环境变量设置正确,避免因路径前缀错误导致资源加载失败。

5. 重新构建部署

执行以下命令重新构建镜像并启动服务:

docker-compose down
docker-compose build --no-cache
docker-compose up -d

额外检查点

  • 查看容器日志排查路径报错:docker logs client
  • 进入容器验证文件结构:docker exec -it client sh,确认/app目录下存在.next/static和public文件夹

内容的提问来源于stack exchange,提问作者Christopher Marshall

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 08:42:34