[{"data":1,"prerenderedAt":522},["ShallowReactive",2],{"news-item-\u002Fnews\u002Fdeploying-node-app-to-ecs-with-docker":3},{"id":4,"title":5,"body":6,"category":509,"created by":510,"date":511,"description":512,"extension":513,"meta":514,"navigation":515,"path":516,"sections":517,"seo":518,"stem":519,"thumbnail":520,"__hash__":521},"content_en\u002Fnews\u002Fdeploying-node-app-to-ecs-with-docker.md","Deploying Node app to ECS with Docker",{"type":7,"value":8,"toc":495},"minimark",[9,13,18,37,46,52,64,68,71,74,78,81,84,95,98,104,107,113,121,124,128,131,137,141,144,150,154,157,163,166,174,177,183,191,195,199,202,205,211,214,220,223,227,230,239,242,246,249,258,262,265,269,272,279,283,286,302,305,339,343,347,350,354,383,387,390,394,397,401,404,433,437,440,448,452,455,460,464,473,477,483,489],[10,11,12],"p",{},"In this tutorial, I'll walk you through the steps on how to dockerize a Node.js application and then deploy it to Amazon Web Services (AWS) using Amazon Elastic Container Registry (ECR) and Amazon Elastic Container Service (ECS). We'll do it in follow under:",[14,15,17],"h2",{"id":16},"_1-environment-setup","1) Environment Setup",[10,19,20,24,25,28,29,36],{},[21,22,23],"strong",{},"Node"," and ",[21,26,27],{},"Npm",": ",[30,31,35],"a",{"href":32,"rel":33},"https:\u002F\u002Fnodejs.org\u002Fen\u002F",[34],"nofollow","Follow this link"," to install the latest versions.",[10,38,39,28,42,36],{},[21,40,41],{},"Docker",[30,43,35],{"href":44,"rel":45},"https:\u002F\u002Fdocs.docker.com\u002Finstall\u002F",[34],[10,47,48,51],{},[21,49,50],{},"AWS account",": Sign up for a free tier.",[10,53,54,57,58,63],{},[21,55,56],{},"AWS CLI",": Follow the ",[30,59,62],{"href":60,"rel":61},"https:\u002F\u002Faws.amazon.com\u002Fcli\u002F",[34],"instructions"," for your OS.",[14,65,67],{"id":66},"_2-overview-of-docker-and-aws","2) Overview of Docker and AWS",[10,69,70],{},"Docker is open source software that allows you to pack an application together with its required dependencies and environment variables in a container that you can ship and run anywhere. It is independent of platforms or hardware, and therefore the containerized application can run in any environment in an isolated fashion.",[10,72,73],{},"Amazon Web Services (AWS) offers a reliable, scalable, and inexpensive cloud computing service. As I mentioned before, this tutorial will focus on using the ECR and ECS services of AWS.",[14,75,77],{"id":76},"_3-the-nodejs-app-to-deploy","3) The Node.js app to deploy",[10,79,80],{},"Let’s quickly build a sample app that we’ll use for the purpose of this tutorial. It going to be very simple Node.js app.",[10,82,83],{},"Enter the following in your terminal:",[85,86,91],"pre",{"className":87,"code":89,"language":90},[88],"language-text","\u002F\u002F create a new directory\n$ mkdir ecs-nodejs-app\n\n\u002F\u002F change to new directory\n$ cd ecs-nodejs-app\n\n\u002F\u002F Initialize npm\n$ npm init -y\n\n\u002F\u002F install express\n$ npm install express\n\n\u002F\u002F create an server.js file\n$ touch server.js\n","text",[92,93,89],"code",{"__ignoreMap":94},"",[10,96,97],{},"Open server.js and paste the code below into it:",[85,99,102],{"className":100,"code":101,"language":90},[88],"\u002F\u002F server.js\n\nconst express = require('express')\nconst app = express()\n\napp.get('\u002F', (req, res) => {\nres.send('Welcome from a Node.js app!')\n})\n\napp.listen(3000, () => {\nconsole.log('Server is up on 3000')\n})\n",[92,103,101],{"__ignoreMap":94},[10,105,106],{},"Start the app with:",[85,108,111],{"className":109,"code":110,"language":90},[88],"$ node server.js\n",[92,112,110],{"__ignoreMap":94},[10,114,115,116,120],{},"Access it on ",[30,117,118],{"href":118,"rel":119},"http:\u002F\u002Flocalhost:3000",[34],". You should get Welcome from a Node.js app! displayed in your browser.",[10,122,123],{},"Next, we're going to dockerize our app.",[14,125,127],{"id":126},"_4-writing-a-dockerfile","4) Writing a Dockerfile",[10,129,130],{},"We are going to start dockerizing the app by creating a single file called a Dockerfile in the base (root) of our project directory. This file has no file extension.",[85,132,135],{"className":133,"code":134,"language":90},[88],"FROM node:8-alpine\nRUN mkdir -p \u002Fusr\u002Fsrc\u002Fapp\nWORKDIR \u002Fusr\u002Fsrc\u002Fapp\nCOPY . .\nRUN npm install\nEXPOSE 3000\nCMD [ \"node\", \"server.js\" ]\n",[92,136,134],{"__ignoreMap":94},[14,138,140],{"id":139},"_5-building-a-docker-image","5. Building a Docker image",[10,142,143],{},"Make sure you have Docker up and running. Now that we have defined our Dockerfile, let’s build the image with a title using -t:",[85,145,148],{"className":146,"code":147,"language":90},[88],"$ docker build -t ecs-nodejs-app .\n",[92,149,147],{"__ignoreMap":94},[14,151,153],{"id":152},"_6-running-a-docker-container","6. Running a Docker Container",[10,155,156],{},"We’ve built the docker image. To see previously created images, run:",[85,158,161],{"className":159,"code":160,"language":90},[88],"$ docker images\n",[92,162,160],{"__ignoreMap":94},[10,164,165],{},"You should see the image we just created as the most recent based on time:",[167,168],"img",{"className":169,"alt":94,"src":172,"style":173},[170,171],"block","mx-auto","https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28113202\u002Fimg_docker.png","width: 100%;",[10,175,176],{},"Copy the Image Id. To run the container, we write on the terminal:",[85,178,181],{"className":179,"code":180,"language":90},[88],"$docker run -p 80:3000 {image-id}\n\n\u002F\u002F fill with your image-id\n",[92,182,180],{"__ignoreMap":94},[10,184,185,186,190],{},"Here we publish the app to port 80:3000. Because we are running Docker locally, go to ",[30,187,188],{"href":188,"rel":189},"http:\u002F\u002Flocalhost",[34]," to view.",[167,192],{"className":193,"alt":94,"src":194,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28155452\u002Flocal.png",[14,196,198],{"id":197},"_7-create-the-registry-and-push-your-app-image-there","7. Create the registry and push your app image there",[10,200,201],{},"Amazon Elastic Container Registry (ECR) is a fully-managed Docker container registry that makes it easy for developers to store, manage, and deploy Docker container images. Amazon ECR is integrated with Amazon Elastic Container Service (ECS), simplifying your development to production workflow.",[10,203,204],{},"Before we get to pushing up our app image, ensure that your AWS CLI can connect to your AWS account. Run on the terminal:",[85,206,209],{"className":207,"code":208,"language":90},[88],"$ aws configure\n",[92,210,208],{"__ignoreMap":94},[10,212,213],{},"If your AWS CLI was properly installed, aws configure will ask for the following:",[85,215,218],{"className":216,"code":217,"language":90},[88],"$ aws configure\nAWS Access Key ID [None]: \u003Caccesskey>\nAWS Secret Access Key [None]: \u003Csecretkey>\nDefault region name [None]: us-east-2\nDefault output format [None]:\n",[92,219,217],{"__ignoreMap":94},[10,221,222],{},"Confirm that your AWS CLI is properly configured by once again running the aws configure command and pressing enter at each prompt to accept:",[167,224],{"className":225,"alt":94,"src":226,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28123142\u002Faws.png",[10,228,229],{},"Steps to create the registry for your images:",[10,231,232,233,238],{},"Go to the ",[30,234,237],{"href":235,"rel":236},"https:\u002F\u002Faws.amazon.com\u002F",[34],"AWS console"," and sign in.",[10,240,241],{},"Under the Services dropdown menu, under Containers, select ECS:",[167,243],{"className":244,"alt":94,"src":245,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28123526\u002Fecs-1-1024x454.png",[10,247,248],{},"On the left menu, under Amazon ECR, go to Repositories then click Create Repository. Enter a name , for example, use ecs-nodejs-app after click button \"Create Repository\".",[10,250,251,255],{},[167,252],{"className":253,"alt":94,"src":254,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28123731\u002Fecr-1024x669.png",[256,257],"br",{},[167,259],{"className":260,"alt":94,"src":261,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F05\u002F14084222\u002Fviewpush-1024x422.png",[10,263,264],{},"Then click on View Push Commands on the upper right and follow the 4 instructions from the AWS console for building, tagging, and pushing your Docker image:",[167,266],{"className":267,"alt":94,"src":268,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28123922\u002Fstep_ecr-1024x669.png",[10,270,271],{},"Now go to your Amazon ECR and click on your repository name and you should see your Docker image there. Copy the Image URI and keep it handy, we'll need to paste it in the next step:",[10,273,274,278],{},[167,275],{"className":276,"alt":94,"src":277,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28124312\u002Fimg_ecr-1024x295.png"," Next we'll create the Task, Service, and Cluster to run this Docker image on an EC2 instance.",[14,280,282],{"id":281},"_8-create-the-task-definition","8. Create the Task Definition",[10,284,285],{},"Tasks on AWS function just like the docker run command on the Docker CLI. They define:",[287,288,289,293,296,299],"ul",{},[290,291,292],"li",{},"Container images",[290,294,295],{},"Volumes",[290,297,298],{},"Networks Environment Variables",[290,300,301],{},"Port mappings",[10,303,304],{},"Steps to create a new Task Definition:",[306,307,309,312,315,318,321,324,327,330,333,336],"div",{"style":308},"padding-left: 30px",[10,310,311],{},"1. From Task Definitions in the ECS dashboard (on the left), click on the Create new Task Definition button.",[10,313,314],{},"2. Select EC2 as the launch type.",[10,316,317],{},"3. Give your task a name (for example: ecs-nodejs-app-task), then skip the next few fields and scroll down to and click on Add container.",[10,319,320],{},"4. Give the container a name (for example: ecs-nodejs-app-container).",[10,322,323],{},"5. Past your Image URI.",[10,325,326],{},"6. Set Memory Limits to Soft Limit: 512",[10,328,329],{},"7. Map your ports (for example, 80: 3000), leave protocol as TCP",[10,331,332],{},"8. Scroll down to Environment Variables and set \"NODE_ENV\" as the key and \"production\" as the value.",[10,334,335],{},"9. Skip everything else, scroll to the bottom and click Add.",[10,337,338],{},"10. Back in your Create Task Definition window, you should see your container loaded with your image, then scroll down and click Create",[167,340],{"className":341,"alt":94,"src":342,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28124940\u002Fcontainer-1024x710.png",[14,344,346],{"id":345},"_9-create-a-cluster","9. Create a Cluster",[10,348,349],{},"Steps to Create a Cluster:",[167,351],{"className":352,"alt":94,"src":353,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28125117\u002Fcluster-1024x252.png",[306,355,356,359,362,365,368,371,374,377,380],{"style":308},[10,357,358],{},"1. From the Services dropdown, select ECS to go back to your ECS dashboard.",[10,360,361],{},"2. From the left menu, under Amazon ECS (not EKS), click on Clusters, then Create Cluster",[10,363,364],{},"3. Select EC2 Linux + Networking as the cluster template.",[10,366,367],{},"4. Name your cluster (for example: ecs-nodejs-app-cluster).",[10,369,370],{},"5. Leave it as an On-Demand Instance and select t2.micro under the EC2 Instance Type dropdown.",[10,372,373],{},"6. Set number of instances to 1, and EBS Storage to 30.",[10,375,376],{},"7. You can leave the Key pair as none or select a keypair if you want to have the ability to SSH into your instance.",[10,378,379],{},"8. Under Networking, set VPC as Create a new VPC, then scroll down.",[10,381,382],{},"9. Under Container instance IAM role, select the role we just created (for example: ECStoEC2role), then click Create.",[167,384],{"className":385,"alt":94,"src":386,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28125454\u002Fcluster_1-1024x784.png",[10,388,389],{},"It'll take a few minutes to finish processing. Once processed, you should be able to click on View Cluster and see the status set to Active.",[14,391,393],{"id":392},"_10-create-a-service-to-run-the-cluster","10. Create a Service to run the Cluster",[10,395,396],{},"Now let's create a service to run this cluster.",[167,398],{"className":399,"alt":94,"src":400,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28125848\u002Fservice-1024x316.png",[10,402,403],{},"Steps to create a Service:",[306,405,406,409,412,415,418,421,424,427,430],{"style":308},[10,407,408],{},"1. From the ECS left menu, click on Task Definition, then select your task (for example: ecs-nodejs-app-task).",[10,410,411],{},"2. Click on the latest revision.",[10,413,414],{},"3. From the Actions drop down (next to Create new revision), select Create Service.",[10,416,417],{},"4. On the Configure Service page, select EC2 as the Launch type.",[10,419,420],{},"5. Set Cluster to the name of our cluster (for example: ecs-nodejs-app-cluster)",[10,422,423],{},"6. Name your service (for example: ecs-nodejs-app-service)",[10,425,426],{},"7. Set Number of Tasks to 1.",[10,428,429],{},"8. Skip through all the other fields and pages and on page 4, scroll down and click on Create Service.",[10,431,432],{},"9. On the Launch Status page, you should see green checks for Create service discovery service and Service created. Click on View Service. If the status in the Tasks tab is pending, give it a few minutes and then it will be Running (click on the refresh icon on the right).",[167,434],{"className":435,"alt":94,"src":436,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28130204\u002Fservice_1-1024x822.png",[10,438,439],{},"Go to Cluster (through a link from the service we just created) > EC2 instances > Click on the container instance to reveal the public DNS.",[10,441,442,446],{},[167,443],{"className":444,"alt":94,"src":445,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28130521\u002Fec2-1024x476.png",[256,447],{},[167,449],{"className":450,"alt":94,"src":451,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28130712\u002Fdns-1024x490.png",[10,453,454],{},"Visit the public DNS to view our app!",[10,456,457],{},[21,458,459],{},"ec2-52-193-75-195.ap-northeast-1.compute.amazonaws.com",[167,461],{"className":462,"alt":94,"src":463,"style":173},[170,171],"https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28130829\u002Fdemo.png",[10,465,466,467,472],{},"Link the code for the Docker part from ",[30,468,471],{"href":469,"rel":470},"https:\u002F\u002Fgitlab.com\u002Fbwv-hp\u002Fecs-demo.git",[34],"Github",".",[14,474,476],{"id":475},"references","References",[10,478,479],{},[30,480,481],{"href":481,"rel":482},"https:\u002F\u002Fdocs.aws.amazon.com\u002FAmazonECS\u002Flatest\u002Fdeveloperguide\u002Fgetting-started.html",[34],[10,484,485],{},[30,486,487],{"href":487,"rel":488},"https:\u002F\u002Fblog.clairvoyantsoft.com\u002Fdeploy-and-run-docker-images-on-aws-ecs-85a17a073281",[34],[10,490,491],{},[30,492,493],{"href":493,"rel":494},"https:\u002F\u002Fmedium.com\u002Fboltops\u002Fgentle-introduction-to-how-aws-ecs-works-with-example-tutorial-cea3d27ce63d",[34],{"title":94,"searchDepth":496,"depth":496,"links":497},2,[498,499,500,501,502,503,504,505,506,507,508],{"id":16,"depth":496,"text":17},{"id":66,"depth":496,"text":67},{"id":76,"depth":496,"text":77},{"id":126,"depth":496,"text":127},{"id":139,"depth":496,"text":140},{"id":152,"depth":496,"text":153},{"id":197,"depth":496,"text":198},{"id":281,"depth":496,"text":282},{"id":345,"depth":496,"text":346},{"id":392,"depth":496,"text":393},{"id":475,"depth":496,"text":476},"tech talk","Briswell Vietnam Co Ltd","2021-05-18","In this tutorial, I'll walk you through the steps on how to dockerize a Node.js application and then deploy it to Amazon Web Services (AWS) using Amazon Elastic Container Registry (ECR) and Amazon Elastic Container Service (ECS). We'll do it in follow under","md",{},true,"\u002Fnews\u002Fdeploying-node-app-to-ecs-with-docker",null,{"title":5,"description":512},"news\u002Fdeploying-node-app-to-ecs-with-docker","https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2021\u002F04\u002F28132456\u002Fecs-t.png","AqZyQ3oSRZuXZEt0DojfQdcZCGckPWt97mSDCqgA6v8",1781934435835]