Best way to build an image

I have just started to learn Docker. I have a nodeJs web application and built 2 images of it using 2 different Dockerfile. The 2 images are really different in size. Can someone tell me why they are really different in size even though they use the same alpine, node, and npm version, and which one is the recommended way to build a nodeJS application image?

First Dockerfile which created a 91.92MB image:

FROM alpine
RUN apk add --update nodejs npm curl
COPY . /src
WORKDIR /src
RUN npm install
EXPOSE 8080
ENTRYPOINT ["node", "./app.js"]

Second Dockerfile which created a 259.57MB image:

FROM node:19-alpine3.16

RUN apk add --update npm curl

COPY . /src

WORKDIR /src

RUN npm install

CMD [ "node", "./app.js" ]

The difference in size between the two Docker images can be attributed to the base image used and the way the images are built. In the first Dockerfile, the base image used is Alpine, which is a lightweight Linux distribution. This means that the final image will only contain the essential packages required to run the application. On the other hand, in the second Dockerfile, the base image used is a pre-built Node.js image that includes many other packages and dependencies. This could be the reason why the second image is larger.

In terms of the recommended way to build a Node.js application image, it depends on the specific requirements of the application. If the application has minimal dependencies and requires a lightweight runtime environment, using a minimal base image like Alpine would be a good option. However, if the application has complex dependencies and requires a more comprehensive runtime environment, using a pre-built Node.js image would be a better option.

In summary, the choice of the base image and the way the Docker images are built can have a significant impact on the final image size. It’s important to choose the right base image and optimize the Dockerfile to create a lightweight and efficient Docker image for the application.

Hello,

As per my personal experience, The significant difference in size between the two Docker images can be attributed to the base images used and the steps performed in each Docker file.

In the first Dockerfile, you used a minimal Alpine image as the base image. Alpine Linux is known for its small size, which contributes to a smaller resulting image. However, the image size increases as you install additional packages. In this case, you added Node.js and npm to the Alpine image, which increased its size to 91.92MB.

In the second Dockerfile, you used the official Node.js Docker image as the base image. This image already includes Node.js and npm, so you didn’t need to install them separately. The node:19-alpine3.16 image is larger compared to the Alpine base image, which is why the resulting image size increased to 259.57MB.

To build a more optimized and smaller image, it’s generally recommended to use a minimal base image like Alpine and only include the necessary dependencies for your application. In your case, the first Dockerfile seems to follow this approach, resulting in a smaller image size.

However, if the larger image size of the second Dockerfile is not a concern for your application, you can still use the official Node.js Docker image. It provides a more straightforward setup, as Node.js and npm are already included.

In the end, if minimizing the image size is a priority, the first Dockerfile using the Alpine base image and installing Node.js and npm manually would be the recommended approach. But if image size is not a major concern for your specific use case, the second Dockerfile with the official Node.js Docker image can simplify your setup.

I hope this will help you.