AWS proposes two container orchestrations service: ECS and Kubernete.
Well integrated with the AWS ecosystem, ECS is the proprietary version.
In this tutorial we will explain how to:
Concepts:
We want to deploy a basic express node application that displays the current time each time the index page is refreshed.
package.json
1{2 "name": "docker_web_app",3 "version": "1.0.0",4 "description": "Node.js on Docker",5 "author": "Raphaël MANSUY raphael.mansuy+contact@gmail.com>",6 "main": "server.js",7 "scripts": {8 "start": "node server.js"9 },10 "dependencies": {11 "express": "^4.17.1"12 }13}
server.js
1"use strict"23const express = require("express")45// Constants6const PORT = 80807const HOST = "0.0.0.0"89// App10const app = express()11app.get("/", (req, res) => {12 res.send(`Hello World - ${new Date().toISOString()}`)13})1415app.listen(PORT, HOST)16console.log(`Running on http://${HOST}:${PORT}`)
https://nodejs.org/en/docs/guides/nodejs-docker-webapp/
In the same directory of this application we can create a Dockerfile
that explains how to build a container with this application:
Dockerfile
1FROM node:142# Create app directory3WORKDIR /usr/src/app4# Install app dependencies5# A wildcard is used to ensure both package.json AND package-lock.json are copied6# where available (npm@5+)7COPY package*.json ./89RUN npm install10# If you are building your code for production11# RUN npm ci --only=production1213# Bundle app source14COPY . .1516EXPOSE 80801718CMD [ "node", "server.js" ]
This file defines the following steps:
/usr/src/ap
inside the containerpackage*.json
in the containernpm install
8080
inside the containernode
with the file server.js
when the container startsRun the following command to build an image with the tag node-web-app
1docker build -t node-web-app .
Run the following command to start the application in detached mode:
1docker run -p 80:8080 -d node-web-app
The container is now running and the 8080 port within the container is exposed as the 80 port on your local machine.
We can now test the application with the CURL command
1curl http://localhost:80
Results:
1Hello World - 2021-02-11T05:06:12.739Z
We are now ready to deploy this container to the cloud.
Prerequisites
aws cli
must be installedRun the following command:
1aws ecr get-login-password --region us-west-2 | docker login
If your have access you should have this display on the terminal:
1Authenticating with existing credentials...2Login Succeeded
Connect to the AWS Console and to the ECS Administration screen to create a new repository.
Click on Create Repository and choose testrepository
as a name for your repository:
The ECR repository is now created:
Click now on the push commands button
on the repository screen:
Copy and execute each command on your machine:
connect:
1aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin 3680199100XXX.dkr.ecr.us-west-2.amazonaws.com
build:
1docker build -t testrepository .
build:
1docker tag testrepository:latest 3680199100XXX.dkr.ecr.us-west-2.amazonaws.com/testrepository:latest
push to ECR:
1docker push 3680199100XXX.dkr.ecr.us-west-2.amazonaws.com/testrepository:latest
The image is now published and available on ECR ready to be deployed:
If you look at AmazonECR, repositories we can see the new created image.
Copy the image URI: we need to keep this to create a task definition for the following steps.
1368019910004.dkr.ecr.us-west-2.amazonaws.com/testrepository:latest
Go to the ECS home page and click on the create cluster
button:
Choose EC2 Linux + Networking
and then click next:
Then enter the following information:
ecs01
t3-micro
1
Then choose:
Enabled
default
And then next press Enter
A task a set of metadata (memory, cpu, port mappings, environment variables, etc) that describes how a container should be deployed.
Click on new Task definition
Choose EC2
Then next
Choose NodeWebAppTask
for the name of the task definition.
Enter 128
for memory size.
Click add container:
NodeWebApp
add image
stepClick create
.
Then go to Run Task
The task is now running:
If we click on the container instance:
We can modify the security group associated with instance to open the port 80
Add 80 in the inbound rule to the security group:
If we try now to open the url: http://ec2-52-38-113-251.us-west-2.compute.amazonaws.com
:
Our clustor and node application is now deployed.
🎉 🎉 🎉
We deliver high quality blog posts written by professionals monthly. And we promise no spam.