DEV Community

DhiWise
DhiWise

Posted on • Edited on

Dockerize your Node.js application in a few simple steps.

Docker Containers are widely adopted everywhere, in the Linx, Windows, Cloud, Datacenter, and Serverless, etc. The technology enables you to share and run your application quickly and reliably in different computing environments most efficiently and simply.

In this article, we are going to learn how to dockerize a Node.js application, but before that let’s understand what Docker technology is and its advantages.

What is Docker?

According to Docker’s official documentation, 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. With Docker, you can manage your infrastructure in the same ways you manage your applications.

Advantages of Using Docker

Docker technology helps to reduce the delay between writing code and running it in production by shipping, testing, and deploying code quickly.

It brings you many benefits as:

  1. Simplify application deployment and management.

  2. Makes your application portable across machines

  3. Facilitates version control and component reuse

  4. Sharing of image files/ docker files

  5. Lightweight footprint and less overhead

Prerequisites for Dockerizing Node.js application

  • Node.js application

  • npm

  • Docker

To understand how to dockerize the node.js application, first, we need to create a simple node.js app. Then we will create a Docker image of that application and run it.

Docker allows you to wrap an application with its environment along with its dependencies into a container. An image is a blueprint of that container and the container is the running instance of that image.

Setting up Node.js Express application

First, set up a basic Node.js Express **application and create a directory where all the files can be placed. Here create a **package.json file that describes your app and its dependencies.

Package.json

{

“name”: “node-docker_app”,

“version”: “1.0.0”,

“description”: “Node.js on Docker”,

“author”: “John Doe”,

“main”: “server.js”,

“scripts”: {

“start”: “node server.js”

},

“dependencies”: {

“express”: “⁴.16.1”

}

}
Enter fullscreen mode Exit fullscreen mode

With the above package.json file, run npm install and then create a server.js file to define your web application using the Express.js framework.

server.js

‘use strict’;

const express = require(‘express’);

// Constants

const PORT = 8080;

const HOST = ‘0.0.0.0’;

// App

const app = express();

app.get(‘/’, (req, res) => {

res.send(‘Hello World’);

});

app.listen(PORT, HOST);

console.log(`Running on [http://${HOST}:${PORT}`);](http://${HOST}:${PORT}`);)
Enter fullscreen mode Exit fullscreen mode

Next, to run this application inside the Docker container, we need to build the Docker image of the app. Also, we can use javascript bundler esbuildto bundle your application.

Create Node.js Dockerfile

Create an empty Dockerfile using the “touch” command as shown below.

touch Dockerfile
Enter fullscreen mode Exit fullscreen mode

In the file, you need to define for what image you are going to build from and create a directory to hold the application code inside the image. Then you need to install your app dependencies using npm.

Finally, bundle your app source code inside the Docker image using the “COPY” instruction and expose it to port 8080.

To run your application use the “CMD” command, here we are using node server.js to start your application.

The Dockerfile we have created will look like this:

Dockerfile

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 port

EXPOSE 8080

# Start the app

CMD [ “node”, “server.js” ]
Enter fullscreen mode Exit fullscreen mode

Ignoring File in the Container

As we don’t want some files such as local modules and debug logs being copied in our Docker image, we create “.dockerignore” file in our project directory as shown below.

.dockerignore

node_modules

npm-debug.log
Enter fullscreen mode Exit fullscreen mode

Building your image

Building and running the images will produce a container.

So, go to your project directory which has your Docker file, and run the following command to build the Docker image.

docker build -t <your username>/node-docker-app
Enter fullscreen mode Exit fullscreen mode

The -t flag here lets you tag your image, making it easy to search using **the **Docker images command.

Once the image is created its gets listed by the Docker.

Run and Test the image

Finally, run the image with the -d flag in the detached mode which will run the container in the background and print the container ID. Also, use the -p flag to map the container port from the local machine to the public port.

docker run -p 49160:8080 -d <your username>/node-docker-app
Enter fullscreen mode Exit fullscreen mode

Print the app output:

# Get container ID

$ docker ps

# Print app output

$ docker logs <container id>

# Example

Running on [http://localhost:8080](http://localhost:8080)
Enter fullscreen mode Exit fullscreen mode

To test your application first get the container ID

$ docker ps

# Example

ID IMAGE COMMAND … PORTS

ecce33b30ebf <your username>/node-docker-app:latest npm start … 49160->8080
Enter fullscreen mode Exit fullscreen mode

In the above snippet, you can see that the docker is mapped to port no 8080 inside the container to port 49160 on your local machine.

Now call your port using the “curl” command

$ curl -i localhost:49160

HTTP/1.1 200 OK

X-Powered-By: Express

Content-Type: text/html; charset=utf-8

Content-Length: 12

ETag: W/”c-M6tWOb/Y57lesdjQuHeB1P/qTV0"

Date: Tues, 30 Nov 2021 15:53:59 GMT

Connection: keep-alive

Hello world
Enter fullscreen mode Exit fullscreen mode

So this is all about running your Node.js application on Docker.

Summing Up:

In the article, we have learned about Docker technology, its advantages, and how to Dockerize a Node.js Express application.

With the above information, you will be able to take advantage of Docker to Dockerize and deploy your Node.js application to any platform.

Are you finding ways to make your app development faster while being up on the code quality, try DhiWise- A ProCode app development platform for web and mobile app development. Build any type of application with the tech stack you love.

Find out more about DhiWise and our open-source Node.js generator. Sign up today!

Happy Learning!

Top comments (0)