I have a simple Next project in Docker following the common recommendations for small-size containers. Heres my Dockerfile:

FROM node:16-alpine as dependencies
COPY package.json ./
COPY yarn.lock ./
RUN yarn install

FROM node:16-alpine as builder
LABEL delme=true
COPY . .
COPY --from=dependencies /app/node_modules ./node_modules
RUN apk add g++ make py3-pip
RUN yarn build

FROM node:16-alpine
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/package.json ./package.json
RUN yarn install --production

CMD 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:

enter image description here

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"
  • Heya, I recommend you read docs of yarn. yarn install - Yarn install is used to install all dependencies for a project.. I hope it helps a bit with your debugging adventure.
    – Kyslik
    Commented Jun 21, 2023 at 19:19
  • node_modules IS in the .dockerignore to prevent the hosts being copied over? Commented Jun 22, 2023 at 6:09


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.