From 8cdb099fb66b74a2645a5f4ac48db519b9367c86 Mon Sep 17 00:00:00 2001 From: Amit Kumar Nandi <11887616+aamitn@users.noreply.github.com> Date: Thu, 22 Feb 2024 00:43:50 +0530 Subject: [PATCH] changed dockerfile and docker-compose.yml Main app container and db are segreegated to 2 different containers. Modified compose file to orchestrate the containers accordingly on a single deployment --- .m2/settings.xml | 36 --------------------------------- Dockerfile | 48 ++++++++++++++++++++++++++------------------ docker-compose.yml | 50 +++++++++++++++++++++++++++------------------- pom.xml | 4 ---- shortener.sh | 37 ---------------------------------- 5 files changed, 59 insertions(+), 116 deletions(-) delete mode 100644 .m2/settings.xml delete mode 100644 shortener.sh diff --git a/.m2/settings.xml b/.m2/settings.xml deleted file mode 100644 index dee273e..0000000 --- a/.m2/settings.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - github - - - - - github - - - central - https://repo1.maven.org/maven2 - - - github - https://maven.pkg.github.com/aamitn/URLShortener - - true - - - - - - - - - github - aamitn - #TOKEN# - - - \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 7501ac4..9924105 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,21 +2,41 @@ # Add the following lines to tag the image (replace 'your_username' and 'shortener-app' with your Docker Hub username and repository name) ARG VERSION=latest -ARG IMAGE_NAME=bigwiz/shortener +ARG IMAGE_NAME=nmpl/shortener ARG TAG=$VERSION -# Stage 1: Build the application + + +# +#------STAGE 1: Build the application-----# +# +# Get Maven with JDK 21 FROM maven:3.9.6-eclipse-temurin-21 AS builder -# Clone the repository +#Cloud Install : Clone the repository RUN git clone https://github.com/aamitn/URLShortener.git +#Local Install +#ADD . /UrlShortener + +# Change working directory to the repo directory WORKDIR /URLShortener +# Docker makes db accessible like this : mysql://:port instead of mysql://:port +# Example real world db access url : mysql://127.0.0.1:3306 +# Example Docker db access url : mysql://database:3306 (container name is datbase) +# Change the database ip in app config to the database docker container name/service +RUN sed -i "s|database.ip=127.0.0.1|database.ip=db |g" src/main/resources/application.properties + # Build the application RUN mvn clean install -# Stage 2: Create the final image + + +# +#------STAGE 2: Deploy the Generated War-----# +# +## Get Tomacat 10 with JDK21 FROM tomcat:10-jdk21-openjdk-slim # Set environment variables @@ -24,29 +44,19 @@ ENV CATALINA_BASE /usr/local/tomcat ENV CATALINA_HOME /usr/local/tomcat ENV PATH $CATALINA_HOME/bin:$PATH - # Copy the WAR file from the builder stage COPY --from=builder /URLShortener/target/shortener.war $CATALINA_BASE/webapps/ + +# +#------STAGE 3: Configure and Start Application Server-----# +# # Add configuration for document base path COPY server.xml $CATALINA_BASE/conf/server.xml - # Expose ports EXPOSE 8080 -EXPOSE 3306 - - -# Copy the startup script -COPY shortener.sh /usr/local/tomcat/shortener.sh - -# Copy the sql file -COPY create.sql /usr/local/tomcat/create.sql - -# Grant execute permissions to the startup.sh script -RUN chmod +x /usr/local/tomcat/shortener.sh # Start Tomcat and MariaDB using the startup script -CMD ["sh", "/usr/local/tomcat/shortener.sh"] - +CMD ["catalina.sh", "run"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 53caa15..df8265f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,37 +1,47 @@ version: '3.8' +volumes: + dbdata: + name: dbdata + services: - shortener-app: + db: + image: mariadb + environment: + MYSQL_ROOT_PASSWORD: 1234qwer + MYSQL_DATABASE: shortener + MYSQL_USER: shortener_user + MYSQL_PASSWORD: 1234qwer + volumes: + - data:/var/lib/mysql + - type: volume + source: dbdata + target: /var/lib/mysql + - ./create.sql:/docker-entrypoint-initdb.d/init.sql + ports: + - "3306:3306" + + app: labels: - "TUSC The URL Shortener Company" - #Build from docker hub image .Comment/Uncomment Below image: nmpl/shortener:latest - #Build from local Dockerfile.Comment/Uncomment Below - # build: - # context: . - # dockerfile: Dockerfile - - ports: - - "8080:8080" - - "3306:3306" - - volumes: - - shortener-db-data:/var/lib/mysql - - type: volume - source: shortener-db-data - target: /var/lib/mysql + # Build from local Dockerfile.Comment/Uncomment Below + # build: + # context: . + # dockerfile: Dockerfile healthcheck: test: [ "CMD", "curl", "-f", "http://localhost:8080/monitoring" ] interval: 30s timeout: 10s retries: 3 - start_period: 60s + start_period: 30s restart: unless-stopped + ports: + - "8080:8080" + depends_on: + - db -volumes: - shortener-db-data: - name: shortener-db-data \ No newline at end of file diff --git a/pom.xml b/pom.xml index e86b44c..44f84a7 100644 --- a/pom.xml +++ b/pom.xml @@ -180,8 +180,6 @@ ${project.artifactId} - - @@ -201,7 +199,6 @@ - org.springframework.boot spring-boot-maven-plugin @@ -232,7 +229,6 @@ - diff --git a/shortener.sh b/shortener.sh deleted file mode 100644 index daf110a..0000000 --- a/shortener.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/bash - -echo "Installing MariaDB..." -# Install MariaDB -apt-get update -apt-get install -y mariadb-server - -echo "Starting MariaDB service..." -# Start MariaDB service -service mariadb start - -echo "Waiting for MariaDB to start (adjust sleep time as needed)..." - -# Wait for MariaDB to start -while ! mysqladmin ping -hlocalhost -uroot -p'YOUR_PASSWORD' --silent; do - echo "MariaDB is not yet available. Waiting..." - sleep 5 -done - -echo "Running SQL script to initialize the database..." -# Access MySQL Command Line and run SQL script to initialize the database -mysql -u root -e "source /usr/local/tomcat/create.sql" - -echo "Displaying databases and tables..." -# Display the databases and tables -mysql -u root -e "SHOW DATABASES; USE shortener; SHOW TABLES;" - -echo "Altering user and reloading privileges..." -# Run SQL commands to alter user and reload privileges -mysql -u root <