Dockerization of Node.JS Applications on Amazon Elastic Containers

栏目: IT技术 · 发布时间: 5年前

内容简介:The Internet has grown exponentially within the past decade. As of October 2019, according to statistics, 4.48 billion people around the world were active internet users. This phenomenon demands highly scalable services that are fail-free and consume the m
Dockerization of Node.JS Applications on Amazon Elastic Containers

Dockerization of NodeJS Applications on Amazon Elastic Containers

Dockerization of Node.JS Applications on Amazon Elastic Containers

Number of users in millions

The Internet has grown exponentially within the past decade. As of October 2019, according to statistics, 4.48 billion people around the world were active internet users. This phenomenon demands highly scalable services that are fail-free and consume the minimum of resources when running because all things in the cloud come at a cost. This article is about creating a critical task-based highly available and scalable NodeJS application on the Amazon Cloud with an architecture that minimizes its cost.

Number of users in millionsAt the end of this article you should have learned about:

  • Dockerization of a Local Node Application
  • Building and Uploading a Docker Image
  • Running a Docker Container Locally
  • Dockerization of a Node Application on AWS
  • Creating Elastic Container Registries
  • Clustering on ECS and Running it as a Service

Prerequisites

To keep things productive, the initial installation of the following will be required. Almost all of those listed below are automated installers, therefore this shouldn’t be a hassle.

  • Node and NPM
  • AWS CLI
  • Docker

NodeJS Boilerplate Application

Let’s start by cloning the boilerplate code, which I have written for a NodeJS Application

https://github.com/th3n00bc0d3r/NodeJS-Boilerplate
git clone https://github.com/th3n00bc0d3r/NodeJS-Boilerplate 
cd NodeJS-Boilerplate
npm i
node app.js
Listening: 3000

The boilerplate code will give a base web server using express.js listening at port 3000 for two requests.

  • An HTTP response request at http://localhost:3000/
  • An HTTP Get request with a JSON Response at http://localhost/:3000/test

Dockerizing the Boilerplate Application

Dockeris an open-source software giving you the ability to create an environment inside a container that makes sure all dependencies of your application are packed together for it to run flawlessly. This ensures that the container, which becomes a container image, runs independently of any hardware specifications or platform restrictions, which, in turn, might run well on a Linux machine, but when executed on a Windows machine, fail miserably due to its library and environment path dependencies. It also solves the problem of sharing a code with someone where, in most cases, the environments of both computers are set differently.

The Dockerfile

A dockerfile is a set of instructions that assist docker to configure and create a container image of your application. To create a docker file just go into the boilerplate application directory and create a file by the name of Dockerfile – case sensitive.

touch Dockerfile
Dockerization of Node.JS Applications on Amazon Elastic Containers

Dockerfile

Contents of our Dockerfile

FROM node:6-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN npm install
EXPOSE 3000
CMD [ "node", "app.js" ]

Let’s go through those lines one by one for clarity of the structure of the dockerfile.

FROM node:6-alpine

All dockerfiles start with the very first line of the file which is known as the base image. A base image is a starting point for your docker container. A base image can be a stripped-down version of Ubuntu or CentOS so that your application can have a dedicated environment. The docker base image acts as layers, and you can pour one layer on top of another and when it compiles, the compiled docker image itself can be used as a base image for some other application. Docker has a Docker Hub which is a cloud-based repository, a collection of distributed container images created by users like you and me. You can explore and access Docker Hub at http://hub.docker.com/

We are using our base image from the following URL: https://hub.docker.com/_/node/

To translate the first line of our docker file in general terms we can refer to the following image.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Translating the first line

RUN mkdir -p /usr/src/app

The RUN identifier allows us to execute a command. In this case, we are using the mkdir command to create a directory in /usr/src/app which will complete the code of our NodeJS Boilerplate application.

WORKDIR /usr/src/app
COPY . .
RUN npm install

In this set of code, WORKDIR is setting up an environmental path to be used internally by the docker container image itself. It sets our working directory. Once our working directory is set, we copy our Node Application files to that directory using the COPY command, and then, using the RUN identifier, execute the node_modules initialization process.

EXPOSE 3000

The last line is the command for our node application to start. It is a way of telling docker how to run our application.

We are now ready to build over the docker image.

NOTE: Make sure that, at this point, you have the docker running. You should have the docker installed using the installer and then opened it. It should be in your taskbar.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Docker Desktop is up and running

Building the Docker Image

To build a docker image, execute the following command in the NodeJS Application directory.

docker build -t nodejs-boilerplate .

You should have a similar output:

Dockerization of Node.JS Applications on Amazon Elastic Containers

Creating a docker image

As you can see, the image has been successfully built and tagged. In docker, tagging is like version, and you use tagging to keep track of images when they are being deployed from one environment to another.

Running the Docker Image (Locally)

As we have successfully built our first docker image, we can display a list of images that we have built using the following command:

docker images
Dockerization of Node.JS Applications on Amazon Elastic Containers

Docker images

To generalize the run command for docker:

docker run -p <external port>:<internal port> {image-id}

Now, run the following command to execute our newly built docker image. Please keep in mind that, in your case, the Image ID will be different. By default, all docker containers do not permit any incoming connections, to fix that we use -p, which publishes all exposed ports to the interfaces.

docker run -p 8080:3000 2f356f3dfec9

You should have the following output, which states that you have successfully executed a docker container image that you have built on your computer:

Dockerization of Node.JS Applications on Amazon Elastic Containers

Successful output

Now, let’s test it out, open your favorite browser and go to http://localhost:8080/ — you should have a similar screen:

Dockerization of Node.JS Applications on Amazon Elastic Containers

localhost8080

Monitoring a running Docker Container

Let’s monitor our running docker container. To do that, simply type the following command in your terminal:

docker container ls
Dockerization of Node.JS Applications on Amazon Elastic Containers

docker container ls

Stopping a Docker Container

To stop a docker container, you can use the following command:

docker stop {container-id}

High-Level Overview of Amazon Architecture for Dockerization

Dockerization of Node.JS Applications on Amazon Elastic Containers

High-Level Overview of Amazon Architecture for Dockerization

Amazon Cloudis truly brilliant. I do agree, it does take a bit of cringing to get used to it, but once you get the hang of it, it can do marvels for you. In a nutshell, Amazon ECS (Elastic Container Service) is a service by Amazon that makes it easy for docker containers to be managed. The operations include start, stop, and manage its health. All docker containers are hosted on a cluster and that cluster can consist of more than one EC2 Instances. This makes it highly scalable (based on demand) and very fast.

As we have seen locally, the docker files, in this case, create an image, that is spun into containers. The containers are then published into an ECR (Elastic Container Registry) which is a repository for storing these container images. Once you have the image on AWS, a task definition is created. A task definition is a set of instructions in JSON format for AWS to configure the container images to use, the environment variables to initialize, and the port mapping for networking. The task definition associated itself with a cluster that is created within the AWS ECS control panel. A cluster is the amount of RAM or CPU that is going to be consumed by the container. The scheduler that maintains the number of consistent instances defined by a task definition to keep it running in case of a failure is a service .

Uploading your Docker Container Image to AWS

Login to your AWS Console and type in ECR and then click on it.

Dockerization of Node.JS Applications on Amazon Elastic Containers

AWS Console

Click on Create Repository

Dockerization of Node.JS Applications on Amazon Elastic Containers

Create Repository

Give your repository a name

Dockerization of Node.JS Applications on Amazon Elastic Containers

Giving a repository a name

You should now have the repository in the management panel:

Dockerization of Node.JS Applications on Amazon Elastic Containers

Repository in the management panel

Note: At this point, you should have your AWS CLI Configured, before moving forward.

Go to your terminal, and authenticate a user with ECR. To do that, use the following command:

aws ecr get-login --no-include-email --region us-east-1

You should now get a key with the complete command. Just copy and paste it again to successfully authenticate into AWS ECR.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Secret Key

docker login -u AWS -p YOURSECRETKEY

You will now see a login successful message.

Now, we need to tag our image with reference to the image repository we created on ECR:

docker tag <localimagename>:<tag> <onlineimageuri>:<tag>
 
docker tag nodejs-boilerplate:latest 981984658744.dkr.ecr.us-east-1.amazonaws.com/nodejs-boilerplate:latest

Then, push the image on AWS with the following command:

docker push 981984658744.dkr.ecr.us-east-1.amazonaws.com/nodejs-boilerplate:latest

You should have a similar output:

Dockerization of Node.JS Applications on Amazon Elastic Containers

Pushing the image on AWS

If you check online in the AWS ECR Console, you should have your image up there.

Dockerization of Node.JS Applications on Amazon Elastic Containers

nodejs boilerplate

Now, go to ECS, in the Amazon Console:

Dockerization of Node.JS Applications on Amazon Elastic Containers

ECS in the Amazon Console

Then, go to Clusters and Create a New Cluster.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Clusters

We will be using an EC2 Linux + Networking cluster, which includes the networking component.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Creating clusters

For this configuration, we will be using a t2.micro and give your cluster a name.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Configuring clusters

You should have a similar screen as below. Next click on view cluster.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Launch status

We also need to create a Task Definition, so click on Task Definition, and then — on Create new Task Definition.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Task definitions

For this, we will be selecting an EC2 Compatibility

Dockerization of Node.JS Applications on Amazon Elastic Containers

Creating a new task definition

Give your task a name. Scroll down, and click on Add Container.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Configure task and container definitions

Dockerization of Node.JS Applications on Amazon Elastic Containers

Container definitions

You would now require the URI from the Repository from ECR, make sure you copy it with the tag which is the latest Port mapping. It’s important to tell AWS which ports to export. In this case, we are redirecting all traffic from 80 to port 3000. The port 3000 is of the internal application running inside the container.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Add container

Scroll down and configure the Environment variables. Set them to Production and then click Create.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Environment

Once the task has been created, we need to create a service for it, therefore click on Actions and select Create Service

Dockerization of Node.JS Applications on Amazon Elastic Containers

Task definitions successfully created

The launch type is EC2 and the number of tasks is 1.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Configure service

Move forward and create the service. You should have the following success screen:

Dockerization of Node.JS Applications on Amazon Elastic Containers

Launch status 2

Click on View Service and then on the Cluster

Dockerization of Node.JS Applications on Amazon Elastic Containers

myboilerplateservice

Next, go to the EC2 instances tab and click on the Container Instance.

Dockerization of Node.JS Applications on Amazon Elastic Containers

You should finally have the URL that is public to view your running instance.

Dockerization of Node.JS Applications on Amazon Elastic Containers

Container instance

Give yourself a pat if you are now able to see the following screen upon opening the Public DNS:

Dockerization of Node.JS Applications on Amazon Elastic Containers

Opening the Public DNS

Some Key Thoughts

This might seem a bit hectic at the start, but once you make sense of things, I am sure you will have a better hold of the way things are working. This setup is very much preferred for critical tasks that one needs to be dependent in your application. Rather than having a single big application, your application can be broken down into smaller applications that work independently of each other like microservices.

Reference


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

成功由我

成功由我

刘世英、彭征 / 湖南人民出版社 / 2010-2 / 28.00元

《成功由我:李彦宏快乐成功之道》讲述:他,18岁高分考入北京大学,毕业后到美国名校学习最热门的计算机专业,然后闯荡于华尔街和硅谷这两大金融和技术圣地,31岁回国创立百度……到如今身价数十亿美元,领导的百度发展成为全球第二大搜索引擎,在国内搜索市场占据近八成的市场份额,将有“上帝”之称的Google抛在身后,最近他又掀起了“框计算”风暴,并雄心万丈宣称“未来十年,要让百度在全球一半以上国家成为家喻户......一起来看看 《成功由我》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器