I have a simple Next project in Docker following the common recommendations for small-size containers. Heres my Dockerfile:
FROM node:16-alpine as dependenciesWORKDIR /appCOPY package.json ./COPY yarn.lock ./RUN yarn installFROM node:16-alpine as builderLABEL delme=trueWORKDIR /appCOPY . .COPY --from=dependencies /app/node_modules ./node_modulesRUN apk add g++ make py3-pipRUN yarn buildFROM node:16-alpineWORKDIR /appCOPY --from=builder /app/next.config.js ./COPY --from=builder /app/public ./publicCOPY --from=builder /app/.next ./.nextCOPY --from=builder /app/package.json ./package.jsonRUN yarn install --productionEXPOSE 9765CMD yarn start
As you can see, I'm using multistage builds, installing the correct dependencies, and copying just what we need. But the total size of the image is around 1.5GB, and looking at the Docker GUI I can see that the bulk of it is in the dependencies installation:
It's strange because the node_module folder if I build the same stack in the host machine is not larger than 300Mb. Where is that overhead coming from?
PS: This is my package.json:
{"private": true,"scripts": {"dev": "next","build": "next build","export": "next build && next export","start": "next start -p 9765","type-check": "tsc" },"dependencies": {"@headlessui/react": "^1.7.4","@heroicons/react": "^2.0.16","@radix-ui/react-tooltip": "^1.0.2","@tailwindcss/forms": "^0.5.3","@tanstack/react-query": "^4.16.1","axios": "^1.1.3","classnames": "^2.3.2","daisyui": "^2.51.0","eslint": "8.27.0","eslint-config-next": "12.2.3","jotai": "^1.9.2","next": "12.2.3","next-seo": "^5.15.0","react": "18.2.0","react-dom": "18.2.0","react-dropzone": "^14.2.3","react-pdf-tailwind": "^1.0.1","react-toastify": "^9.1.1","recharts": "^2.4.3","tailwind-merge": "^1.8.0","tailwindcss-animatecss": "^3.0.2","typescript": "4.9.3" },"devDependencies": {"@svgr/webpack": "^6.5.1","@types/node": "18.11.9","@types/react": "18.0.25","@types/react-dom": "18.0.9","@typescript-eslint/eslint-plugin": "^5.54.0","autoprefixer": "^10.4.13","eslint-plugin-simple-import-sort": "^10.0.0","eslint-plugin-tailwindcss": "^3.10.1","postcss": "^8.4.21","tailwindcss": "^3.2.7","typescript": "^4.8.3" },"peerDependencies": {"@unovis/ts": "^1.0.3" }}