3

I am using this docker file to give a simple hello world output on the browser. The docker file copies in the package.json(which already has express defined on it) and the index.json(which uses the express framework to display hello world)

Currently the size of this image is around 900Mb, I do want to make it smaller. I have tried multi-stage build , but im quite new to docker so don't really know how to.

FROM node:latest
WORKDIR /app
COPY package.json index.j ./
RUN npm install
EXPOSE 8080
CMD node index.js

As currently this is a very big docker file, how can I reduce the size using multi-stage?

trying2dev
  • 51
  • 5
  • After you install your npm packages dou you still need them? Or somehow with building just js file is enough? – Mustafa Güler Mar 01 '21 at 18:07
  • @MustafaGüler I am not sure exactly what packages I need .. as alot of npm packages are installed ,but maybe I wont need them .. how can I try and check ? – trying2dev Mar 01 '21 at 18:17
  • I guess js uses packages in action. You have to minimize your package.json file to download only necessary packages. – Mustafa Güler Mar 01 '21 at 18:20
  • @MustafaGüler currently the only dependency in my package.json file is express, which is used in the index.js file.. its when npm install creates node modules .. thos increase the size of the docker image .. but I dont know what modules I need. – trying2dev Mar 01 '21 at 18:28
  • @MustafaGüler ok thanks , il try that but I also did want to see if multi-stage could be used on this .. would that not work ? – trying2dev Mar 01 '21 at 18:48
  • I shared my answer below. But it is not about multi-stage. You use so big image. Also you need packages installed. – Mustafa Güler Mar 01 '21 at 19:12

2 Answers2

3

Try to use node:slim (160 MB) image not latest (960 MB) which has common packages inside.

Mustafa Güler
  • 884
  • 6
  • 11
  • For me when I used node:slim , hello word wouldn't display in the browser .. bu tI have used node:alpine which is the same size as slim but displays – trying2dev Mar 01 '21 at 20:19
2

Try to work with a virtual machine that solving many issues with the size issues of docker images, which pull from the docker hub. After that, you can work very fast with docker hub images, otherwise, the size of docker hub images which pull may be a big issue for you.

udara vimukthi
  • 168
  • 1
  • 8
  • If I understood your answer correctly you're suggesting VMs instead of docker, right? The question is about whether there's a solution to reduce image size using multi-stage builds or not; and not about other technologies. VMs and containers are used in different use cases and each have challenges of their own – Salim Mahboubi Oct 06 '22 at 07:24