Docker Image Build Process for Novice

ABHISHEK ASARAWA
Geek Culture
Published in
4 min readJul 6, 2021

We will learn how to create docker image of Node.js app/service. There are lots of article already on this topic but they have lack of simplicity. When I went through them to create my first docker image. I realized they are complicated. But making a docker image of Node.js app is not as tough as it seems.

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.

I am assuming that you already have knowledge of working in Node.js. Here I will not go through creating a Node.js project. You can refer this repo for code.

When you start learning Docker, two main terms come up

  1. Image
  2. Container

In this article, we will learn about Images.

What is Docker Image?

A docker image is collection of files that bundle together all essentials (installations, application code and dependencies) required to configure a fully operational container environment. In simple words, it bundle all files necessary to run a container.

For creating docker image, docker needs a manual. This manual is called Dockerfile. Developer just needs to create this. Sounds cool!

Lets create a Dockerfile for our case.

Creating Dockerfile

Dockerfile contains all steps and commands necessary to create an image.

I know what are you thinking… only six lines! Creating a docker image is not hard. But deciding about these lines and optimizing docker image take time. So, lets understand the lines one by one.

1. Base Image

First line in Dockerfile mention a base image. Base image also called as Parent Image. Initially docker container is empty. It does not have any software present in it.

Base image include all necessary software that need to run a container.

In our case, node:alpine will make sure that we can run a node app, so it will add all necessary files/software needed for node application. So we don`t have to worry about anything.

There are many versions of node base image. You can select one suitable for your project. You can check node base images here.

Why did I use node:alpine ?

In docker, it is necessary to keep your image as small as possible. node:apline remove all unnecessary files/software. It reduce node image size significantly. It is also optimized production grade image for node applications. For knowing more about alpine images you can refer this article. Lets go to second line in file.

2. Working Directory

WORKDIR command define a working directory in docker container. If the WORKDIR command is not written in the Dockerfile, it will automatically be created by the Docker compiler. Any RUN, CMD, COPY, ADD and ENTERYPOINT command will be executed to specified working directory. It is considered good practice to define a work directory for your app. Here, we have specified /app as a working directory.

From line third to fifth commands are interconnected. They are related to moving our project code into container.

3. Coping Files into Docker

For different languages moving project into docker is different. In node.js, we will first copy package.json file to docker then after installing all packages we will copy other files of app. why ?

I am sure you will be thinking why to increase lines ? why not copy all files together and then install packages. This will remove one extra line. Let me answer that quickly.

For every line written in Dockerfile, docker build an intermediate image and save it in cache.

It do this so next time if you build image it will just fetch these images and use it. This make building images really fast. So here is the catch, docker note the lines and there order. And if any line changes, docker will create intermediate images for this line and lines after that.

We continuously update/change app files but we rarely change pakcage.json. So, if I copy all files at once then on every change in code will trigger installing packages again even if package.json is not changed and this will make building docker image slow.

To solve this problem we follow these steps

  1. Copy package.json
  2. Install all packages
  3. Copy other code files

Last but not least…

4. Command to Run Container

CMD used to run software contained in docker image. For node application as we have entry point at index.js, we will use command node index.js. This will start our app as soon as container start.

CMD should follow format CMD [“executable”, “param1”, “param2”…].

We now understand about every line in Dockerfile. But this is just a recipe not a dish. They are all steps required to build image that we want. So now lets see how to build the image which is really simple.

Building Docker Image

For building docker image we use following command.

docker build .

Docker build command need a Dockerfile to build image. You must use above command at directory level where you created Dockerfile.

Docker gives bunch of parameters to use with docker build command. Explaining those commands will go over the scope of this article. You can see them here.

--

--

ABHISHEK ASARAWA
Geek Culture

Software developer. Working on tech stacks like Node.js, Python, Docker, AWS and Firebase. Learning about microservices architecture.