Springify and dockerize project (fixed and improved) #2

Merged
lsmyrnaios merged 17 commits from springify_project into master 2022-02-04 14:56:17 +01:00
3 changed files with 64 additions and 7 deletions
Showing only changes of commit b206114144 - Show all commits

View File

@ -1,7 +1,7 @@
FROM openjdk:8-jdk-alpine FROM openjdk:8-jdk-alpine
COPY build/libs/*-SNAPSHOT.jar app.jar COPY build/libs/*-SNAPSHOT.jar urls_controller.jar
EXPOSE 1880 EXPOSE 1880
ENTRYPOINT ["java","-jar","/app.jar", "--spring.config.location=file:///mnt/config/application.properties"] ENTRYPOINT ["java","-jar","/urls_controller.jar", "--spring.config.location=file:///mnt/config/application.properties"]

View File

@ -1,11 +1,20 @@
cd "${0%/*}" || (echo "Could not chdir to this script's dir!" && exit) # Change the working directory to the script's directory, when running from other location. cd "${0%/*}" || (echo "Could not chdir to this script's dir!" && exit) # Change the working directory to the script's directory, when running from other location.
justInstall=0 justInstall=0
shouldRunInDocker=0
if [[ $# -eq 1 ]]; then if [[ $# -eq 1 ]]; then
justInstall=$1 justInstall=$1
elif [[ $# -gt 1 ]]; then elif [[ $# -eq 2 ]]; then
echo -e "Wrong number of arguments given: ${#}\nPlease execute it like: script.sh <justInstall: 0 | 1>"; exit 1 justInstall=$1
shouldRunInDocker=$2
elif [[ $# -gt 2 ]]; then
echo -e "Wrong number of arguments given: ${#}\nPlease execute it like: script.sh <justInstall: 0 | 1> <shouldRunInDocker: 0 | 1>"; exit 1
fi
if [[ justInstall -eq 1 && shouldRunInDocker -eq 1 ]]; then
echo -e "Cannot run in docker without re-building the project (just to be safe). Setting \"justInstall\" to < 0 >"
justInstall=0
fi fi
gradleVersion="7.3.3" gradleVersion="7.3.3"
@ -27,10 +36,25 @@ if [[ justInstall -eq 0 ]]; then
#gradle tasks # For debugging installation #gradle tasks # For debugging installation
#gradle -v # For debugging installation #gradle -v # For debugging installation
gradle clean gradle clean build
gradle build
if [[ shouldRunInDocker -eq 1 ]]; then
echo "Give the username for the Docker Hub:"
read -r username
echo -e "\nBuilding docker image..\n"
sudo docker --version || (echo -e "Docker was not found!"; exit 9)
dockerImage=${username}"/urls_controller:latest"
sudo docker build -t "${dockerImage}" .
echo -e "\nPushing docker image.. (the account password is required)..\n"
(sudo docker login -u "${username}" && sudo docker push "${dockerImage}") || true
(sudo mkdir -p "$HOME"/tmp/config && sudo cp ./src/main/resources/application.properties "$HOME"/tmp/config) || true
sudo docker run -d --mount type=bind,source="$HOME"/tmp/config,target=/mnt/config -p 1880:1880 "${dockerImage}" && echo "The docker container started running."
# Run in "detached mode" (in the background).
fi
else else
export PATH=/opt/gradle/gradle-${gradleVersion}/bin:$PATH # Make sure the gradle is still accessible (it usually isn't without the "export"). export PATH=/opt/gradle/gradle-${gradleVersion}/bin:$PATH # Make sure the gradle is still accessible (it usually isn't without the "export").
fi fi
gradle bootRun if [[ shouldRunInDocker -ne 1 ]]; then
gradle bootRun
fi

View File

@ -0,0 +1,33 @@
<configuration debug="false">
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/UrlsController.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>logs/UrlsController.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>20</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>50MB</maxFileSize>
</triggeringPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(@%line\) - %msg%n</pattern>
</encoder>
</appender>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{36}.%M\(@%line\)) - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="RollingFile" />
</root>
</configuration>