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
This commit is contained in:
Amit Kumar Nandi 2024-02-22 00:43:50 +05:30
parent 71744993ce
commit 8cdb099fb6
5 changed files with 59 additions and 116 deletions

View file

@ -1,36 +0,0 @@
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/aamitn/URLShortener</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>aamitn</username>
<password>#TOKEN#</password>
</server>
</servers>
</settings>

View file

@ -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://<container-name>:port instead of mysql://<server-ip>: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"]

View file

@ -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

View file

@ -180,8 +180,6 @@
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
@ -201,7 +199,6 @@
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
@ -232,7 +229,6 @@
</plugins>
</build>

View file

@ -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 <<EOF
ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('1234qwer');
FLUSH PRIVILEGES;
EOF
echo "Starting Tomcat in the background..."
# Start Tomcat in the background
sh /usr/local/tomcat/bin/catalina.sh run