How To Containerized Node-Js Application.

Riyazur Razak
3 min readSep 29, 2021

--

Photo by Ian Taylor on Unsplash

What is Docker?

Docker is a container management system. Then What are containers? Containers are the ideology that develops once, ship everywhere. Containers are very useful to us when moved from development to production. Once you containerized an application, you can deploy/run it everywhere.

The key points are first we create an image for our application, then we run it as a container.

How To containerized?

For containerized an application we need a Dockerfile. Dockerfile is the root or entry point for docker to tell how to build an image.

Let's Start creating an empty express server using node js.

index.js is a root file for our application. On the Same Directory Create a Dockerfile (no extension).

Let’s breakdown it

FROM is a starting keyword in Dockerfile. We want to declare from which base image we want to create our image. For a node-js application, we use node-alpine. (alpine is a lighter version provide for docker base images). There are lots and lots of images that are publicly available in the docker hub.

Next, We instruct docker to change the working directory to the app directory. If it’s not available, docker will create it for us.

Further, we instruct docker to copy all the package.json files from our current directory (make sure your Dockerfile is available at the same level as package.json is available.

Next, We Run usually npm install the command. (We use Node js as our base image so npm is already installed under the hood)

Next, We COPY all the source files from our working directory to the docker image.

Finally, The CMD is for executing the image. CMD should be only one time for a Dockerfile.

Finally, We Created our docker image. Next, We learn How to build an image.

Build An Image

docker image build . -t exampleapp:v0.1

Execute the above command in the same directory where the Dockerfile is available. Otherwise, You can specify the path of the Dockerfile. The -t flag is for assign a name and tag for our image.

Congratulations, We successfully build our image.

Now We are going to run the image as a container. For that, we are using the docker run command.

docker run -p 80:3000 exampleapp:v0.1

Assume that our express app opens on port 3000 in our container. We map our container to our host machine port 80.

Tada. We Containerized our node application using docker. Open your browser on localhost. Your app is running.

Still can’t understand the use of Docker?

I Tell Some live tricks. Now Take another machine and it must not install node js. Without Containerisation the node js app won't run without installing it on our machine. Now just install docker in that machine and build the image and run it as a container. check your browser, Tada, It will run even without node js is configured in your machine.

Hope You enjoyed this and something to learn new. See you on another blog.

--

--

Riyazur Razak
Riyazur Razak

Written by Riyazur Razak

Full Stack Web Developer, Student

No responses yet