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

Optimize the Dockerfile in your Node.js TypeScript Project
1.35 GEEK