

Instead, you can simply install production node modules.Īs npm install will download all the node modules, you can do npm prune -production to remove all dev dependencies after you build your application. Copying only the production-required Node modulesįor the application runtime, you don’t need any of the dev dependencies. For example, you’ll need to install g++, make and python3 so that node-gyp can build the native modules.īut you don’t need these packages during the application runtime, so use docker multi-stage build to simply skip these packages in the final image. Sometimes, you might need to install packages to support building the application. Please perform complete end-to-end testing after changing the base image. Although you might reduce the image size, moving to Alpine/slim images might break the application, as these images might not have all the packages like full Node images. For example, if you’re using Node v16, you can use node:16-alpine image as your base image, which will have a size of ~39MB, instead of a node:16 image, which would have a size of ~332MB.īy just changing the base image to Alpine, you can reduce almost 293MB of size from your final image.īut here’s a note of caution. The first step in reducing the image size is to move the base image from Node to Node Alpine.

By simply following the below mentioned steps, I have reduced its size to 450MB and made it almost ~71% more space efficient now. To illustrate this further, I’ve used one of our node images with an initial size of 1.6GB.

Who wouldn’t love a small production image, right? If you’re looking for a way to make your images smaller, here’s how you can do it.
