Member-only story
Lightweight and Performance Dockerfile for Node.js
How to create lightweight and performance Dockerfile especially for Node.js applications.
Hello World! Today I’m about to share my notes and thoughts about Dockerfile
for Node.js
applications. I hope you will find it useful and create Dockerfile
, which will suit all your needs.
Docker Introduction
If you are here it means, that you have faced Docker
before, but to be sure that we are on the same page let’s remind what it is.
So, there are no secrets — Dockerfile
is an instruction of how our image will be built. Step by step, layer by layer this file describes how our image will look like.
Okay, so another question — what is an image? We can treat it as a template. Once you build an image from your Dockerfile
, it can be shared with other people. You can run a container from an image.
The last but not the least — container, it’s a running image, the actual process.
Why do we need this system? It solves the particular problem — environment. You can run a container where you need to, regardless of your OS. So, as you can see everything begins from Dockerfile
.
FROM
First things first — FROM
. The command, which describes what will be used as a base for the container. We should keep in mind that we are building Node.js
Dockerfile
since we discuss it in the context of our article. Usually, you can meet the next approach:
FROM node
Also common are the next images:
FROM node:slim
and
FROM node:alpine
but I prefer another one:
FROM alpine
Node
, node:slim
, node:alpine
and alpine
are official images. You can read more about them on official pages. Okay, so what for and why? The reason here is the weight. So, if we use anode
image, we get 900+ Mb from the start, node:slim
gives 160 Md, node:alpine
— 109 Mb and at the same time only 5 Mb for alpine
.
node