<p>I have a simple Next project in Docker following the common recommendations for small-size containers. Heres my  Dockerfile:</p>
<pre><code>FROM node:16-alpine as dependencies
WORKDIR /app
COPY package.json ./
COPY yarn.lock ./
RUN yarn install


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


FROM node:16-alpine
WORKDIR /app
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
EXPOSE 9765

CMD yarn start
</code></pre>
<p>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:</p>
<p><a href="https://i.sstatic.net/bwIP7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/bwIP7.png" alt="enter image description here" /></a></p>
<p>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?</p>
<p>PS: This is my package.json:</p>
<pre><code>{
    &quot;private&quot;: true,
    &quot;scripts&quot;: {
        &quot;dev&quot;: &quot;next&quot;,
        &quot;build&quot;: &quot;next build&quot;,
        &quot;export&quot;: &quot;next build &amp;&amp; next export&quot;,
        &quot;start&quot;: &quot;next start -p 9765&quot;,
        &quot;type-check&quot;: &quot;tsc&quot;
    },
    &quot;dependencies&quot;: {
        &quot;@headlessui/react&quot;: &quot;^1.7.4&quot;,
        &quot;@heroicons/react&quot;: &quot;^2.0.16&quot;,
        &quot;@radix-ui/react-tooltip&quot;: &quot;^1.0.2&quot;,
        &quot;@tailwindcss/forms&quot;: &quot;^0.5.3&quot;,
        &quot;@tanstack/react-query&quot;: &quot;^4.16.1&quot;,
        &quot;axios&quot;: &quot;^1.1.3&quot;,
        &quot;classnames&quot;: &quot;^2.3.2&quot;,
        &quot;daisyui&quot;: &quot;^2.51.0&quot;,
        &quot;eslint&quot;: &quot;8.27.0&quot;,
        &quot;eslint-config-next&quot;: &quot;12.2.3&quot;,
        &quot;jotai&quot;: &quot;^1.9.2&quot;,
        &quot;next&quot;: &quot;12.2.3&quot;,
        &quot;next-seo&quot;: &quot;^5.15.0&quot;,
        &quot;react&quot;: &quot;18.2.0&quot;,
        &quot;react-dom&quot;: &quot;18.2.0&quot;,
        &quot;react-dropzone&quot;: &quot;^14.2.3&quot;,
        &quot;react-pdf-tailwind&quot;: &quot;^1.0.1&quot;,
        &quot;react-toastify&quot;: &quot;^9.1.1&quot;,
        &quot;recharts&quot;: &quot;^2.4.3&quot;,
        &quot;tailwind-merge&quot;: &quot;^1.8.0&quot;,
        &quot;tailwindcss-animatecss&quot;: &quot;^3.0.2&quot;,
        &quot;typescript&quot;: &quot;4.9.3&quot;
    },
    &quot;devDependencies&quot;: {
        &quot;@svgr/webpack&quot;: &quot;^6.5.1&quot;,
        &quot;@types/node&quot;: &quot;18.11.9&quot;,
        &quot;@types/react&quot;: &quot;18.0.25&quot;,
        &quot;@types/react-dom&quot;: &quot;18.0.9&quot;,
        &quot;@typescript-eslint/eslint-plugin&quot;: &quot;^5.54.0&quot;,
        &quot;autoprefixer&quot;: &quot;^10.4.13&quot;,
        &quot;eslint-plugin-simple-import-sort&quot;: &quot;^10.0.0&quot;,
        &quot;eslint-plugin-tailwindcss&quot;: &quot;^3.10.1&quot;,
        &quot;postcss&quot;: &quot;^8.4.21&quot;,
        &quot;tailwindcss&quot;: &quot;^3.2.7&quot;,
        &quot;typescript&quot;: &quot;^4.8.3&quot;
    },
    &quot;peerDependencies&quot;: {
        &quot;@unovis/ts&quot;: &quot;^1.0.3&quot;
    }
}
</code></pre>

CC BY-SA 4.0
2023-06-21T19:19:49.120Z
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
CC BY-SA 4.0
2023-06-22T06:09:30.630Z
node_modules IS in the .dockerignore to prevent the hosts being copied over?
Chris Becke
<docker><dockerfile>
node_modules is several orders of magnitude bigger inside the container compare to host machine
DomingoSL
# node_modules is several orders of magnitude bigger inside the container compare to host machine

(Asked by: username_0 on 2023-06-16)

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
WORKDIR /app
COPY package.json ./
COPY yarn.lock ./
RUN yarn install


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


FROM node:16-alpine
WORKDIR /app
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
EXPOSE 9765

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:

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"
    }
}

**Tags:** docker, dockerfile

> **Comments:**
> **username_1**: 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.  
> **username_2**: node_modules IS in the .dockerignore to prevent the hosts being copied over?  

---

## 0 answers

