Process to Build Node.js Application with Docker

It is possible for programmers to bundle and execute apps in containers using the Docker platform. Containers are a lower weight alternative to virtual machines since they are separated processes running on a shared operating system. Although containers are not new, they are becoming more important as more developers turn to distributed application architectures and benefit from their features such as process separation and environment uniformity. 

In order to construct and scale an application using Node.js docker, you normally start by generating a container image for your application. Everything you need to build and operate your application is included in the image. This guarantees that your container has a consistent environment and includes just what you need to create and operate your application. 

An application picture for a static website that utilizes the Express and Bootstrap frameworks will be created in this tutorial. Using this image, you will next create a container and upload it to node docker Hub. Last but not least, you’ll replicate and grow your application by grabbing the image from your Docker Hub repository and building another container. You can look for the best place to learn Node.js for more info.

Build Node.js App with Docker – Prerequisites

You’ll need the following items to follow this guide: 

This post will show you how to set up an Ubuntu 18.04 server from scratch. 

  • How To Install and Use node docker on Ubuntu 18.04 have been completed on your server. 
  • Go to the site https://nodejs.org/en/download/ and download all necessary files for the installation. 
  • Installed Node.js and npm by following the installation instructions for the NodeSource PPA. 
  • A Docker Hub account is required to use this service. The Docker ID must be included when creating a new docker user. The first member of the Owners team will have complete administrative ability to configure and operate the organization, and this user account will become the first member of that team. This introduction to getting started with Docker Hub will give you a good idea of how to go about doing that. 

What is Docker in Node.js?

 Docker in Node.js

Using Docker, you can build, ship and execute apps. In order to provide software rapidly, Docker lets you decouple your applications from your infrastructure. 

In the same manner that apps are managed, your infrastructure can now be managed using Docker as well. With Docker’s rapid shipping, testing, and deploying methods for code, you can drastically cut the time it takes to get new code into production. 

The purpose of this Dockerode example is to demonstrate how to run a Node.js application in a container on Docker. For development purposes only, this guide is recommended. A functional Docker installation and a rudimentary grasp of how Node.js applications are organized are also presupposed in this tutorial. 

An example Node.js web app will be built, a pm2 docker image will be created, and finally a container will be created from the image in the first section of this article. 

A container is a “box” that contains a program, its environment, and all of its dependencies. It’s common for a container to be made up of an application running on a stripped-down Linux OS. Containers are made up of images, while images are made up of containers. 

Using a container to package your Node.js application means that you may include everything you need to execute your web application on a variety of platforms and CPU architectures. A container image is an item that may be used to deliver these images. Node.js developers now have a means to execute the same project or product across a variety of platforms like Linux, fedora, centos etc. using these Docker images, which are software-based bundles. 

Last but not least, using node docker containers makes it easier to try out new platform releases or other modifications without having to worry about obtaining specific rights or creating a specialized environment. 

Steps to Build Node.js Application with Docker

Build Node.js Application with Docker

Create a new directory for the node-web app

  • Run the node web app 
  • Create an environment variable to hold data 
  • Make sure you install npm and express, a node.js framework, to get started. Once we’ve done that, we can start npm in our directory. 
  • Npm start up 
  • When using npm, an app’s dependencies are stored in a package.json file. Next, you’ll need to install the express framework. 
  • Node.js express is now installed. 

Result: 

  • node alpine, version 1.0.0; index.js; scripts; test; echo ‘Error: no test provided.’ && exit 1′; author, license, and dependencies all specify ‘ISC’; dependencies include: In the case of “express,” “devDependencies” are “4.17.1.” 
  • This is the “description,” which ixs:}When you’re done with this, you’ll have the Hello World site up and running. With the’require’ directive, express is loaded. “express” is the name of the variable to be used. 
  • the expression of the variable app ()Create a new root URL for the answer to a request (/)The app.get(‘/’, function (req, res).hello world! res.send(‘Hello!’)}) 
  • A listening server on port 8080 has been started. Using the function of ‘application.listen’ 
  • ‘app listening on port 8080!’: console.log})Start The Program 
  • The app is all set to go live: In the command line, type: $ node app.js Go to http://localhost:8080/ and log in To see it, open it in your browser. 

DockerFile Creation in Step Two

Create a file named Dockerfile in the root directory. 

First and foremost, we must decide on the picture from which we will construct. Version 9 of node, accessible from node docker, will be used in the following example: 

Node 9 in the chain of command. The next step is to set up your application’s working directory. 

# Set up a directory for your application software Apps are located in the WORKDIR The npm binary may be used to install the app’s dependencies. 

Install the app’s prerequisites. COPY the package*.json file to your computer’s hard drive. 

RUN NPM INSTALL 

The remainder of the NestJS docker program should be moved to the app folder. This command will copy the app’s data. 

Begin the application by exposing the port. The 8080 interface should be exposed. 

Run the following command: 

This is how your Dockerfile should look: 

Node 9 in the chain of command # Set up a directory for your application software. Apps are located in the WORKDIR. Install the app’s prerequisites. COPY the package*.json file to your computer’s hard drive. Also, a best online full stack developer course can give more accurate knowledge.

PUT IN A WORKSHOP 

In the app directory, copying the remainder of the program. COPY. /app to the clipboard. Start the program by exposing the port and launching it. The 8080 interface should be exposed. CMD [“npm”,”start”] is what you need to do. 

NOTE: To speed up the rebuild process, we utilized two separate COPY commands as seen above. 

the.dockerignore file. Avoid copying superfluous dockerize node app files to the container by using a.dockerignore file. 

node modules npm-debug.log 

The local module and debug logs will not be transferred to your Docker image if this option is enabled. 

Making a Docker Image is the third step

The process of creating a node docker image is simple and may be completed with only one command. 

Tags may be added to your picture using the -t parameter. 

For instance: 

run docker build -t cisca 

Result: 

  • Docker daemon: Sending build context (19.97kB) 
  • One seventh of the way through the process: node:9 
  • 9: Using a library or node as a source 
  • In order to finish the pull, type d660b1f15b9b. 
  • 46dde23c37b3: Completed a full pull 
  • 6ebaeb074589: Completed the pull 
  • It’s time to finish the pull. 
  • Completed by eda527043444 
  • It’s time to pull the whole thing out. 

The following command completes the pull: 1ded38ff7fdc 

  • completed with hash da44c9274f48 
  • The digest for this hash is as follows: sha256: cddc729f8326f7e8966c246ba2e87bad4c15365494f3d6f021cdab04 
  • Node:9 has an updated image that has been downloaded. 

The following code was entered: 08a8c8089ab1. 

  • Step 2/7: WORKDIR /app /. 
  • Cad30dfd3fdb is currently running. 
  • Cad30DfD3FDB intermediate container is removed. 
  • A unique hash of this hash is: 97CDF5DFBFD 
  • 3.7.3 COPY package*.json to / 
  • As a result, the corresponding number is: 
  • RUN the command npm install 4/7. 
  • Over the next 2e3c38322fd4 seconds 
  • 2e3c38322fd4 was removed as an intermediary container. 

 

This is the fifth of seven steps

In this case, the number is 33d29ad9ebe 

  • Exposition of 8080 at 6/7 
  • Invoking 8b2a54h557cc —-> 
  • 8b2a5ah557cc has been removed as an intermediary container. 
  • It’s now 13b95635c201 
  • On step 7/7, type CMD [npm start] in the command line. 
  • This program is now running in 50a5824edf5e. 

As an alternative to this, you may use the following: 

  • faa3092e5595 was successfully constructed. 
  • cisca:latest has been successfully tagged. 

After running the command, you should get something like the result seen above. This indicates that the docker image was successfully generated and that the app is functioning properly. 

It’s time to have a look at the final product: 

ls -a docker image 

Result: 

  • IMAGE ID CREATED SIZE FOR REPOSITORY TAG. 
  • faa3092e5595 is the most recent version of cisca. 4 minutes and 44 seconds ago 675MB 
  • The alpine node is node 12 alpine 08a8c8089ab1. Last month 673MB 

The fourth step is to start a container

We can now use the following command to execute the yarn docker image: 

It is possible to use the docker run command to execute a docker image on a certain host port and the specific Docker port. 

In order for the docker container to operate in the background, the -d option must be used. The -p option tells Docker to use a certain host port for communication. 

Running cisca using the docker node alpine run -d -p option is recommended. 

docker run -d -p <Host port>:<Docker port> <docker-image-name> 

Example: 

docker run -d -p 8080:8080 cisca 

Result: 

34fe3040ff6b9d1b593a3c5d650ef0f16d53a989fe41816634456dd7f1341183 

To check the running container, we can use docker ps command: 

docker ps –a 

Result: 

CONTAINER ID IMAGE COMMAND CREATED STATUS
34fe3040ff6b cisca "npm start" About a minute ago Exited (1) About a minute ago
PORTS   NAMES 
0.0.0.0:8080->8080/tcp intelligent_hofstadter 

Conclusion

Docker compose Node.js image creation and publication to the docker hub are much simpler than setting up a server. The most important thing is to learn and retain instructions. The Docker ecosystem is a great way to run our apps and lower the cost of our infrastructure. Also, if you are looking for the best place to learn, KnowledgeHut is the best place to learn Node.js .

Frequently Asked Questions (FAQs)

1. What is Node.js application?

Node.js (Node) is an open-source JavaScript server-side programming platform. A permanent connection between the browser and the server is required for services like chat, news feeds, and web push alerts to function. 

2. What is the use of Docker in Node.js?

A container is a “box” that contains a node alpine docker program, its environment, and all of its dependencies. It’s common for a container to be made up of an application running on a stripped-down Linux OS. 

3. How can I launch an application by using Docker?

  • Setup is the first step. 
  • Create a Dockerfile 
  • Create a Compose file with service definitions. 
  • Using Compose, build and test your app. 
  • To add a bind mount to the Compose file, edit it in this order: 
  • With Compose, build and test the app again. 
  • Make sure that the application is up to date. 

4. What is role of docker-compose build?

Docker-compose.yml files are created with this command. The build command’s goal is to prepare the images for creating containers, therefore it will bypass any services that are utilizing the prebuilt image. 

Leave a Reply

Your email address will not be published.