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:
Screenshot-from-2022-05-24-18-17-47-2 Docker: How to get a Node.js application into a Docker Container

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

gpw0BYJjCDwO9HdJqu3Nc7px74M85NZ0tHeo4e1nPIWc1d_PvWT1VRFjM57-WpCVDvQ76-oIenA8uF8h7_6dqB_Up06Bp6i8BmUZqIB0HyN3PAfa8JqFrkdguiuqKlLyPe4Nme7yPQyMPWnbTg Docker: How to get a Node.js application into a Docker Container

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.

Screenshot-from-2022-05-27-00-07-43 Docker: How to get a Node.js application into a Docker Container

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.

Screenshot-from-2022-05-27-00-12-13-2 Docker: How to get a Node.js application into a Docker Container

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

Screenshot-from-2022-05-27-00-42-51 Docker: How to get a Node.js application into a Docker Container

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.

Screenshot-from-2022-05-27-00-45-13 Docker: How to get a Node.js application into a Docker Container

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

Screenshot-from-2022-05-27-00-50-41 Docker: How to get a Node.js application into a Docker Container

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

Screenshot-from-2022-05-27-00-52-40-1 Docker: How to get a Node.js application into a Docker Container

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

Screenshot-from-2022-05-27-00-59-32 Docker: How to get a Node.js application into a Docker Container

Final Output: 

Screenshot-from-2022-05-27-01-15-20 Docker: How to get a Node.js application into a Docker Container
Screenshot-from-2022-05-27-01-08-33-2 Docker: How to get a Node.js application into a Docker Container

.dockerignore file

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

Screenshot-from-2022-05-27-01-19-32 Docker: How to get a Node.js application into a Docker Container

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:

Screenshot-from-2022-05-27-01-24-37 Docker: How to get a Node.js application into a Docker Container

Now check the docker images

IZGKW0HIV39CjgtAQNY9dj5n709X_Kw0gPpWI3gCAMIN_3shUeVLf4ZgTFX3V7DFRxR4DcGdZ1-kv04yJgQ0oqbHM0K2zDK-rLt2evdh9zRIs1QaaoEL0crSZgJSY3zY__EtA4nObZx4KqjzaA Docker: How to get a Node.js application into a Docker Container

Here you can find all your images and their details.

Run the image

It’s time to run your image on a container

Screenshot-from-2022-05-27-01-27-03-1 Docker: How to get a Node.js application into a Docker 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 

Screenshot-from-2022-05-27-01-37-46-1 Docker: How to get a Node.js application into a Docker Container

Here you can find your application logs

Test

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

gp2_DEvi2ilLTGY7nqTf58dq_Dv-Zbk3EFJGZngk7yOINUZ4C7jxW0O_zKByMJqdJUi49aoYeIiV2w5edYxfIsnqNgX_weN8VFeidcdpfxIA_0wfpKyeZ6WQ-5H0XYLyEEuUnXgRtJo-LBoYcg Docker: How to get a Node.js application into a Docker Container

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

pZXwzmnXaO4CsHIkW4-3N0yYsZgi-bKcSC_bW2Di7rIMRg7viIq5LCr3PfmklLx9FtFzpL7T70-cha2JDf-sTvRyYUBFoia043Dtn5-la_VLbV0gAE-JE-Iz34qzYzasllHwomzuik_nc4a5cw Docker: How to get a Node.js application into a Docker Container

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, including web development, or if you’re looking to hire Node.js developers, we invite you to visit us at Thinkitive.

Nilesh Bhopali

Sr. Software Engineer @Thinkitive

Related Articles

One Comment

Leave a Reply

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

Back to top button