Deploy Simple Java Application using Docker Compose.

Goal
The goal of this project is to create Docker Compose Stack to deploy a simple Java application running in Containers 3-Tier Architecture.

Pre-Requisites

Create AWS EC2 Instance in Public Subnet with any desired Instance Type
Allow Port 80 in Se…


This content originally appeared on DEV Community and was authored by divine kenechukwu

Goal
The goal of this project is to create Docker Compose Stack to deploy a simple Java application running in Containers 3-Tier Architecture.

Pre-Requisites

  1. Create AWS EC2 Instance in Public Subnet with any desired Instance Type
  2. Allow Port 80 in Security Group
  3. Apache Maven Build Artifact (.war)
  4. Install docker and run docker service
  5. Install docker-compose
  6. Java Source Code

Step 1: Set Up the Environment:

1. Create an Amazon Linux EC2 Instance:

  • Launch an EC2 instance in a public subnet.
  • Choose any instance type, such as t2.micro for testing.
  • Allow port 80 (HTTP) in the security group.

2. Install Docker and Docker Compose on the EC2 Instance:

- Docker:

sudo yum update -y

sudo yum install -y docker

sudo service docker start

sudo usermod -a -G docker ec2-user

- docker-compose:

sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

docker-compose version

3. Install Java and Apache Maven (for building the Java application):

curl -LO https://corretto.aws/downloads/latest/amazon-corretto-11-x64-linux-jdk.tar.gz

sudo tar -xvzf amazon-corretto-11-x64-linux-jdk.tar.gz -C /usr/java/

cd /usr/java/amazon-corretto-*-linux-x64/bin

for i in java javac jfr; do path=$(find `pwd`/$i -type f); echo $path; sudo alternatives --install /usr/bin/$i $i $path 20000 ; sudo update-alternatives --config $i ; done

java -version

Step 2: Clone the Java Source Code:

  • To deploy this application, clone the source code into the Amazon Linux instance created using git clone command.
git clone https://github.com/Divine4212/java-login-app

Step 3: Write Dockerfiles:
Dockerfiles are the blueprints for creating Docker images. In a Docker Compose project, they define the environment for each service. Docker Compose uses these Dockerfiles to build the images and then run them as containers.

- Dockerfile-sql:
The Dockerfile-sql will create custom MySQL container image by taking docker.io/mysql:8.0 as reference image from Docker Hub registry.

FROM mysql:8.0

ENV MYSQL_ROOT_PASSWORD=rootpassword
ENV MYSQL_DATABASE=UserDB
ENV MYSQL_USER=admin
ENV MYSQL_PASSWORD=Admin123

COPY init.sql /docker-entrypoint-initdb.d/

- init.sql:

init.sql:
CREATE TABLE Employee (
  id int unsigned auto_increment not null,
  first_name varchar(250),
  last_name varchar(250),
  email varchar(250),
  username varchar(250),
  password varchar(250),
  regdate timestamp,
  primary key (id)
);

- Dockerfile-app:
The Dockerfile-app is a simplified and optimized version for your Java Login App that takes docker.io/openjdk:11-jre-slim as base image from Docker Hub registry.. It installs unzip, vim, telnet and mysql-client and copy .war artifact to /app.war and start the application.

# Use OpenJDK 11 as the base image
FROM openjdk:11-jre-slim

# Install necessary packages (unzip, vim, telnet, mysql-client)
RUN apt-get update && \
    apt-get install -y unzip vim telnet mysql-client && \
    rm -rf /var/lib/apt/lists/*

# Set volume for temporary files
VOLUME /tmp

# Copy the WAR file to the container
COPY target/dptweb-1.0.war /app.war

# Expose the port Tomcat will run on
EXPOSE 8080

# Run the application using the JAR command
ENTRYPOINT ["java", "-jar", "/app.war"]

- Dockerfile-nginx:
The Dockerfile creates a custom Nginx container image by taking docker.io/amazonlinux as reference image from Docker Hub registry. Will installs Nginx server, telnet and also copy the custom nginx.conf file with proxy_pass rule to forward the traffic to the app container.

FROM amazonlinux:2

RUN yum update -y && \
    amazon-linux-extras install nginx1 -y && \
    yum install -y telnet

COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

- nginx.conf:

events {
    worker_connections 1024;
}

http {
    upstream app {
        server app:8080;
    }

    server {
        listen 80;
        location / {
            proxy_pass http://app;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

Step 4: Write the Docker Compose File (docker-compose.yml):

- docker-compose.yml:
Docker Compose Stack Builds and Runs the Nginx, MySQL, App containers.

services:
  db:
    build:
      context: .
      dockerfile: Dockerfile-mysql
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: UserDB
      MYSQL_USER: admin
      MYSQL_PASSWORD: Admin123
    networks:
      - app-network

  app:
    build:
      context: .
      dockerfile: Dockerfile-app
    depends_on:
      - db
    networks:
      - app-network
    ports:
      - "8080:8080"  # Expose port 8080

  nginx:
    build:
      context: .
      dockerfile: Dockerfile-nginx
    ports:
      - "80:80"
    depends_on:
      - app
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

After creating these files in your cloned application folder, run mvn clean package command to build your application with Maven, run docker-compose build command to verify if container images are getting created and the run Run docker-compose up -d command to verify if all containers are running.

Validation

  • Login to Nginx container shell and check App port is reachable.
docker exec -it 'container-id-nginx' /bin/sh

curl http://app:8080
  • Login to App container shell and check MySQL port is reachable.
docker exec -it 'container-id-app' /bin/sh

telnet db 3306
  • Browse the application from public internet browser to verify the application is accessible.

Conclusion

Once the project setup has been completed, do not forget to clean up resources to avoid the billing for your test environment. Run docker-compose down command to stop and remove the containers and finally, terminate the instance from AWS Console.


This content originally appeared on DEV Community and was authored by divine kenechukwu


Print Share Comment Cite Upload Translate Updates
APA

divine kenechukwu | Sciencx (2024-09-14T21:28:37+00:00) Deploy Simple Java Application using Docker Compose.. Retrieved from https://www.scien.cx/2024/09/14/deploy-simple-java-application-using-docker-compose/

MLA
" » Deploy Simple Java Application using Docker Compose.." divine kenechukwu | Sciencx - Saturday September 14, 2024, https://www.scien.cx/2024/09/14/deploy-simple-java-application-using-docker-compose/
HARVARD
divine kenechukwu | Sciencx Saturday September 14, 2024 » Deploy Simple Java Application using Docker Compose.., viewed ,<https://www.scien.cx/2024/09/14/deploy-simple-java-application-using-docker-compose/>
VANCOUVER
divine kenechukwu | Sciencx - » Deploy Simple Java Application using Docker Compose.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/14/deploy-simple-java-application-using-docker-compose/
CHICAGO
" » Deploy Simple Java Application using Docker Compose.." divine kenechukwu | Sciencx - Accessed . https://www.scien.cx/2024/09/14/deploy-simple-java-application-using-docker-compose/
IEEE
" » Deploy Simple Java Application using Docker Compose.." divine kenechukwu | Sciencx [Online]. Available: https://www.scien.cx/2024/09/14/deploy-simple-java-application-using-docker-compose/. [Accessed: ]
rf:citation
» Deploy Simple Java Application using Docker Compose. | divine kenechukwu | Sciencx | https://www.scien.cx/2024/09/14/deploy-simple-java-application-using-docker-compose/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.