Selecting A Node.js Image for Docker
FROM node:x.x.x-(alpine|stretch|buster|bullseye)-slim WHAT???
Docker builds images automatically by reading the instructions from a Dockerfile
- a text file that contains all commands, in order, needed to build a given image. A Dockerfile
adheres to a specific format and set of instructions which you can find at Dockerfile reference.
FROM node:16
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
FROM
creates a layer from thenode:16
Docker image.COPY
adds files from your Docker client’s current directory.RUN
builds your application withmake
.CMD
specifies what command to run within the container.
FROM directive is probably the most crucial amongst all others for Dockerfiles. It defines the base image to use to start the build process. It can be any image, including the ones you have created previously.
If a FROM image is not found on the host, Docker will try to find it (and download) from the Docker Hub or other container repository. It needs to be the first command declared inside a Dockerfile.
Let's check supported tags and respective Dockerfile
links, and explain what are different.
FROM node:16-alpine:
Link: alpinelinux
Description: This image is based on the popular Alpine Linux project, available in the alpine
official image. Alpine Linux is much smaller than most distribution base images (~5MB) and thus leads to much slimmer images in general.
Package manager: apk
Shells: /bin/sh
Size: 110MB
FROM node:16-stretch:
Link: https://wiki.debian.org/DebianStretch
Description: stretch
is the development codename for Debian 9. Debian is a Linux distribution that’s composed entirely of free and open-source software.
Package manager: apt
Shells: /bin/bash
and many more
Size: 884MB
FROM node:16-buster:
Link: https://wiki.debian.org/DebianBuster
Description: buster
is the development codename for Debian 10
Package manager: apt
Shells: /bin/bash
and many more
Size: 851MB
FROM node:16-bullseye:
Link: https://wiki.debian.org/DebianBullseye
Description: bullseye
is the development codename for Debian 11. You can check what new in Bullseye
Package manager: apt
Shells: /bin/bash
and many more
Size: 885MB
FROM node:<version>:
Link: https://hub.docker.com/_/node
Description: This is the defacto image. If you are unsure about what your needs are, you probably want to use this one. It is designed to be used both as a throwaway container (mount your source code and start the container to start your app), as well as the base to build other images off of.
Some of these tags may have names like Bullseye, buster, or stretch in them. These are the suite code names for releases of Debian and indicate which release the image is based on. If your image needs to install any additional packages beyond what comes with the image, you’ll likely want to specify one of these explicitly to minimize breakage when there are new releases of Debian.
This tag is based on buildpack-deps
. buildpack-deps
is designed for the average user of Docker who has many images on their system. It, by design, has a large number of extremely common Debian packages. This reduces the number of packages that images that derive from it need to install, thus reducing the overall size of all images on your system.
Package manager: apt
Shells: /bin/bash
and many more
Size: 851MB
FROM node:<version>-slim:
This image does not contain the common packages contained in the default tag and only contains the minimal packages needed to run a node
. Unless you are working in an environment where only the node
image will be deployed and you have space constraints, we highly recommend using the default image of this repository.
Size: 155MB
The full version, however, adds a significant number of tools, including:
- source controls software, such as
git
,subversion
,bazaar
, andmercurial
- runtime libraries and build tools like make,
gcc
,g++
, and others - libraries and API sets such as
ImageMagick
, a dozen another lib* libraries), and others
Our Recommendation: Alpine Linux:
To minimize image size, it’s uncommon for additional related tools (such as git
or bash
) to be included in Alpine-based images. Using this image as a base, add the things you need in your Dockerfile
(see the alpine
image description for examples of how to install packages if you are unfamiliar).
Member discussion