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.
PermalinkCreate 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!')
})
PermalinkRun the app
The application is ready to launch:
node index.js
Go to localhost:8081 in your browser to view it.
PermalinkDockerize 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. After this create an empty file called DockerFile
in our codebase.
Open the DockerFile
in any code editor. Now lets creating the docker file.
- Line 1: We have to mention which version of node do we want and image we want to build from.
FROM node:16
- Line 2: making a working directory in the contaier to
/app
. This will create a folder inside the docker container.
WORKDIR /app
- Line 3-4-5: Copy the application to the
/app
directory and install dependencies. If you add thepackage.json
first and runnpm install
later, Docker won't have to install the dependencies again if you change thepackage.json
file.
COPY package.json /app
RUN npm ci --only=production && npm cache clean --force
COPY . /app
- Line 6: This line describes what should be executed when the Node Docker image is launching. What we want to do is to run our application:
CMD node index.js
- Line 7: Now all things are being done inside the container and no one can access it until we expose the port.
EXPOSE 8081
- Final Docker File
Summing up, the whole Dockerfile should look like this:
FROM node:12-alpine3.14
WORKDIR /app
COPY package.json /app
RUN npm ci --only=production && npm cache clean --force
COPY . /app
CMD node index.js
EXPOSE 8081
PermalinkBuild Docker image
With the instructions ready, all that remains is to run the docker build command, set the name of your image with -t parameter, and choose the directory where the Dockerfile is located:
docker build -t nodejs_docker .
PermalinkRun Docker container
The application has been baked into the image. Dinner time! Execute the following command to launch the container and publish it on the host with the same port 8081:
docker run -p 8081:8081 nodejs_docker
#Thank You