Docker Image on ECS Fargate

A Sample Web Application Containerized as a Docker image Deployed on AWS ECS Fargate

In this tutorial, we will package and containerize a sample web application as a Docker image running on Apache Tomcat having JRE-8 as a runtime. Then, we will push this new Docker image to our public repository at Dockerhub. The web application, which is packaged into a Docker image will be deployed on AWS ECS Fargate cluster. Finally, to view and access our web application cluster, we will utilize a DNS of a load balancer.

The below diagram is our design architecture for this tutorial.

prj3

It’s optional to either utilize Ubuntu EC2 instance or your own device to package and push the Docker image to Dockerhub. I expect that you have Docker installed on your instance or device, if not, please refer to my previous tutorial install Docker.

Architecture Implementation - Tutorial Scenario:

  1. Download the WAR file from my GitHub
  2. Web application needs to be packaged as a Docker image running on Tomcat having JRE8 - you will have to write a Dockerfile
  3. Once the image is created, run and verify image by accessing web application using ec2 instance public-ip
  4. Sign up for docker hub and create public repository.
  5. Tag the image appropriately and push to Docker Hub Repository.
  6. Using AWS ECS Fargate create a cluster, task and service(s).

Step 1: Create a Docker image

  1. Navigate to folder /opt and create helloworld folder.
sudo chown ubuntu:ubuntu -R /opt
cd /opt
mkdir helloworld
cd helloworld/
  1. Download the War file from my GitHub to /opt/helloworld
wget https://github.com/OmarCloud20/aws-tutorials/raw/main/prj3/HelloWorld.war

prj3

  1. Create a Docker file in the helloworld folder.
cd /opt/helloworld/
sudo nano Dockerfile

Then, add or type the following sentences into the Dockerfile. Note, you could add your name and email address as a maintainer.

FROM tomcat:jre8
MAINTAINER [your name]
COPY HelloWorld.war /usr/local/tomcat/webapps

prj3

Note: to save the file in nano text editor, hold ^x, type yes and click enter.

  • Verify that the file is created.
ls -al

prj3

  1. Build the Docker image using the Docker build command:
docker build -t helloworld .

Note: if you receive any permission error during the build, add sudo to the command line

sudo docker build -t helloworld .
  1. Run the image created above using the command given below. We will use -p to port mapping port 8080 of the container to port 80 of the host:
docker run -d -p 80:8080 helloworld

prj3

  • Let’s check whether or not our container is running:
docker ps -a

prj3

  1. Now, let’s verify that our application can be accessed from the device using the URL:

    <public IP address of instance>/HelloWorld>

prj3

Step 2: Push the Packaged Web Application Docker Image to Docker Hub

  1. Create a new free account at Docker Hub.
  2. Create a new public repository in your Dockerhub account.

prj3

  1. Login to the Dockerhub account from the CLI using the command below:

Example:

docker login --username=<DockerHub username>

Actual:

docker login --username=omarcloud20

Note: you will be prompted to enter your password.

prj3

  1. We will need to tag the image which we will upload to Docker hub using the Image ID, then we will use the tag command as shown below:

To find the Image ID:

docker images

Then, we use the tag command:

Example:

docker tag <image id> <dockerhub username>/<repository name>:latest

Actual:

docker tag c0bd35d35ced omarcloud20/project3:latest

prj3

  1. Finally, we will push the image to Dockerhub:

Example:

docker push <image id> <dockerhub username>/<repository name>:latest

Actual:

docker push c0bd35d35ced omarcloud20/project3:latest

prj3

  • Verfiy that the image is uploaded to your public dockerhub account.

prj3

Step 3: Create an AWS ECS cluster to run the Docker image

  1. Navigate to the ECS service in your AWS account and click Get Started.

  2. Under Container and Task Definitions, select Custom->Configure, and enter the following values:

    A. Container name. Mine is project3.

    B. Image location: docker.io/dockerhub username/dockerhub repository:latest

    C. Memory Limits: Soft limit - 256MB

    D. Port mappings: 8080 - tcp

    E. Click update and hen Next

prj3

prj3

  1. On the defining the Service, select Application Load Balancer and click Next.

prj3

  1. On the defining the Cluster, enter a Cluster name and click Next.

prj3

  1. On the final review, click Create.

prj3

  1. AWS ECS Fargate will create all necessary services and the cluster on your behalf. If you have encounter any errors, you may need to repeat the process one more time.

prj3

Step 4: Verification of Running Container on ECS Fargate

  1. Navigate to EC2 using the Services Menu.

  2. Navigate to Load balancer.

  3. Select the Load Balancer that has been created by the ECS Cluster.

  4. Take note of the DNS of the load balancer and visit the below URL to verify that the ECS cluster is running the container:

    <DNS of load balancer>:8080/HelloWorld

prj3

prj3

Conclusion:

Congratulations, we have successfully packaged a sample web application into a Docker image utilizing Apache Tomcat and JRE-8 runtime. From our newly created Dockerhub public account, we pushed the new image to AWS ECS Fargate cluster.