Why And How To Containerize Modern NodeJS Applications
Imagine you write your code, wrap it up, and deliver it straight to your friend – gift-wrapped as a present. This is containerization of apps in a nutshell. Containerization is a concept, that lets you open up your presents for Christmas and fit it straight into a puzzle, the puzzle being your complete software package.
Besides offering a blend of portability, ease of use with different coding stacks, and code reusability, let’s discuss why you should consider containerizing your Node.JS apps with Docker and how to get it done.
What benefits do containerization of apps offer for modern day software developers? Let’s take a look below.
1. App Execution In Isolated Runtime Environments with Virtual OS
2. Virtualizing of OS allows applications to be distributed across a single host without the need of running Virtual Machines through access to a single OS kernel
3. All apps run on the same Linux Kernel, thus saving hardware resources and improving boot times
4. Multiple containerized apps can be run concurrently on a single server
5. Simplifies DevOps deployment workflows as all runtime differences get encapsulated within containers. Docker apps are executable in different framework stacks.
6. No need to set up or install libraries, packages, configuration files, etc.
7. The working environment remains the same whether the container is run on development, staging, or production servers.
Undoubtedly, scalability is the biggest bonus point of app containerization as you get to add on or remove containers based on your choice in software development pipelines.
How To Containerize Modern Node JS Applications
In this article, we’ll discuss how to containerize a very basic “hello world” Node.JS app using Docker and run it on another port besides your localhost. You can apply the same steps for your other Node.JS files no matter the code complexity since the concept remains the same. Let’s begin.
1. Visit Docker.com and download the Docker Enterprise Edition from the official website.
2. Initialize the “hello world” file with npm init.
3. Install npm manager and run Express Framework with the following line-
npm install –save express
4. Create an index.js file for your app code using this line-
5. Open up your favorite code editor now. We’ll proceed with Microsoft Visual Studio in this example.
6. Create a Docker image since that’s needed to run your code inside a Docker container. Open up Command Line and write down the following code to create a Docker file (a text file that tells Docker how to create a Docker image) and make that happen
7. Go back to your code editor and change the local port value to your desired choice on Line 7.
8. Run the CLI (Command Line Interface) and type in the following lines to finally create your Docker build
docker build -t hello-world .
9. Run your Docker image inside a container with the following command-
docker run -p 8082:(local port) hello-world
And voila! You should find your app running inside your Docker container.
Did you like this post? Comment below and let us know your thoughts!