Deploy a Docker App to AWS using ECS

Deploy a Docker App to AWS using ECS

AWS proposes two container orchestrations services: ECS and Kubernete.

Well integrated with the AWS ecosystem, ECS is the proprietary version.

What we will build

In this tutorial we will expl…



Deploy a Docker App to AWS using ECS

AWS proposes two container orchestrations services: ECS and Kubernete.

Well integrated with the AWS ecosystem, ECS is the proprietary version.



What we will build

In this tutorial we will explain how to:

  • Package and build a node application and package a simple node application with Docker
  • Create an ECR repository to store our Docker Image
  • Upload the Docker image to the repository
  • Create and launch an Elastic Container Cluster (ECR)
  • Launch our application as a task within the Elastic Container Cluster
  • Expose and open this application on the internet

Workflow Shema

  • Docker is a technology that helps to package and ship applications easily in production.
  • ECS stands for Elastic Container Service. It is a fully managed container orchestration service
  • ECR stands for Elastic Container Repository. ECR allows storage of Docker Images on AWS.

Concepts:

  • A cluster is a logical grouping of hardware resources.
  • A task is a set of metadata (memory, CPU, port mapping, environmental variables, etc) that describes how a container should be deployed.
  • Services are responsible for managing advanced configurations such as load balancing



The NodeJS application to deploy

We want to deploy a basic express node application that displays the current time each time the index page is refreshed.



package.json

{
  "name": "docker_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "Raphaël MANSUY raphael.mansuy+contact@gmail.com>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}
Enter fullscreen mode

Exit fullscreen mode



server.js

"use strict"

const express = require("express")

// Constants
const PORT = 8080
const HOST = "0.0.0.0"

// App
const app = express()
app.get("/", (req, res) => {
  res.send(`Hello World - ${new Date().toISOString()}`)
})

app.listen(PORT, HOST)
console.log(`Running on http://${HOST}:${PORT}`)
Enter fullscreen mode

Exit fullscreen mode

https://nodejs.org/en/docs/guides/nodejs-docker-webapp/



Package the node.js application with a Docker file

In the same directory of this application, we can create a Dockerfile that explains how to build a container with this application:



Dockerfile

FROM node:14
# Create app directory
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
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080

CMD [ "node", "server.js" ]

Enter fullscreen mode

Exit fullscreen mode

This file defines the following steps:

  • start from the node:14 image
  • create a directory /usr/src/ap inside the container
  • copy the local file with pattern package*.json in the container
  • run npm install
  • copy all the local files to the container
  • expose the port 8080 inside the container
  • run node with the file server.js when the container starts



Building the image

Run the following command to build an image with the tag node-web-app

docker build -t node-web-app .
Enter fullscreen mode

Exit fullscreen mode



Running the image

Run the following command to start the application in detached mode:

docker run -p 80:8080 -d node-web-app
Enter fullscreen mode

Exit fullscreen mode

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

curl http://localhost:80
Enter fullscreen mode

Exit fullscreen mode

Results:

Hello World - 2021-02-11T05:06:12.739Z
Enter fullscreen mode

Exit fullscreen mode

We are now ready to deploy this container to the cloud.



Connect to AmazonECR

Prerequisites

  • aws cli must be installed
  • your aws profile must be configured and have ECS admin rights enabled

Run the following command:

aws ecr get-login-password --region us-west-2 | docker login
Enter fullscreen mode

Exit fullscreen mode

If you have access, you should have this displayed on the terminal:

Authenticating with existing credentials...
Login Succeeded

Enter fullscreen mode

Exit fullscreen mode



Create your AmazonECR in the AWS Console

Connect to the AWS Console and to the ECS Administration screen to create a new repository.

ECR Step1

Click on Create Repository and choose testrepository as a name for your repository:

ECR Step2

The ECR repository is now created:

ECR Step3



Upload the image on AWS ECR

Click now on the push commands button on the repository screen:

ECR Step4

Copy and execute each command on your machine:

ECR Step5

connect:

aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin 3680199100XXX.dkr.ecr.us-west-2.amazonaws.com
Enter fullscreen mode

Exit fullscreen mode

build:

docker build -t testrepository .

Enter fullscreen mode

Exit fullscreen mode

build:

docker tag testrepository:latest 3680199100XXX.dkr.ecr.us-west-2.amazonaws.com/testrepository:latest
Enter fullscreen mode

Exit fullscreen mode

push to ECR:

docker push 3680199100XXX.dkr.ecr.us-west-2.amazonaws.com/testrepository:latest
Enter fullscreen mode

Exit fullscreen mode

The image is now published and available on ECR ready to be deployed:

ECR Step6

If you look at AmazonECR, repositories we can see the newly created image.

ECR Step6-1

ECR Step6-2

Copy the image URI: we need to keep this to create a task definition for the following steps.

368019910004.dkr.ecr.us-west-2.amazonaws.com/testrepository:latest
Enter fullscreen mode

Exit fullscreen mode



Create an ECS Cluster

Go to the ECS home page and click on the create cluster button:

ECR Step7

Choose EC2 Linux + Networking and then click next:

ECR Step8

Then enter the following information:

  • name of the cluster: ecs01
  • EC2 instance type: t3-micro
  • Number of instances: 1

ECR Step9

Then choose:

  • Default VPC
  • Auto assign IP: Enabled
  • Security group: default
  • Choose one of the subnet

ECR Step10

And then next press Enter



Create a new Task definition

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

ECR Step11

Choose EC2

ECR Step12

Then next

Choose NodeWebAppTask for the name of the task definition.

ECR Step13

Enter 128for memory size.

Click Add Container:

ECR Step14

  • Add the name of the container: NodeWebApp
  • Set the image URI that we have saved add the end of the add image step
  • Set the port mappings 80:8080

ECR Step15

Click create.

ECR Step16

Then go to Run Task

ECR Step17

ECR Step18

The task is now running:

ECR Step19

If we click on the container instance:

ECR Step20

We can modify the security group associated with instance to open the port 80

ECR Step21
ECR Step22

Add 80 in the inbound rule to the security group:

ECR Step23

If we try now to open the url: http://ec2-52-38-113-251.us-west-2.compute.amazonaws.com:

ECR Step24



Et voilà

Our cluster and node application is now deployed.

? ? ?


Print Share Comment Cite Upload Translate
APA
Raphael MANSUY | Sciencx (2024-03-28T18:21:55+00:00) » Deploy a Docker App to AWS using ECS. Retrieved from https://www.scien.cx/2021/02/11/deploy-a-docker-app-to-aws-using-ecs/.
MLA
" » Deploy a Docker App to AWS using ECS." Raphael MANSUY | Sciencx - Thursday February 11, 2021, https://www.scien.cx/2021/02/11/deploy-a-docker-app-to-aws-using-ecs/
HARVARD
Raphael MANSUY | Sciencx Thursday February 11, 2021 » Deploy a Docker App to AWS using ECS., viewed 2024-03-28T18:21:55+00:00,<https://www.scien.cx/2021/02/11/deploy-a-docker-app-to-aws-using-ecs/>
VANCOUVER
Raphael MANSUY | Sciencx - » Deploy a Docker App to AWS using ECS. [Internet]. [Accessed 2024-03-28T18:21:55+00:00]. Available from: https://www.scien.cx/2021/02/11/deploy-a-docker-app-to-aws-using-ecs/
CHICAGO
" » Deploy a Docker App to AWS using ECS." Raphael MANSUY | Sciencx - Accessed 2024-03-28T18:21:55+00:00. https://www.scien.cx/2021/02/11/deploy-a-docker-app-to-aws-using-ecs/
IEEE
" » Deploy a Docker App to AWS using ECS." Raphael MANSUY | Sciencx [Online]. Available: https://www.scien.cx/2021/02/11/deploy-a-docker-app-to-aws-using-ecs/. [Accessed: 2024-03-28T18:21:55+00:00]
rf:citation
» Deploy a Docker App to AWS using ECS | Raphael MANSUY | Sciencx | https://www.scien.cx/2021/02/11/deploy-a-docker-app-to-aws-using-ecs/ | 2024-03-28T18:21:55+00:00
https://github.com/addpipe/simple-recorderjs-demo