最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reducing the Docker image size of Next.js in a Turborepo - Stack Overflow

programmeradmin5浏览0评论

I have set up a Turborepo with a Next.js application (admin-frontend) and three packages:

  • eslint-config
  • typescript-config
  • ui

All three packages are used within the Next.js application.

After building my Docker image, it has a size of around 850MB, which seems quite large to me. Am I unintentionally copying unnecessary files, or am I not following the correct Turborepo workflow, which might be inflating the image size?

This is the Dockerfile of the next.js application

FROM node:20-alpine AS base

# Prune project
FROM base AS prune
WORKDIR /app
RUN npm install -g turbo
COPY . .
RUN turbo prune @project/admin-frontend --docker

# Install dependencies
FROM base AS installer
WORKDIR /app
COPY --from=prune /app/out/json/ .
RUN npm install

# Install production dependencies
FROM base AS installer-production
WORKDIR /app
COPY --from=prune /app/out/json .
RUN npm install --only=production

# Build the project
FROM base AS builder
WORKDIR /app
COPY --from=installer /app/node_modules /app/node_modules
COPY --from=prune /app/out/full/ .
RUN npx turbo build --filter=@project/admin-frontend 

# Final image
FROM base AS runner
WORKDIR /app
COPY --from=installer-production /app/node_modules /app/node_modules
COPY --from=builder  /app/apps/admin-frontend/package.json /app/apps/admin-frontend/
COPY --from=builder  /app/apps/admin-frontend/.next /app/apps/admin-frontend/.next

WORKDIR /app/apps/admin-frontend

CMD ["npm", "run", "start"]

This is the package.json of the next.js application:

{
  "name": "@project/admin-frontend",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "dotenv -e .env -- next dev --turbopack",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@project/ui": "*",
    "next": "15.2.3",
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
  },
  "devDependencies": {
    "@eslint/eslintrc": "^3",
    "@project/eslint-config": "*",
    "@project/typescript-config": "*",
    "@tailwindcss/postcss": "^4",
    "@types/node": "^20",
    "@types/react": "^19",
    "@types/react-dom": "^19",
    "dotenv-cli": "^8.0.0",
    "eslint": "^9",
    "eslint-config-next": "15.2.3",
    "tailwindcss": "^4",
    "typescript": "^5"
  }
}

If I run du in /app/apps/admin-frontend inside the container, I get the following output.

12  ./.next/diagnostics
12  ./.next/static/css
96  ./.next/static/media
8   ./.next/static/chunks/app/_not-found
20  ./.next/static/chunks/app
12  ./.next/static/chunks/pages
796 ./.next/static/chunks
12  ./.next/static/_eCz52W9GGEyf7Z-bojwC
920 ./.next/static
12  ./.next/types/app
28  ./.next/types
8   ./.next/cache/eslint
4   ./.next/cache/swc/plugins/v7_linux_aarch64_8.0.0
8   ./.next/cache/swc/plugins
12  ./.next/cache/swc
12  ./.next/cache/webpack/edge-server-production
22208   ./.next/cache/webpack/client-production
15416   ./.next/cache/webpack/server-production
37640   ./.next/cache/webpack
37668   ./.next/cache
348 ./.next/server/chunks
20  ./.next/server/app/_not-found
72  ./.next/server/app/favicon.ico
212 ./.next/server/app
48  ./.next/server/pages
660 ./.next/server
39536   ./.next
39544   .
发布评论

评论列表(0)

  1. 暂无评论