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.
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.
sudo chown ubuntu:ubuntu -R /opt
cd /opt
mkdir helloworld
cd helloworld/
wget https://github.com/OmarCloud20/aws-tutorials/raw/main/prj3/HelloWorld.war
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
Note: to save the file in nano text editor, hold ^x, type yes and click enter.
ls -al
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 .
docker run -d -p 80:8080 helloworld
docker ps -a
Now, let’s verify that our application can be accessed from the device using the URL:
<public IP address of instance>/HelloWorld>
Example:
docker login --username=<DockerHub username>
Actual:
docker login --username=omarcloud20
Note: you will be prompted to enter your password.
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
Example:
docker push <image id> <dockerhub username>/<repository name>:latest
Actual:
docker push c0bd35d35ced omarcloud20/project3:latest
Navigate to the ECS service in your AWS account and click Get Started.
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
Navigate to EC2 using the Services Menu.
Navigate to Load balancer.
Select the Load Balancer that has been created by the ECS Cluster.
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
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.