Use Docker to Create a Node Development Environment

In this tutorial, instead of creating and running a Node app locally, you’ll to take advantage of the Debian Linux operating system that official Docker Node images are based on. You’ll create a portable Node development environment that solves the “But it runs on my machine” problem that constantly trolls developers since containers are created predictably from the execution of Docker images on any platform.

Throughout this tutorial, you’ll be working in two realms:

  • Local Operating System: Using a CLI application, such as Terminal or PowerShell, you’ll use a local installation of Docker to build images and run them as containers.

  • Container Operating System: Using Docker commands, you’ll access the base operating system of a running container. Within that context, you’ll use a container shell to issue commands to create and run a Node app.

The container operating system runs in isolation from the local operating system. Any files created within the container won’t be accessible locally. Any servers running within the container can’t listen to requests made from a local web browser. This is not ideal for local development. To overcome these limitations, you’ll bridge these two systems by doing the following:

  • Mount a local folder to the container filesystem: Using this mount point as your container working directory, you’ll persist locally any files created within the container and you’ll make the container aware of any local changes made to project files.

  • Allow the host to interact with the container network: By mapping a local port to a container port, any HTTP requests made to the local port will be redirected by Docker to the container port.

To see this Docker-based Node development strategy in action, you’ll create a basic Node Express web server. Let’s get started!

Removing the Burden of Installing Node

To run a simple “Hello World!” Node app, the typical tutorial asks to:

  • Download and install Node
  • Download and install Yarn
  • To use different versions of Node, uninstall Node and install nvm
  • Install NPM packages globally

Each operating system has its own quirks making the aforementioned installations non-standard. However, access to the Node ecosystem can be standardized using Docker images. The only installation requirement for this tutorial is Docker. If you need to install Docker, choose your operating system from this Docker installation document and follow the steps provided.

Similar to how NPM works, Docker gives us access to a large registry of Docker images called Docker Hub. From Docker Hub, you can pull and run different versions of Node as images. You can then run these images as local processes that don’t overlap or conflict with each other. You can simultaneously create a cross-platform project that depends on Node 8 with NPM and another one that depends on Node 11 with Yarn.

Creating the Project Foundation

To start, anywhere in your system, create a node-docker folder. This is the project directory.

With the goal of running a Node Express server, under the node-docker project directory, create a server.js file, populate it as follows and save it:

// server.js
const express = require("express");
const app = express();

const PORT = process.env.PORT || 8080;

app.get("/", (req, res) => {
  res.send(`
    <h1>Docker + Node</h1>
    <span>A match made in the cloud</span>
  `);
});

app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}...`);
});


A Node project needs a package.json file and a node_modules folder. Assuming that Node is not installed in your system, you’ll use Docker to create those files following a structured workflow.

Accessing the Container Operating System

You can gain access to the container OS with any of the following methods:

  • Using a single but long docker run command.
  • Using a Dockerfile combined with a short docker run command.
  • Using a Dockerfile in combination with Docker Compose.

Using a single docker run command

Execute the following command:

docker run --rm -it --name node-docker \
-v $PWD:/home/app -w /home/app \
-e "PORT=3000" -p 8080:3000  \
-u node node:latest /bin/bash


Let’s breakdown this docker run command to understand how helps you access the container shell:

docker run --rm -it


docker run creates a new container instance. The [--rm](https://docs.docker.com/engine/reference/run/#clean-up---rm "--rm") flag automatically stops and removes the container once the container exits. The combined -i and -t flags run interactive processes such as a shell. The -i flag keeps the STDIN (Standard Input) open while the -t flag lets the process pretend it is a text terminal and pass along signals.

Think of --rm as “out of sight, out of mind”.

Without the -it team, you won’t see anything on the screen

docker run --rm -it --name node-docker


The [--name](https://docs.docker.com/engine/reference/run/#name---name "--name") flag assigns a friendly name to the container to easily identify it in logs and tables. For example when you run [docker ps](https://docs.docker.com/engine/reference/commandline/ps/ "docker ps").

docker run --rm -it --name node-docker \
-v $PWD:/home/app -w /home/app


The [-v](https://docs.docker.com/engine/reference/run/#volume-shared-filesystems "-v") flag mounts a local folder into a container folder using this mapping as its argument:

<HOST FOLDER RELATIVE PATH>:<CONTAINER FOLDER ABSOLUTE PATH>

An environmental variable can print the current working directory when the command is executed: $PWD on Mac and Linux and $CD on Windows. The [-w](https://docs.docker.com/engine/reference/run/#workdir "-w") flag sets the mounting point as the container working directory.

docker run --rm -it --name node-docker \
-v $PWD:/home/app -w /home/app \
-e "PORT=3000" -p 8080:3000


The [-e](https://docs.docker.com/engine/reference/run/#env-environment-variables "-e") flag sets an environmental variable PORT with a value of 3000. The [-p](https://docs.docker.com/engine/reference/run/#expose-incoming-ports "-p") flag maps a local port 8080 to a container port 3000 to match the environmental variable PORT that is consumed within server.js:

const PORT = process.env.PORT || 8080;


docker run --rm -it --name node-docker \
-v $PWD:/home/app -w /home/app \
-e "PORT=3000" -p 8080:3000  \
-u node node:latest /bin/bash


For security and to avoid file permission problems, the [-u](https://docs.docker.com/engine/reference/run/#user "-u") flag sets the non-root user node available in the Node image as the user that runs the container processes. After setting the flags, the image to execute is specified: node:latest. The last argument is a command to execute inside the container once it’s running. /bin/bashinvokes the container shell.

If the image is not present locally, Docker issues docker pull in the background to download it from Docker Hub.

Once the command executes, you’ll see the container shell prompt:

node@<CONTAINER ID>:/home/app$


Before moving to the next method, exit the container terminal by typing exit and pressing <ENTER>.

Using a Dockerfile

The docker run command from the previous section is made of image build time and container runtime flags and elements:

docker run --rm -it --name node-docker \
-v $PWD:/home/app -w /home/app \
-e "PORT=3000" -p 8080:3000  \
-u node node:latest /bin/bash


Anything related to image build time can be defined as a custom image using a Dockerfile as follows:

  • FROM specifies the container base image: node:latest
  • WORKDIR defines -w
  • USER defines -u
  • ENV defines -e
  • ENTRYPOINT specifies to execute /bin/bash once the container runs

Based on this, under the node-docker project directory, create a file named Dockerfile, populate it as follows, and save it:

FROM node:latest

WORKDIR /home/app
USER node
ENV PORT 3000

EXPOSE 3000

ENTRYPOINT /bin/bash


EXPOSE 3000 documents the port to expose at runtime. However, container runtime flags that define container name, port mapping, and volume mounting still need to be specified with docker run.

The custom image defined within Dockerfile needs to be built using [docker build](https://docs.docker.com/engine/reference/commandline/build/ "docker build") before it can be run. In your local terminal, execute:

docker build -t node-docker .


docker build provides your image the friendly name node-docker using the -t flag. This is different than the container name. To verify that the image was created, run docker images.

With the image built, execute this shorter command to run the server:

docker run --rm -it --name node-docker \
-v $PWD:/home/app -p 8080:3000 \
node-docker


The container shell prompts comes up with the following format:

`node@:/home/app<div id=“blog-post-content” class=“entry-content js-entry-content” itemprop=“articleBody”

Once again, before moving to the next method, exit the container terminal by typing exit and pressing <ENTER>.

Using Docker Compose

For Linux, Docker Compose is installed separately.

Based on the Dockerfile and the shorter docker run command of the previous section, you can create a Docker Compose YAML file to define your Node development environment as a service:

Dockerfile

FROM node:latest

WORKDIR /home/app
USER node
ENV PORT 3000

EXPOSE 3000

ENTRYPOINT /bin/bash


Command

docker run --rm -it --name node-docker \
-v $PWD:/home/app -p 8080:3000 \
node-docker


The only elements left to abstract from the docker run command are the container name, the volume mounting, and the port mapping.

Under the node-docker project directory, create a file named docker-compose.yml, populate it with the following content, and save it:

version: "3"
services:
  nod_dev_env:
    build: .
    container_name: node-docker
    ports:
      - 8080:3000
    volumes:
      - ./:/home/app


  • nod_dev_env gives the service a name to easily identify it
  • build specifies the path to the Dockerfile
  • container_name provides a friendly name to the container
  • ports configures host-to-container port mapping
  • volumes defines the mounting point of a local folder into a container folder

To start and run this service, execute the following command:

docker-compose up


up builds its own images and containers separate from those created by the docker run and docker buildcommands used before. To verify this run:

docker image
# Notice the image named <project-folder>_nod_dev_env
docker ps -a
# Notice the container named <project-folder>_nod_dev_env_<number>


up created an image and a container but the container shell prompt didn’t come up. What happened? up starts the full service composition defined in docker-compose.yml. However, it doesn’t present interactive output; instead, it only presents static service logs. To get interactive output, you use docker-compose run instead to run nod_dev_env individually.

First, to clean the images and containers created by up, execute the following command in your local terminal:

docker-compose down


Then, execute the following command to run the service:

docker-compose run --rm nod_dev_env


The run command acts like docker run -it; however, it doesn’t map and expose any container ports to the host. In order to use the port mapping configured in the Docker Compose file, you use the --service-ports flag. The container shell prompt comes up once again with the following format:

node@<CONTAINER ID>:/home/app$

If for any reason the ports specified in the Docker Compose file are already in use, you can use the --publish, (-p) flag to manually specify a different port mapping. For example, the following command maps the host port 4000 to the container port 3000:

docker-compose run --rm -p 4000:3000 nod_dev_env

Installing Dependencies and Running the Server

If you don’t have an active container shell, using any of the previous section methods to access it.

In the container shell, initialize the Node project and install dependencies by issuing the following commands (if you prefer, use npm):

yarn init -y
yarn add express
yarn add -D nodemon


Verify that package.json and node_modules are now present under your local node-docker project directory.

[nodemon](https://github.com/remy/nodemon "nodemon") streamlines your development workflow by restarting the server automatically anytime you make changes to source code. To configure nodemon, update package.json as follows:

{
  // Other properties...
  "scripts": {
    "start": "nodemon server.js"
  }
}


In the container shell, execute yarn start to run the Node server.

To test the server, visit [http://localhost:8080/](http://localhost:8080/ "http://localhost:8080/") in your local browser. Docker intelligently redirects the request from the host port 8080 to the container port 3000.

To test the connection of local file content and server, open server.js locally, update the response as follows and save the changes:

// server.js

// package and constant definitions...

app.get("/", (req, res) => {
  res.send(`
    <h1>Hello From Node Running Inside Docker</h1>
  `);
});

// server listening...


Refresh the browser window and observe the new response.

Modifying and Extending the Project

Assuming that Node is not installed in your local system, you can use the local terminal to modify project structure and file content but you can’t issue any Node-related commands, such as yarn add. As the server runs within the container, you are also not able to make server requests to the internal container port 3000.

In the event that you want to interact with the server within the container or modify the Node project, you need to execute commands against the running container using [docker exec](https://docs.docker.com/engine/reference/commandline/exec/ "docker exec") and the running container ID. You don’t use docker run as that command would create a new isolated container.

Getting the running container ID is easy.

  • If you already have a container shell open, the container ID is present in the shell prompt:
    node@:/home/app$

  • You can also get the container ID programmatically using docker ps to filter containers based on name and return the CONTAINER ID of any match:

docker ps -qf "name=node-docker"


The -f flag filters containers by the name=node-docker condition. The -q (–quiet) limits the output to only display the ID of the match, effectively plugging the CONTAINER ID of node-docker into the docker exec command.

Once you have the container ID, you can use docker exec to:

  • Open a new instance of the running container shell:
docker exec -it $(docker ps -qf "name=node-docker") /bin/bash


  • Make a server request using the internal port 3000:
docker exec -it $(docker ps -qf "name=node-docker") curl localhost:3000


Install or remove dependencies:

docker exec -it $(docker ps -qf "name=node-docker") yarn add body-parser


One you have another active container shell, you can easily run curl and yarn add there instead.

Recap… and Uncovering Little Lies

You’ve learned how to create an isolated Node development environment through different levels of complexity: by running a single docker run command, using a Dockerfile to build and run a custom image, and using Docker Compose to run a container as a Docker service.

Each level requires more file configuration but a shorter command to run the container. This is a worthy trade-off as encapsulating configuration in files makes the environment portable and easier to maintain. Additionally, you learned how to interact with a running container to extend your project.

You may still need to install Node locally for IDEs to provide syntax assistance, or you can use a CLI editor like vimwithin the container.

Even so, you can still benefit from this isolated development environment. If you restrict project setup, installation, and runtime steps to be executed within the container, you can standardize those steps for your team as everyone would be executing commands using the same version of Linux. Also, all the cache and hidden files created by Node tools stay within the container and don’t pollute your local system. Oh, and you also get yarn for free!

JetBrains is starting to offer the capability to use Docker images as remote interpreters for Node and Python when running and debugging applications. In the future, we may become entirely free from downloading and installing these tools directly in our systems. Stay tuned to see what the industry provides us to make our developer environments standard and portable.

#docker #node-js

What is GEEK

Buddha Community

Use Docker to Create a Node Development Environment
Aria Barnes

Aria Barnes

1622719015

Why use Node.js for Web Development? Benefits and Examples of Apps

Front-end web development has been overwhelmed by JavaScript highlights for quite a long time. Google, Facebook, Wikipedia, and most of all online pages use JS for customer side activities. As of late, it additionally made a shift to cross-platform mobile development as a main technology in React Native, Nativescript, Apache Cordova, and other crossover devices. 

Throughout the most recent couple of years, Node.js moved to backend development as well. Designers need to utilize a similar tech stack for the whole web project without learning another language for server-side development. Node.js is a device that adjusts JS usefulness and syntax to the backend. 

What is Node.js? 

Node.js isn’t a language, or library, or system. It’s a runtime situation: commonly JavaScript needs a program to work, however Node.js makes appropriate settings for JS to run outside of the program. It’s based on a JavaScript V8 motor that can run in Chrome, different programs, or independently. 

The extent of V8 is to change JS program situated code into machine code — so JS turns into a broadly useful language and can be perceived by servers. This is one of the advantages of utilizing Node.js in web application development: it expands the usefulness of JavaScript, permitting designers to coordinate the language with APIs, different languages, and outside libraries.

What Are the Advantages of Node.js Web Application Development? 

Of late, organizations have been effectively changing from their backend tech stacks to Node.js. LinkedIn picked Node.js over Ruby on Rails since it took care of expanding responsibility better and decreased the quantity of servers by multiple times. PayPal and Netflix did something comparative, just they had a goal to change their design to microservices. We should investigate the motivations to pick Node.JS for web application development and when we are planning to hire node js developers. 

Amazing Tech Stack for Web Development 

The principal thing that makes Node.js a go-to environment for web development is its JavaScript legacy. It’s the most well known language right now with a great many free devices and a functioning local area. Node.js, because of its association with JS, immediately rose in ubiquity — presently it has in excess of 368 million downloads and a great many free tools in the bundle module. 

Alongside prevalence, Node.js additionally acquired the fundamental JS benefits: 

  • quick execution and information preparing; 
  • exceptionally reusable code; 
  • the code is not difficult to learn, compose, read, and keep up; 
  • tremendous asset library, a huge number of free aides, and a functioning local area. 

In addition, it’s a piece of a well known MEAN tech stack (the blend of MongoDB, Express.js, Angular, and Node.js — four tools that handle all vital parts of web application development). 

Designers Can Utilize JavaScript for the Whole Undertaking 

This is perhaps the most clear advantage of Node.js web application development. JavaScript is an unquestionable requirement for web development. Regardless of whether you construct a multi-page or single-page application, you need to know JS well. On the off chance that you are now OK with JavaScript, learning Node.js won’t be an issue. Grammar, fundamental usefulness, primary standards — every one of these things are comparable. 

In the event that you have JS designers in your group, it will be simpler for them to learn JS-based Node than a totally new dialect. What’s more, the front-end and back-end codebase will be basically the same, simple to peruse, and keep up — in light of the fact that they are both JS-based. 

A Quick Environment for Microservice Development 

There’s another motivation behind why Node.js got famous so rapidly. The environment suits well the idea of microservice development (spilling stone monument usefulness into handfuls or many more modest administrations). 

Microservices need to speak with one another rapidly — and Node.js is probably the quickest device in information handling. Among the fundamental Node.js benefits for programming development are its non-obstructing algorithms.

Node.js measures a few demands all at once without trusting that the first will be concluded. Many microservices can send messages to one another, and they will be gotten and addressed all the while. 

Versatile Web Application Development 

Node.js was worked in view of adaptability — its name really says it. The environment permits numerous hubs to run all the while and speak with one another. Here’s the reason Node.js adaptability is better than other web backend development arrangements. 

Node.js has a module that is liable for load adjusting for each running CPU center. This is one of numerous Node.js module benefits: you can run various hubs all at once, and the environment will naturally adjust the responsibility. 

Node.js permits even apportioning: you can part your application into various situations. You show various forms of the application to different clients, in light of their age, interests, area, language, and so on. This builds personalization and diminishes responsibility. Hub accomplishes this with kid measures — tasks that rapidly speak with one another and share a similar root. 

What’s more, Node’s non-hindering solicitation handling framework adds to fast, letting applications measure a great many solicitations. 

Control Stream Highlights

Numerous designers consider nonconcurrent to be one of the two impediments and benefits of Node.js web application development. In Node, at whatever point the capacity is executed, the code consequently sends a callback. As the quantity of capacities develops, so does the number of callbacks — and you end up in a circumstance known as the callback damnation. 

In any case, Node.js offers an exit plan. You can utilize systems that will plan capacities and sort through callbacks. Systems will associate comparable capacities consequently — so you can track down an essential component via search or in an envelope. At that point, there’s no compelling reason to look through callbacks.

 

Final Words

So, these are some of the top benefits of Nodejs in web application development. This is how Nodejs is contributing a lot to the field of web application development. 

I hope now you are totally aware of the whole process of how Nodejs is really important for your web project. If you are looking to hire a node js development company in India then I would suggest that you take a little consultancy too whenever you call. 

Good Luck!

Original Source

#node.js development company in india #node js development company #hire node js developers #hire node.js developers in india #node.js development services #node.js development

Hire Dedicated Node.js Developers - Hire Node.js Developers

If you look at the backend technology used by today’s most popular apps there is one thing you would find common among them and that is the use of NodeJS Framework. Yes, the NodeJS framework is that effective and successful.

If you wish to have a strong backend for efficient app performance then have NodeJS at the backend.

WebClues Infotech offers different levels of experienced and expert professionals for your app development needs. So hire a dedicated NodeJS developer from WebClues Infotech with your experience requirement and expertise.

So what are you waiting for? Get your app developed with strong performance parameters from WebClues Infotech

For inquiry click here: https://www.webcluesinfotech.com/hire-nodejs-developer/

Book Free Interview: https://bit.ly/3dDShFg

#hire dedicated node.js developers #hire node.js developers #hire top dedicated node.js developers #hire node.js developers in usa & india #hire node js development company #hire the best node.js developers & programmers

The  NineHertz

The NineHertz

1611828639

Node JS Development Company | Hire Node.js Developers

The NineHertz promises to develop a pro-active and easy solution for your enterprise. It has reached the heights in Node js web development and is considered as one of the top-notch Node js development company across the globe.

The NineHertz aims to design a best Node js development solution to improve their branding status and business profit.

Looking to hire the leading Node js development company?

#node js development company #nodejs development company #node.js development company #node.js development companies #node js web development #node development company

sophia tondon

sophia tondon

1619772410

10 Reasons Which Make Node.js "A First Choice For Web-App Development"

Businesses and developers opt for Node.js since it possesses unbeatable features like JavaScript. 43% of Node.js developers utilize it for business apps, and 85% primarily use it for web application development. So It Has More To Offer Than Thought!

Before we go ahead, it is essential to understand why Node.js is better?

The Open- source and cross-platform attributes of Node.js make this a preferred choice for web development. Nonetheless, enterprises would be wondering about possible alternatives to Node.js. So here, throughout this blog, we’ll highlight critical pointers that’ll make Node.js a real success for web app development.

Read Full Blog- https://vocal.media/stories/title-10-reasons-which-make-node-js-a-first-choice-for-web-app-development

#node js developer #node js developers #node.js developers #nodejs developers #hire node developer #hire node js developer

sophia tondon

sophia tondon

1620890362

Top Node js Development Company India | Node js Web Development Services

Looking to hire top Node.js developers at affordable prices? Get dedicated Node.js developers with 6+ years of average experience on hourly or full time (dedicated monthly) basis to build dynamic, feature-rich and secure software applications.

Our offshore Node JS developers create simple as well as complex enterprise-grade Node.js applications for small, mid-large scale businesses & can save your development cost up to 60%.

Planning to outsource** Node js web development services**? Or would you like to hire a team of Nodejs developers? Get in touch for a free consultation!

##hire node developer #hire node js developer #hire node js developers #hire nodejs developers #hire nodejs developer #hire node developers