Node.js is an open-source JavaScript runtime environment. Node.js is used to develop server side scripts and it is used to create dynamic web applications. Node.js represents a “JavaScript Anywhere” paradigm, unifying web application development around a single programming language. (courtesy: Wikipedia)
By following the trend, Node.js also provides the Official Docker images for creating Node.js Docker containers.
In this article, you will learn how to deploy and run web apps in Node.js Docker containers.
If you are new to Docker platform, then you should read Docker in Action by Manning Publications.
This Article Provides:
- Environment Specification
- Create a Directory for Node.js Docker Project
- Develop Node Web App for Node.js Docker Project
- Create a Dockerfile for Node.js Docker Project
- Build the Node.js Docker Image
- Start the Node.js Docker Container
- Test the Dockerized Node.js Application
Environment Specification:
We are using a preconfigured Docker Host with following specification.
- CPU - 3.4 Ghz (2 cores)
- Memory - 4 GB
- Storage - 20 GB
- Operating System - Ubuntu Server 18.04 LTS
- Hostname – docker-01.centlinux.com
- IP Address - 192.168.116.206 /24
If you want to see how to configure a Docker Host, the you can follow our following articles.
Create a Directory for Node.js Docker Project:
Connect with Docker Host (docker-01.centlinux.com) as an admin user by using a ssh tool.
Create a directory for our Node.js Docker Project.
ahmer@docker-01:~$ mkdir ~/nodejs_docker ahmer@docker-01:~$ cd nodejs_docker
Develop Node Web App for Node.js Docker Project:
To keep things simple, we are writing a “HelloWorld” node web application for our Node.js Docker container.
ahmer@docker-01:~/nodejs_docker$ nano myapp.js
And add following lines of code to create a simple "Hello World" application.
'use strict'; const express = require('express'); // Constants const PORT = 3000; const HOST = '0.0.0.0'; // App const app = express(); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(PORT, HOST); console.log(`Running at http://${HOST}:${PORT}`);
Create the Node.js package.json file. This file provides the meta data information for our Node.js Docker application.
{ "name": "myapp", "version": "1.0.0", "description": "Node.js on Docker", "author": "Ahmer Mansoor <ahmer.mansoor@gmail.com>", "main": "myapp.js", "scripts": { "start": "node myapp.js" }, "dependencies": { "express": "^4.16.1" } }
Create a Dockerfile for Node.js Docker Project:
To define the deployment procedure such as docker image to be used, dependencies, service port, etc. We are required to create a Dockerfile for our Node.js Docker container.
ahmer@docker-01:~/nodejs_docker$ nano Dockerfile
Add following directives therein (each directive is briefly described in the comments before it).
# Docker Image to be used FROM node:13-alpine # Create a working directory within NodeJS Docker container WORKDIR /usr/src/app # Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied where available (npm@5+) COPY package*.json ./ RUN npm install # Bundle the App source COPY . . # Expose the port 3000 to network computers EXPOSE 3000 #Run the node command and start our web application CMD [ "node", "myapp.js" ]
We are using the node:13-alpine image here, because it is the lightest image that will fulfill our project’s requirement.
However, there are many variants of the node images are available at the Docker Hub. To get the complete list, you can visit the Official Docker Images for Node Page.
Build the Node.js Docker Image:
We have created the Dockerfile and the Node.js web application.
Now build the Docker image for our Node.js web application by using the Dockerfile.
ahmer@docker-01:~/nodejs_docker$ sudo docker build .
Sending build context to Docker daemon 4.096kB
Step 1/7 : FROM node:13-alpine
---> 483343d6c5f5
Step 2/7 : WORKDIR /usr/src/app
---> Using cache
---> bad78ca928c6
Step 3/7 : COPY package*.json ./
---> Using cache
---> fdd7ac2c1274
Step 4/7 : RUN npm install
---> Using cache
---> 8a34ec80bd87
Step 5/7 : COPY . .
---> 873f9ddd4360
Step 6/7 : EXPOSE 3000
---> Running in 7d567e0f4da3
Removing intermediate container 7d567e0f4da3
---> b3e087682594
Step 7/7 : CMD [ "node", "myapp.js" ]
---> Running in 4e170d3a1567
Removing intermediate container 4e170d3a1567
---> 7be386175292
Successfully built 7be386175292
We have successfully built our Node.js Docker image.
We have built this image locally, however you can build and store this image on Docker Hub by using your Docker Hub credentials.
Start the Node.js Docker Container:
Start a container by using above Docker image.
ahmer@docker-01:~/nodejs_docker$ sudo docker run \ > --name nodejs-docker-app \ > -p 80:3000 \ > -d \ > 7be386175292 87f61f85cd0546786a2af8a5eb6da1402e753a1239a0f1088a735217130a603b
We have started our Node.js Docker container. Check its log now.
ahmer@docker-01:~/nodejs_docker$ sudo docker logs nodejs-docker-app
Running at http://0.0.0.0:3000
Test the Dockerized Node.js Application:
Access the Dockerized NodeJS application using the curl command.
ahmer@docker-01:~/nodejs_docker$ curl localhost:80
Hello World!
Our Node.js application has been deployed in a Docker container.
Remember that, this is a beginner example of deploying Node.js web application in docker containers. But the same procedure can be followed for deploying the larger Node.js web applications in the Docker containers.
No comments:
Post a Comment