Java application running on 3-Tier Architecture Docker Container.

·

3 min read

TABLE OF CONTENTS

  1. Goal

  2. Pre-Requisites

  3. Docker Compose Stack

  4. Validation

  5. Destroy

Goal

Goal of this project to deploy simple Java application running in Docker Containers 3-Tier Architecture.

Pre-Requisites

  1. Create AWS EC2 Instance in Public Subnet with any desired Instance Type

  2. Allow Port 80,8080 in Security Group

  3. Push/clone java source code into Bitbucket

  4. Install Java/Maven and build the artifact.

  5. Apache Maven Build Artifact (.war)

  6. Install docker and run docker service

  7. Create RDS mysql db engine.

MySQL

  1. Create MYSQL DB engine.

  2. Copy the DB endpoints.

App:

  1. Write Dockerfile to create custom Tomcat application container image by taking docker.io/amazonlinux as reference image from Docker Hub registry.

  2. Install Java, Maven.

  3. Install Apache Tomcat 8.5

  4. Install unzip, vim, telnet, mysql client, JDK 11 packages

  5. Copy .war artifact to webapps

  6. Start Tomcat services on container runtime automatically.

Nginx

  1. Write Dockerfile to create custom Nginx container image by taking docker.io/amazonlinux as reference image from Docker Hub registry.

  2. Install Nginx server

  3. Install telnet

  4. 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 .

  1. Pull the Java code from Bitbucket repo.

  2. 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:

  1. Once the project setup completed do not forget to clean up resources to avoid the billing for your test environment

  2. 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