Node.js

Docker: How to get a Node.js application into a Docker Container


Introduction

The goal of this article is to show you an example of dockerizing a Node js application. Where you can have a basic understanding of Docker. It will help you to set up the Node js application and docker installation.

What is Docker?

Docker is an open-source 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. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Why Docker?

Developing apps today requires so much more than writing code. Multiple languages, frameworks, architectures, and discontinuous interfaces between tools for each lifecycle stage create enormous complexity. Docker simplifies and accelerates your workflow while giving developers the freedom to innovate with their choice of tools. The day which comes in every developer’s life that application is working on our system, but It’s not working on the client’s system. To prevent this type of situation, we use Docker.

How can we use Docker with Nodejs?

Before starting, I am assuming that you have a working Docker installation and a basic understanding of how a Node.js application is structured.

In the first part of this video, 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.

Setup Nodejs Server

  1. Run command npm init and add the required details for your project
  2. Install express in the project using npm I express
  3. Then, create a server.js file that defines a web app using the Express.js framework:

Now we can test the node server, and start the application using node server.js. Let’s try to hit the URL http://localhost:8080/ and check the response

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 create a docker file, Where we are going to add some commands.

#Dockerfile

Creating a Dockerfile

Create a docker file in the root directory of the project using the touch command.

Edit the docker file using any editor and write the below instructions into the docker file.
Initially, we need to pick a node image that will run on a container, and here I am using the latest stable version of the node.

Next, we need to create a directory for the application. Here we can add all the project files.

This image comes with Node.js and NPM already installed, so the next thing we need to do is install your app dependencies using the npm install. So I am going to copy the package. JSON file.

To bundle your app’s source code inside the Docker image, use the COPY instruction:

Your app binds to port 8080 so you’ll use the EXPOSE instruction to have it mapped by the docker daemon:

Lastly, we are going to run the application using the CMD, which will execute node server.js

Final Output:

.dockerignore file

Creating a .dockerignore file in the root directory of the project

This will prevent your local modules and debug logs from being copied onto your Docker image and possibly overwriting modules installed within your image.

Build 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:

Now check the docker images

Here you can find all your images and their details.

Run the image

It’s time to run your image on a container

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.

Logs

Let’s check the container logs 

Here you can find your application logs

Test

To test your app, get the port of your app that Docker mapped:

In the example above, Docker mapped the 8080 port inside of the container to port 49160 on your machine.

Now we can call the rest endpoint using the curl command or any of the other methods

Finally, we have the response at the endpoint. 

I hope this article gives you exposure to docker and some basic concepts we used here.

Conclusion

I hope this article gives you exposure to docker and some basic concepts we used here.

For any questions and inquiries, visit us at thinkitive

Nilesh Bhopali

Sr. Software Engineer @Thinkitive

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button