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:
parent
71744993ce
commit
8cdb099fb6
5 changed files with 59 additions and 116 deletions
|
@ -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>
|
|
48
Dockerfile
48
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)
|
# 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 VERSION=latest
|
||||||
ARG IMAGE_NAME=bigwiz/shortener
|
ARG IMAGE_NAME=nmpl/shortener
|
||||||
ARG TAG=$VERSION
|
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
|
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
|
RUN git clone https://github.com/aamitn/URLShortener.git
|
||||||
|
|
||||||
|
#Local Install
|
||||||
|
#ADD . /UrlShortener
|
||||||
|
|
||||||
|
# Change working directory to the repo directory
|
||||||
WORKDIR /URLShortener
|
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
|
# Build the application
|
||||||
RUN mvn clean install
|
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
|
FROM tomcat:10-jdk21-openjdk-slim
|
||||||
|
|
||||||
# Set environment variables
|
# Set environment variables
|
||||||
|
@ -24,29 +44,19 @@ ENV CATALINA_BASE /usr/local/tomcat
|
||||||
ENV CATALINA_HOME /usr/local/tomcat
|
ENV CATALINA_HOME /usr/local/tomcat
|
||||||
ENV PATH $CATALINA_HOME/bin:$PATH
|
ENV PATH $CATALINA_HOME/bin:$PATH
|
||||||
|
|
||||||
|
|
||||||
# Copy the WAR file from the builder stage
|
# Copy the WAR file from the builder stage
|
||||||
COPY --from=builder /URLShortener/target/shortener.war $CATALINA_BASE/webapps/
|
COPY --from=builder /URLShortener/target/shortener.war $CATALINA_BASE/webapps/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
#------STAGE 3: Configure and Start Application Server-----#
|
||||||
|
#
|
||||||
# Add configuration for document base path
|
# Add configuration for document base path
|
||||||
COPY server.xml $CATALINA_BASE/conf/server.xml
|
COPY server.xml $CATALINA_BASE/conf/server.xml
|
||||||
|
|
||||||
|
|
||||||
# Expose ports
|
# Expose ports
|
||||||
EXPOSE 8080
|
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
|
# Start Tomcat and MariaDB using the startup script
|
||||||
CMD ["sh", "/usr/local/tomcat/shortener.sh"]
|
CMD ["catalina.sh", "run"]
|
||||||
|
|
|
@ -1,37 +1,47 @@
|
||||||
version: '3.8'
|
version: '3.8'
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
dbdata:
|
||||||
|
name: dbdata
|
||||||
|
|
||||||
services:
|
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:
|
labels:
|
||||||
- "TUSC The URL Shortener Company"
|
- "TUSC The URL Shortener Company"
|
||||||
|
|
||||||
#Build from docker hub image .Comment/Uncomment Below
|
#Build from docker hub image .Comment/Uncomment Below
|
||||||
image: nmpl/shortener:latest
|
image: nmpl/shortener:latest
|
||||||
|
|
||||||
#Build from local Dockerfile.Comment/Uncomment Below
|
# Build from local Dockerfile.Comment/Uncomment Below
|
||||||
# build:
|
# build:
|
||||||
# context: .
|
# context: .
|
||||||
# dockerfile: Dockerfile
|
# dockerfile: Dockerfile
|
||||||
|
|
||||||
ports:
|
|
||||||
- "8080:8080"
|
|
||||||
- "3306:3306"
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
- shortener-db-data:/var/lib/mysql
|
|
||||||
- type: volume
|
|
||||||
source: shortener-db-data
|
|
||||||
target: /var/lib/mysql
|
|
||||||
|
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: [ "CMD", "curl", "-f", "http://localhost:8080/monitoring" ]
|
test: [ "CMD", "curl", "-f", "http://localhost:8080/monitoring" ]
|
||||||
interval: 30s
|
interval: 30s
|
||||||
timeout: 10s
|
timeout: 10s
|
||||||
retries: 3
|
retries: 3
|
||||||
start_period: 60s
|
start_period: 30s
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
|
||||||
volumes:
|
|
||||||
shortener-db-data:
|
|
||||||
name: shortener-db-data
|
|
4
pom.xml
4
pom.xml
|
@ -180,8 +180,6 @@
|
||||||
<build>
|
<build>
|
||||||
<finalName>${project.artifactId}</finalName>
|
<finalName>${project.artifactId}</finalName>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
|
@ -201,7 +199,6 @@
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
@ -232,7 +229,6 @@
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
|
|
||||||
|
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
||||||
|
|
37
shortener.sh
37
shortener.sh
|
@ -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
|
|
Loading…
Reference in a new issue