Java application running on 3-Tier Architecture Docker Container.
TABLE OF CONTENTS
Goal
Goal of this project to deploy simple Java application running in Docker Containers 3-Tier Architecture.
Pre-Requisites
Create AWS EC2 Instance in Public Subnet with any desired Instance Type
Allow Port 80,8080 in Security Group
Push/clone java source code into Bitbucket
Install Java/Maven and build the artifact.
Apache Maven Build Artifact (.war)
Install docker and run docker service
Create RDS mysql db engine.
MySQL
Create MYSQL DB engine.
Copy the DB endpoints.
App:
Write Dockerfile to create custom Tomcat application container image by taking docker.io/amazonlinux as reference image from Docker Hub registry.
Install Java, Maven.
Install Apache Tomcat 8.5
Install unzip, vim, telnet, mysql client, JDK 11 packages
Copy .war artifact to webapps
Start Tomcat services on container runtime automatically.
Nginx
Write Dockerfile to create custom Nginx container image by taking docker.io/amazonlinux as reference image from Docker Hub registry.
Install Nginx server
Install telnet
Copy custom nginx.conf file with proxy_pass rule to forward the traffic to the app container.
Please checkout the link for details explanations.
I deployed the app in reverse order to under the backend functionality.
Ø Create a MYSQL DB engine under RDS in AWS. Here I have used same SG which is used in ec2.
Ø Copy the endpoint: myql-tomdb-us-east.c21em3hjuspr.us-east-1.rds.amazonaws.com
Ø Deploy an ec2 instance, allow port 80,8080, 22 on security group.
SSH into Ec2 server: ssh -i dpt6-key.pem ec2-user@3.84.141.25
Connect to the MYSQL DB : mysql -h mysql–instance1.123456789012.us-east-1.rds.amazon.. -P 3306 -u mymasteruser -p
Ø Create database UserDB, USE UserDB and create a table.
Ø Steps to create a database and tables
Ø https://bitbucket.org/shakil-practice/java-login-app/src/master/README.md
EXIT .
Pull the Java code from Bitbucket repo.
Edit the DB endpoint and username and password.
a. Note: Here password is visible to everyone. The best practice will be seen in another project. Just to understand the connectivity I have added the credentials.
b. Push to code back to Bitbucket repository
Install the docker engine:
\>> Yum install docker -y
\>> Systemctl start docker
Generate the ssh key:
\>> ssh-keygen -t id_rsa
cat /root/.ssh/id_rsa.pub
copy the public key and paste it on Bitbucket java app repo as an token authentication.
Make the directory, cd into the directory. Copy the private key into the current location.
\>>cp /root/.ssh/id_rsa .
Create a Dockerfile and add the all the instructions.
Vi Dockerfile
Create the image from Dockerfile
docker build -t shakil16/tomcat_image:1.0 .
Create docker network
\>>docker network create webnet
\>> docker network ls
Create docker container from image:
Ø docker run –name tomapp -d -p 8080:8080 –network webnet shakil1602/tom_image
Similarly, create a directory as nginx and edit the nginx.conf file.
Forward the proxy rule to tomcat application.
Create a Dockerfile for nginx server.
Create nginx image.
\>> docker build -t shakil16/ng_image:1.0 .
Create docker container from image:
Open the browser and type the ec2-instance public IP.
Click on login here:
Click in register here.
Login again
Verify your credentials in the Database, connect to the database again.
mysql -h myql-tomdb-us-east.c21em3hjuspr.us-east-1.rds.amazonaws.com -P 3306 -u admin -p
USE UserDB;
Finally. Push to code into Docker Hub
Ø docker login
Ø docker push shakil16/ng_image:1.0
Ø docker push shakil1602/tom-image:1.0
Destroy:
Once the project setup completed do not forget to clean up resources to avoid the billing for your test environment
Terminate EC2 instance from AWS Console.
Source Code:
Bitbucket docker repository: https://bitbucket.org/shakil-practice/docker
Bitbucket java source code : https://bitbucket.org/shakil-practice/java-login-app
https://www.linkedin.com/in/md-shakil
#feedback #suggestion #corrections #docker #devops