Optimize the Dockerfile in your Node.js TypeScript Project

Optimize the Dockerfile in your Node.js TypeScript Project

Making optimized Docker image for an highly efficient workflows and faster builds. Optimize the Dockerfile in your Node.js TypeScript Project. OK, to start from positive side, if you will try just copying over some basic Dockerfile example for building Node.js based microservice.

Okay, to start from positive side, if you try just copying over some basic Dockerfile example for building Node.js based microservice container, it would be fine. If you need something small and portable you can just use node:lts-alpine docker base image and go with it by building this kind of simple Dockerfile:

FROM node:lts-alpine
ADD . /app
WORKDIR /app

RUN yarn && yarn build
ENV NODE_ENV=production
CMD ['node', './dist']

If you have a Node.js TypeScript project like I have here  https://github.com/tigranbs/node-typescript-starter-kit this will work perfectly fine, especially if alpine itself is quite small base image, the only storage that you have to acquire is your node_modules, which of-course could grow to few GB's if you have even 20+ packages in your dependencies.

In microservice world it is considered a good practice of having small Docker images for faster deployments and faster uptimes on server side. This is also means if you have for example a staging environment or Github Actions for running your automated tests, it will take a lot less time if you have smaller images with a less build times. So, the idea of having an optimised Docker image comes down to this following 2 points

  • Keep Image size as small as possible
  • Make sure to keep unique Docker Image layers for not rebuilding entire image layers all the time when you have a slight change

For the 1st point we already have pretty match the bare minimum Docker image, which means we probably can remove some unnecessary files from Dockerfile like README.md, .git, etc... , but overall it wouldn't make that match of a difference.

devops typescript programming node docker

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Node.js Live | Node.js Docker Tutorial | Dockerizing Node.js App|Node.js Training|Edureka

🔥 Node.js Certification Training: https://www.edureka.co/nodejs-certification-training This Edureka video on 'Node.js Docker Tutorial' will help you in learn...

Docker And JFrog Partnership: Path To Limitless DevOps in the Cloud

This article talks about the recent partnership between Docker and JFrog and how this partnership impacts developers and enterprises positively? The whole DevOps world works involving containers in building, packaging, running and shipping their software applications.

Ever Wondered Why We Use Containers In DevOps?

At some point we've all said the words, "But it works on my machine." It usually happens during testing or when you're trying to get a new project set up. Sometimes it happens when you pull down changes from an updated branch.

Docker manifest - A peek into image's manifest.json files

The docker manifest command does not work independently to perform any action. In order to work with the docker manifest or manifest list, we use sub-commands along with it. This manifest sub-command can enable us to interact with the image manifests. Furthermore, it also gives information about the OS and the architecture, that a particular image was built for. The image manifest provides a configuration and a set of layers for a container image. This is an experimenta

Docker Explained: Docker Architecture | Docker Registries

Following the second video about Docker basics, in this video, I explain Docker architecture and explain the different building blocks of the docker engine; docker client, API, Docker Daemon. I also explain what a docker registry is and I finish the video with a demo explaining and illustrating how to use Docker hub.