Dockerizing a Node.js web application

The goal of this is to show you how to get a Node.js application into a Docker container. In the first part of this guide, we will create a simple web application in Node.js, then we will build a Docker image for that application, and lastly, we will instantiate a container from that image.

An image is a blueprint for a container, a container is a running instance of an image normally. Let’s try to dockerize a node.js application step by step.

1. Create the Node.js app

First, open visual studio code and create and open a new folder to the making the application. Then create a new directory which contains package.json the file that describes your app and its dependencies.

With your new package.json file, run npm install. If you are using npm version 5 or later, this will generate a package-lock.json file which will be copied to your Docker image.

Then, create a server.js the file that defines a web app using the Express.js framework.

In the next steps, we’ll look at how you can run this app inside a Docker container using the official Docker image. First, you’ll need to build a Docker image of your app.

2. Creating a Dockerfile

Create an empty file called Dockerfile.Then need to do is define what image we want to build from. Here we have to select node.js image from the docker hub. Then we have to add the following commands to the Dockerfile.

3. Building your image

Go to the directory that has your Dockerfile and run the following command to build the Docker image. The -t flag lets you tag your image so it's easier to find later using the docker images command.

docker build . -t hello-docker-world

Then your image will now be listed by Docker

$ docker images hello-docker-world

# Example
REPOSITORY TAG ID CREATED
node 10 1934b0b038d1 5 days ago
<your username>/node-web-app latest d64d3505b0d2 1 minute ago

4. Run the image

Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container. Run the image you previously built (port Mapping)

docker run --name c1 -p 80:8080 -d hello-docker-world

Then it's creating a container for this node image. We can see the running containers through the below command.

docker ps

Also, we can get and print outputs of your dockerized node js application from the following commands.

# Get container ID
$ docker ps

# Print app output
$ docker logs <container id>

In the end, we can get the hello world output in the localhost:80 port listening.

# Example
Running on http://localhost:8080

If you need to go inside the container you can use the exec command:

# Enter the container
$ docker exec -it <container id> /bin/bash

Through these steps, we can dockerize any image in the docker hub easily.

3rd Year Undergraduate, University of Moratuwa.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store