Wanna know some often overlooked good principles to containerise Node.js apps efficiently with
Docker?
- faster builds and when necessary
- smaller images for faster deploys and smaller cold starts
Read on

- faster builds and when necessary

- smaller images for faster deploys and smaller cold starts

Read on

Principle #1
Create a comprehensive .dockerignore file to ignore all the files that aren't necessary to your Docker image build (docs, node modules, config files for ESLint, Jest, etc.).
- better Docker layer caching
- fewer files sent to the build context
=> faster builds
Create a comprehensive .dockerignore file to ignore all the files that aren't necessary to your Docker image build (docs, node modules, config files for ESLint, Jest, etc.).
- better Docker layer caching
- fewer files sent to the build context
=> faster builds

Principle #2
Use an appropriate base image.
=> faster builds
=> smaller resulting Docker image
Compare:
node:14.15.0-alpine: ~40MB
node:14.15.0: ~345MB
(7 characters to save 300MB, not bad)
Use an appropriate base image.
=> faster builds

=> smaller resulting Docker image
Compare:
node:14.15.0-alpine: ~40MB

node:14.15.0: ~345MB

(7 characters to save 300MB, not bad)
Principle #1 and #2 are low hanging fruits.
Not much effort for big and immediate benefits.
Go do them now, I'll wait.
Here's a link if you need pointers https://www.robincussol.com/docker-for-js-devs-how-to-containerise-nodejs-apps-efficiently/#create-a-comprehensive-dockerignore-file
Not much effort for big and immediate benefits.
Go do them now, I'll wait.
Here's a link if you need pointers https://www.robincussol.com/docker-for-js-devs-how-to-containerise-nodejs-apps-efficiently/#create-a-comprehensive-dockerignore-file
Principle #3
Decouple 'npm install' from 'npm run build'
COPY package.json package-lock.json . # This line here
RUN npm install
COPY . .
RUN npm run build
The node modules are better cached and 'npm intall' runs only when necessary.
=> faster builds
Decouple 'npm install' from 'npm run build'
COPY package.json package-lock.json . # This line here
RUN npm install
COPY . .
RUN npm run build
The node modules are better cached and 'npm intall' runs only when necessary.
=> faster builds

Principle #4
Decouple the 'npm run build' output from the base files
Your app does not need the source files to run, so why keep them around to bloat our Docker image?
=> smaller final image
Decouple the 'npm run build' output from the base files
Your app does not need the source files to run, so why keep them around to bloat our Docker image?
=> smaller final image

Bonus
Purge node modules from unnecessary files to reclaim some MBs in your final image.
For example, by using `node-prune`.
=> smaller final image
Purge node modules from unnecessary files to reclaim some MBs in your final image.
For example, by using `node-prune`.
=> smaller final image

If that was too abstract, go read my article
Docker for JavaScript Devs: How to Containerise Node.js Apps Efficiently
It contains examples for each Principle above, a glossary of Dockerfile keywords as well as a checklist.
https://www.robincussol.com/docker-for-js-devs-how-to-containerise-nodejs-apps-efficiently/
That's it for this thread

It contains examples for each Principle above, a glossary of Dockerfile keywords as well as a checklist.
https://www.robincussol.com/docker-for-js-devs-how-to-containerise-nodejs-apps-efficiently/
That's it for this thread
