This content originally appeared on DEV Community and was authored by Sanjay Singh Rajpoot
We all have heard of docker a lot. In this post we will see how to dockerize your existing node js appliation.
We will create a docker image for our Nodejs application. A Docker image is a self-contained unit that bundles the app with the environment required to run it. No more installing libraries, dependencies, downloading packages, messing with config files, etc. If your machine supports Docker, you can run a Dockerized app, period.
For this tutorial lets first create a new simple Node Js application.
Create a simple Node.js app
User the commands below to create a simple nodejs application.
mkdir nodejs_docker
cd nodejs_docker
npm init
set the name to nodejs_docker. For other options, just confirm the default values with enter.
Npm will create a package.json file that will hold the dependencies of the app. Let's add the Express framework as the first dependency:
npm install express --save
The file should look like this now:
{
"name": "nodejs_docker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Now create an index.js file with a simple HTTP server that will serve the nodejs_docker app, this code will just run the server and will give confirmation message that the server is running:
//Load express module with `require` directive
var express = require('express')
var app = express()
//Define request response in root URL (/)
app.get('/', function (req, res) {
res.send('Hello World!')
})
//Launch listening server on port 8081
app.listen(8081, function () {
console.log('app listening on port 8081!')
})
Run the app
The application is ready to launch:
node index.js
Go to http://localhost:8081/ in your browser to view it.
Dockerize Node.js appplication
Once the node applicaiton is up and running the next step we can take is to dockerize this application. To do this go ahead and install Docker on your system.
This content originally appeared on DEV Community and was authored by Sanjay Singh Rajpoot

Sanjay Singh Rajpoot | Sciencx (2022-04-02T14:09:38+00:00) Dockerizing a Node.js web app. Retrieved from https://www.scien.cx/2022/04/02/dockerizing-a-node-js-web-app/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.