Deploy a Docker App to AWS using ECS

Development
Thursday, February 11, 2021

Deploy a Docker App to AWS using ECS

AWS proposes two container orchestrations service: 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 a 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 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 to storage of Docker Images on AWS.

Concepts:

  • A cluster is logical grouping of hardware ressources.
  • A task a set of metadata (memory, cpu, port mappings, environment variables, etc) that describes how a container should be deployed.
  • Services are responsible to manage advance configuration 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

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"
2
3const express = require("express")
4
5// Constants
6const PORT = 8080
7const HOST = "0.0.0.0"
8
9// App
10const app = express()
11app.get("/", (req, res) => {
12 res.send(`Hello World - ${new Date().toISOString()}`)
13})
14
15app.listen(PORT, HOST)
16console.log(`Running on http://${HOST}:${PORT}`)

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

Package the nodejs 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

1FROM node:14
2# Create app directory
3WORKDIR /usr/src/app
4# Install app dependencies
5# A wildcard is used to ensure both package.json AND package-lock.json are copied
6# where available (npm@5+)
7COPY package*.json ./
8
9RUN npm install
10# If you are building your code for production
11# RUN npm ci --only=production
12
13# Bundle app source
14COPY . .
15
16EXPOSE 8080
17
18CMD [ "node", "server.js" ]

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

1docker build -t node-web-app .

Running the image

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.

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:

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

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:

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:

ECR Step6

If you look at AmazonECR, repositories we can see the new 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.

1368019910004.dkr.ecr.us-west-2.amazonaws.com/testrepository:latest

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 clustor and node application is now deployed.

🎉 🎉 🎉

Subscribe to our Newsletter

We deliver high quality blog posts written by professionals monthly. And we promise no spam.

elitizon ltd.

© 2020 elitizon ltd. All Rights Reserved.