Skip to content

leoimewore/DockerizedApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

faf8341 · Oct 21, 2023

History

11 Commits
Oct 21, 2023
Oct 21, 2023
Oct 21, 2023
Oct 21, 2023
Oct 21, 2023
Oct 21, 2023

Repository files navigation

DockerizedApp

Objectives

  • The goal of this project to deploy Java application stack (Nginx, Tomcat, MySQL) using docker-compose
  • Use Maven to build the JAVA project and deploy war file on Tomcat
  • Create a dockerfile to build a tomcat image on amazonlinux with the proper dependencies
image

Steps

  • Install Maven and Java on my local terminal
  • Fork and clone the Java app source code on my local machine : https://bitbucket.org/dptrealtime/java-login-app/src/master/
  • Create a Dockerfile to build tomcat on linux
  • Create docker-compose file with services for proxy-server, tomcat server and database
  • Log into the SQL container and create DB Schema for the application

Directory for project

image
  • Ensure the Dockerfile is placed inside the java-login-app folder

Docker file to build tomcat on amazon linux

 FROM amazonlinux:2.0.20230727.0

  RUN mkdir /opt/tomcat/
  WORKDIR /opt/tomcat
  RUN yum install java-1.8.0-openjdk -y wget
  RUN yum install tar -y
  RUN yum install gzip -y
  RUN yum install wget -y
  RUN yum install mysql -y && yum -y install telnet
  
  ADD https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.82/bin/apache-tomcat-9.0.82.tar.gz .
  RUN tar xvfz apache*.tar.gz
  
  RUN mv apache-tomcat-9.0.82/* /opt/tomcat
  
  COPY target/dptweb-1.0.war /opt/tomcat/webapps
  
  RUN rm /opt/tomcat/conf/tomcat-users.xml
  RUN rm /opt/tomcat/webapps/manager/META-INF/context.xml
  COPY tomcat-users.xml /opt/tomcat/conf/
  COPY context.xml /opt/tomcat/webapps/manager/META-INF/
  
  
  EXPOSE 8080 
  
  
  CMD ["/opt/tomcat/bin/catalina.sh","run"]

  

  using http:localhost or IPAdd:8080 should give you


 BUILD: docker build -t tomcat .
 RUN :  docker run -d -p 8080:8080 tomcat

image
  • This shows that your Tomcat server is running and the war file of the Java app has been deployed on Tomcat.

  • We don't want the Java app to be accessed through TOMCAT so I won't be exposing the Tomcat port in the docker-compose file ( We will access through a proxy server)

In the docker-project directory create a docker-compose yaml file to create three services: proxy_web, app(tomcat), and DB(database)

version: "3.8"

services:
  proxy-web:
    image: nginx:latest
    ports:
      - "80:80"

    volumes:
      - ./proxyConf:/etc/nginx/conf.d
    

  app:
    build: ./java-login-app
    hostname: tomcat

    volumes:
      - tomcat:/usr/local/tomcat/webapps

  db: 
    image: mysql:8.1.0-oracle
    ports:
      - "3306:3306"

    volumes:
      - mysql-data:/var/lib/mysql

    environment:
      - MYSQL_ROOT_PASSWORD=Admin123
      - MYSQL_DATABASE=UserDB
      - MYSQL_USER=admin
      - MYSQL_PASSWORD:=Admin123

  
volumes:
  nginx_data:
  mysql-data:
  tomcat:

  • Build : docker-compose up -d

  • Very Important that i changed the /etc/nginx/conf.d folder in the proxy_web to help have access to the tomcat server

    server {
      listen       80;
      server_name  localhost;
    
      #access_log  /var/log/nginx/host.access.log  main;
    
      location / {
          proxy_pass http://tomcat:8080/dptweb-1.0/;    ## Important
      }
    
      #error_page  404              /404.html;
    
      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   /usr/share/nginx/html;
      }
    
      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      #    proxy_pass   http://127.0.0.1;
      #}
    
      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      #    root           html;
      #    fastcgi_pass   127.0.0.1:9000;
      #    fastcgi_index  index.php;
      #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
      #    include        fastcgi_params;
      #}
    
      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #    deny  all;
      #}
     }
    
    
    • docker ps shows the running containers image

    • localhost://80 the java app is now accessible through the nginx(proxy_web) port 80 image

    The Next step is to verify that the SQL database is functioning

    image
    • Inside the java source file ensure that the application.properties file is configured with mysql URL, USERNAME, AND PASSWORD before building with Maven image

    The next step is to get the IP address of the mysql container

    docker inspect <containerID> | grep Gateway
    
    • next access the database container:

        docker-compose exec db /bin/bash
        mysql -h <GatewayIP>  -P 3306 -u <USERNAME> -p
      
      image

      Follow Directions given in the READ ME FILE of the Project and Create a new user on the UI.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published