Skip to content
GitShowcase Forum

My Node.js Docker image is over a gigabyte, how do I make it smaller?

Asked by Kwesi A. · Hosting, Edge and Deploys2 repliesupdated

Short answer

I am containerising a client's Express and TypeScript API for the first time. It works, but docker image ls shows the image is over a gigabyte, and pushing it to the registry from my slow connection takes ages. The client's host also bills for registry storage, so I would like it lean.

Written by Kwesi A. ·
CiteGitShowcase Forum, "My Node.js Docker image is over a gigabyte, how do I make it smaller?", https://gitshowcase.com/forum/discuss/my-nodejs-docker-image-is-over-a-gigabyte-how-do-i-make-it-smaller/, accessed 2026-12-05.
  1. Member ·

    This is my whole Dockerfile:

    FROM node:22
    WORKDIR /app
    COPY . .
    RUN npm install
    RUN npm run build
    CMD ["node", "dist/server.js"]
    

    There is no .dockerignore. The project has about 40 dependencies and a dozen dev dependencies (TypeScript, ESLint, test tooling).

    What are the biggest wins, in order? I have read about multi-stage builds and Alpine images but I do not know which matters more, and I am worried that a smaller base image will break things I cannot see yet. I would also like to know what should never end up inside the image, since I copy the whole folder right now.

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

  1. Teodor M.Member ·

    Start with a .dockerignore. COPY . . takes your local node_modules, .git, build output and probably an .env into the image, and the local node_modules may even overwrite the one npm install just made. Ignore them all before touching anything else.

  2. Imani H.Member ·

    I think you can just add RUN npm prune --production and RUN rm -rf the dev stuff at the end of the Dockerfile and it gets smaller? That is what a tutorial showed me.

GitShowcase Forum. Questions and answers on shipping developer projects: dev tools, APIs, infrastructure and portfolios.