Dockerizing NodeJS App: Simpler Steps

Dockerizing NodeJS App: Simpler Steps

Minimum steps required for dockerizing a NodeJS application with express as its framework.

Docker allows us to build our own images using a file called Dockerfile . This Dockerfile contains instructions that Docker reads to build our images. These instructions are actually command-line instructions which Docker executes in succession. Further, the docker build command builds the image from the Dockerfile and a context. Context is the location of your app whether that be in your local filesystem or a remote repository.

We can exclude files and directories from our build using .dockerignore file much like the .gitignore file in git.

Note - This guide teaches how to build an image of only the NodeJS Application and not of any other application like a database. You would be needing docker-compose tool for that purpose but that is not the aim of this article. Please refer another article for it.

Create the Dockerfile

Open your NodeJS app's root directory. Mine looks like this.

image.png

Create a file and name it exactly like this - Dockerfile with D capital and no extension.

I will state and explain the instructions line by line as follows :-

  1. FROM node:latest - This instruction will pull node's latest image from dockerhub and be the base image of our application. Every docker image is built on top of another docker image. For example, the official node image in dockerhub has alpine as its base image.

  2. WORKDIR /usr/src/app - The running instance of an image i.e. the container has its own virtual linux filesystem. This instruction tells docker that our app will reside in that location and to execute the rest of the commands inside that directory.

  3. COPY . . - Copies all files from this (where the Dockerfile is in our local filesystem) to that (our working directory inside the container). While copying, we will be ignoring node_modules directory in our local filesystem for build's performance.

  4. RUN npm install - Installs all the dependencies stated in package.json file.

  5. CMD ["node", "server.js"] - Instruction to execute our node.js application. The entrypoint in my case is server.js.

The final Dockerfile should look like this

FROM node:latest

WORKDIR /usr/src/app

COPY . .

RUN npm install

CMD ["node", "server.js"]

Save the file.

Create .dockerignore file

We will be ignoring node_modules in our build just like how we ignore it in version control like git. To do this create a file name .dockerignore and just write node_modules inside it. Save it and the final root directory should look like this.

image.png

Execute docker build command

Open the terminal in the app's root directory and simply execute the following command.

docker build -t todos .
  • -t is the tag flag to mention the name of your image and tag. If tag is not specified, docker will assume latest as its value.
  • todos is the name of my image.
  • . is the path of my Dockerfile. Since I opened the terminal in the same directory, it will be a dot.

Docker will take few seconds to minutes to build your image. After its done you can check your image using docker images command. You should see your image with the name and tag you specified.

Run the image

To start a container of your image and expose the port on which your node.js application is listening, execute the following command.

docker run -p 6000:6000 todos

Note - The port my node.js application was listening is 6000. Change it according to your application.

Congratulations

That's it! If you can see your container running and successfully send requests to it, you have built your docker image. For more articles on docker and its tool such as docker-compose, visit my blog