In this tutorial, we will learn, how to create a Node.js application and then dockerize it. Dockerization will help to deploy and scale the application easily on any machine.
To develop the Node.js application we need to install a few packages and dependencies.
Node.js installation
Here, I will install the Node.js packages on my Ubuntu 18.04 operating system. To trigger the installation, execute the below command.
sudo apt-get install nodejs
The Nodejs use npm for package management. This will allow you to easily install modules and packages to use with Node.js.
sudo apt-get install npm
Confirm the version of installed packages with the below commands.
techies@techiescorner:~$ nodejs -v v8.10.0 techies@techiescorner:~$ npm -v 3.5.2
Next, create a folder to store our code.
$ mkdir nodejs_work $ cd nodejs_work $ npm init
The above command will ask a few questions about our application. You can use default values or can add the value that you wish to set for your application. The output looks like below.
This utility will walk you through creating a package.json file. version: (1.0.0) description: Node.js project for techiescorner.in entry point: (index.js) test command: git repository: keywords: author: admin license: (ISC) About to write to /home/techies/nodejs_work/package.json: { "name": "techiescorner", "version": "1.0.0", "description": "Node.js project for techiescorner.in", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "admin", "license": "ISC" } Is this ok? (yes) yes
The above-created package.json will hold the dependencies for the app, so add our first dependency which is “Express Frameworks”. To install the dependency execute below command from the same folder.
npm install express --save
The final package.json file will be as shown below.
techies@techiescorner:~/nodejs_work$ cat package.json
{ "name": "techiescorner", "version": "1.0.0", "description": "Node.js project for techiescorner.in", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "admin", "license": "ISC", "dependencies": { "express": "^4.17.1" } }
Create Index.js file
Index.js file is the entry point file, create this file as follows. This code contains a simple HTTP server that will serve the content.
//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 Techiescorner.in!!!') }) //Launch listening server on port 8000 app.listen(8000, function () { console.log('app listening on port 8000!') })
Run the application
Now our Node.js application is ready, to run this application execute below command in the terminal.
$ node index.js
app listening on port 8000!
To view the application, open another terminal and execute below command or you can access the application on a browser with the help of the private IP address.
$ curl http://localhost:8000 Hello Techiescorner.in!!!
To view on browser enter the private IP address and port.
Application Dockerization
Docker installation is not part of this tutorial. I recommend you refer to other sources and install it in your Operating System.
To build the Node.js application docker image, we will use Dockefile. To understand more about Dockerfile refer below article.
Create a file named Dockerfile and add the following code.
$ vi Dockerfile
FROM node:7 WORKDIR /app COPY package.json /app RUN npm install COPY . /app EXPOSE 8000 CMD node index.js
FROM node:7:- This uses the node:7 image as the base image for our container. We will install and run everything on this base image.
WORKDIR: The directory (/app) in the container, which we use to keep all application-related files and binaries.
COPY and RUN: Copy command copy package.json file and other files that are created as part of npm installation will be copied to the working directory (/app). RUN command to install the package.
EXPOSE: It will expose the port 8000 to outside for the application to run.
CMD: The process which should be executed while launching the container. Here it executes index.js file.
The Dockerfile is ready, to build the image, execute below command from the directory which contains the Dockerfile (Don’t miss the last dot ).
$ sudo docker build -t hello-nodejs .
-t flag is to give a name for the container (hello-nodejs). The build process may take a few minutes to complete.
To list the available images execute below command.
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-nodejs latest d21a9696b98f 24 minutes ago 667MB
From this image, we will launch our container, for this execute below command. (Make sure already running node.js is stopped before triggering this command.)
sudo docker run --name nodejs-app -p 8000:8000 -d hello-nodjs
–name: Name for the Docker container
-p: flag will open port 8000 to and out from the container
-d: Run the process in the background
$ sudo docker run --name nodejs-app -p 8000:8000 -d hello-nodjs 9bf509b5c2ba9140e262a2f16b4a42eb18fe0648719e82df5387377840c8fe15
To list all running container and to get its details
$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9bf509b5c2 hello-nodjs "/bin/sh -c 'nod" 8s ago Up 7 seconds :8000->8000/tcp nodejs-app
The container is started and running for 7 seconds. Execute the curl command again to confirm the content is loading from the Docker container.
$ curl http://localhost:8000
Hello Techiescorner.in!!!
Yes, we have successfully dockerized our Node.js application. Next, we can see how we can share this image.
Publishing Docker images
Docker images can be stored in a repository so we can download and install it on any server. Dockerhub is one of the public repositories where we can host our Docker images.
Steps to publish Docker images
- Sign up at hub.docker.com
- Log in to the Docker Hub with the credentials.
sudo docker login
3. Build the image again with Docker Hub user name
sudo docker build -t techiescorner/nodejs-image .
4. To list all images in the system execute the below command.
$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-nodjs latest d21a9696b98f 2 hours ago 667MB techiescorner/nodejs-image latest d21a9696b98f 2 hours ago 667MB
5. Push the image to the repository.
sudo docker push techiescorner/nodejs-image
Output:-
The push refers to repository [docker.io/techiescorner/nodejs-image] bdeb2f3307a3: Pushed d81f972ab94b: Pushed 5616a6292c16: Mounted from library/node 2c40c66f7667: Mounted from library/node latest: digest: sha256:d782305235d57b5240869d2a6c1b15aea87476f98f892715f8a27ae06a6d8a7b size: 2841
Login to the Dockerhub and you can see the uploaded images are present under your account. These images can be pulled to any server which has Docker service running it. To download the image
sudo docker pull techiescorner/nodejs-image