From 0cb51209f42c7a3ba7389d8e8daa34bed52d08c4 Mon Sep 17 00:00:00 2001 From: septian wijaya Date: Mon, 19 Feb 2024 14:48:17 +0700 Subject: [PATCH 1/8] dockerfile (trial) --- Dockerfile | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d0b373f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM maven:3.6.3-openjdk-17-slim AS build_stage + +#set working dir as an active folder on container +WORKDIR /app + +COPY pom.xml . +COPY src ./src + +# build app using maven +RUN mvn clean package -DskipTests + + +#user openJDK as base image +FROM openjdk:17.0.2-jdk-slim + +WORKDIR /app + +#copy built jar from prev stage (maven as Build) to container +COPY --from=build_stage /app/build/app.jar . + +#command to run the app +CMD [ "java", "-jar", "app.js" ] + + From ffd1d0eb98031b129ac72f3f652154b7235d7728 Mon Sep 17 00:00:00 2001 From: septian wijaya Date: Mon, 19 Feb 2024 18:00:37 +0700 Subject: [PATCH 2/8] downgrade to java 17 & docker compose (error connection DB) --- Dockerfile | 52 +++++++++++++++++++++++------- docker-compose.yml | 37 +++++++++++++++++++++ pom.xml | 11 ++++--- src/main/resources/application.yml | 13 +++----- 4 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile index d0b373f..c4be0ee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,24 +1,52 @@ +# FROM maven:3.6.3-openjdk-17-slim AS build_stage + +# #set working dir as an active folder on container +# WORKDIR /app + +# COPY pom.xml . +# COPY src ./src + +# # build app using maven +# RUN mvn clean package -DskipTests + + +# #user openJDK as base image +# FROM openjdk:17.0.2-jdk-slim + +# WORKDIR /app + +# #copy built jar from prev stage (maven as Build) to container +# COPY --from=build_stage /app/build/app.jar . + +# #command to run the app +# CMD [ "java", "-jar", "app.js" ] + + FROM maven:3.6.3-openjdk-17-slim AS build_stage -#set working dir as an active folder on container -WORKDIR /app +# # # Build the application +# # RUN ./mvnw clean package -DskipTests +# # Copy Maven files for dependency resolution COPY pom.xml . +# COPY .mvn .mvn + +# # Copy application source code +# COPY . . +RUN mvn -e -B dependency:resolve COPY src ./src -# build app using maven -RUN mvn clean package -DskipTests +# Build the project and create the executable JAR +RUN mvn clean -e -B package -DskipTests -#user openJDK as base image -FROM openjdk:17.0.2-jdk-slim +FROM eclipse-temurin:17.0.10_7-jre-jammy +# Set the working directory WORKDIR /app -#copy built jar from prev stage (maven as Build) to container -COPY --from=build_stage /app/build/app.jar . - -#command to run the app -CMD [ "java", "-jar", "app.js" ] - +# Copy the built JAR file from the build stage +COPY --from=build_stage /target/*.jar . +# Command to run the application +CMD ["java", "-jar", "sekolah_app.1.0.0.jar"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0de15b9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,37 @@ +version: "3.8" + +services: + app: + build: + context: . + dockerfile: Dockerfile + + container_name: sekolah_app + ports: + - "8080:8080" + environment: + - SPRING_PROFILES_ACTIVE=dev + depends_on: + - db + volumes: + - ./logs:/app/logs + networks: + - sekolah-network + + db: + image: postgres:15-alpine + container_name: sekolah_db + environment: + - POSTGRES_USER=sekolah + - POSTGRES_PASSWORD=sekolah_password + - POSTGRES_DB=sekolah + volumes: + - sekolah-data:/var/libposgresql/data + networks: + - sekolah-network + +volumes: + sekolah-data: + +networks: + sekolah-network: diff --git a/pom.xml b/pom.xml index 3d36fd6..f56334b 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ Demo project for Spring Boot - 21 + 17 @@ -150,6 +150,7 @@ + sekolah_app.1.0.0 org.springframework.boot @@ -170,7 +171,7 @@ - + org.apache.maven.plugins maven-compiler-plugin 3.12.1 - 21 - 21 + 17 + 17 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 00f557d..57ece75 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,8 +1,8 @@ spring: datasource: url: jdbc:postgresql://localhost:5432/sekolah - username: adminSekolah - password: passwordSekolah + username: sekolah + password: sekolah_password jpa: open-in-view: false show-sql: true @@ -15,7 +15,7 @@ spring: enabled: true check-location: true locations: classpath:/db/migration -# Templates reloading during development + # Templates reloading during development thymeleaf: cache: false check-template: true @@ -25,7 +25,7 @@ spring: mode: HTML # prefix: classpath:/templates/ prefix: file:src/main/resources/templates/ -# prefix: ${project.basedir}/src/main/resources/templates/ + # prefix: ${project.basedir}/src/main/resources/templates/ suffix: .html # excluded-view-names: # template-resolver-order: @@ -69,8 +69,5 @@ server: # 1 days = 1 * 24 * 60 * 60 cookie: maxAge: 86400 - - - #hendisantika/password -#admin/password \ No newline at end of file +#admin/password From 538867a13a956a9524ab861e44576b89d282ede7 Mon Sep 17 00:00:00 2001 From: septian wijaya Date: Mon, 4 Mar 2024 17:37:24 +0700 Subject: [PATCH 3/8] run vesion - but index template not found --- Dockerfile | 30 -------------------- README.md | 44 ++++++++++++++++++++++++++---- docker-compose.yml | 3 ++ src/main/resources/application.yml | 8 +++--- 4 files changed, 45 insertions(+), 40 deletions(-) diff --git a/Dockerfile b/Dockerfile index c4be0ee..66955f3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,38 +1,8 @@ -# FROM maven:3.6.3-openjdk-17-slim AS build_stage - -# #set working dir as an active folder on container -# WORKDIR /app - -# COPY pom.xml . -# COPY src ./src - -# # build app using maven -# RUN mvn clean package -DskipTests - - -# #user openJDK as base image -# FROM openjdk:17.0.2-jdk-slim - -# WORKDIR /app - -# #copy built jar from prev stage (maven as Build) to container -# COPY --from=build_stage /app/build/app.jar . - -# #command to run the app -# CMD [ "java", "-jar", "app.js" ] - - FROM maven:3.6.3-openjdk-17-slim AS build_stage -# # # Build the application -# # RUN ./mvnw clean package -DskipTests -# # Copy Maven files for dependency resolution COPY pom.xml . -# COPY .mvn .mvn -# # Copy application source code -# COPY . . RUN mvn -e -B dependency:resolve COPY src ./src diff --git a/README.md b/README.md index 2502b1c..22a7c0a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # sekolah + MYSCHOOL adalah source code opensource yang dibangun mengunakan Spring Boot 2.6.3 version yang terinspirasi dari MSCHOOL yang dibangun menggunakan Codeigniter 3.1.9 dan Bootstrap 4.0. @@ -23,10 +24,8 @@ Bagaimana mengonline-kannya? - Buat database di hosting (di sini saya menggunakan PostgreSQL) - Jika Anda mengalami kesulitan saat mengonlinekannya, Anda dapat meminta bantuan pihak hosting untuk membantu Anda mengonlinekannya. - Modul dan Fitur: 1. Dashboard (Unique Visitors Counter) @@ -60,16 +59,21 @@ Modul dan Fitur: 15. SEO URL Friendly ## Cara Membuat Database + 1. Create user untuk connect ke database + ``` createuser -P adminSekolah ``` 2. Create database untuk user tersebut + ``` createdb -OadminSekolah sekolah ``` + Cara install uuid-ossp module, anda harus CREATE EXTENSION statement seperti berikut ini: + ``` CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; ``` @@ -96,6 +100,30 @@ docker run --rm \ postgres:15 ``` +or just run the command + +``` +docker-compose up +``` + +everytime doing change on application \*case like change url from + +``` +spring.datasource.url=jdbc:postgresql://localhost:5432/sekolah +``` + +to + +``` +spring.datasource.url=jdbc:postgresql://db:5432/sekolah +``` + +you need to rebuild the image using + +```shell +docker-compose build [service_app_defined_on_docker_compose_file: ex: app] +``` + ### Cara konek ke Postgres Database via Docker ```shell @@ -110,28 +138,32 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; Di sini saya berasumsi bahawa JDK8, maven & PostgreSQL Database sudah terinstal di lokal anda masing-masing. -* Untuk menjalankan aplikasinya cukup ketik perintah di bawah ini d terminal: +- Untuk menjalankan aplikasinya cukup ketik perintah di bawah ini d terminal: ```shell mvn clean spring-boot:run ``` +- Untuk membungkusnya menjadi paket jar anda bisa menggunakan perintah berikut ini: -* Untuk membungkusnya menjadi paket jar anda bisa menggunakan perintah berikut ini: ``` mvn clean package ``` -* Untuk menjalankan jar dari paket tsb bisa dengan perintah berikut ini: +- Untuk menjalankan jar dari paket tsb bisa dengan perintah berikut ini: + ``` java -jar sekolah.jar ``` + Aplikasi bisa dilihat di Heroku: https://sekolah1.herokuapp.com/ adminnya di https://sekolah1.herokuapp.com/admin ## Contributing + Please see [CONTRIBUTING](CONTRIBUTING.md) for details. -## Listed by *Hendi Santika* +## Listed by _Hendi Santika_ + - Email: hendisantika@gmail.com / hendisantika@yahoo.co.id - Telegram: [@hendisantika34](https://t.me/hendisantika34) - Linktree: https://linktr.ee/hendisantika diff --git a/docker-compose.yml b/docker-compose.yml index 0de15b9..b3237ab 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,8 +25,11 @@ services: - POSTGRES_USER=sekolah - POSTGRES_PASSWORD=sekolah_password - POSTGRES_DB=sekolah + - PGDATA=/var/lib/postgresql/data/pgdata volumes: - sekolah-data:/var/libposgresql/data + ports: + - "5432:5432" networks: - sekolah-network diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 57ece75..ed6bd28 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,6 +1,6 @@ spring: datasource: - url: jdbc:postgresql://localhost:5432/sekolah + url: jdbc:postgresql://db:5432/sekolah username: sekolah password: sekolah_password jpa: @@ -19,12 +19,12 @@ spring: thymeleaf: cache: false check-template: true - check-template-location: true + check-template-location: false enabled: true encoding: UTF-8 mode: HTML - # prefix: classpath:/templates/ - prefix: file:src/main/resources/templates/ + prefix: classpath:/templates/ + # prefix: file:src/main/resources/templates/ # prefix: ${project.basedir}/src/main/resources/templates/ suffix: .html # excluded-view-names: From f4bbe9f2b44f7e0f94908f28375c5d4e06e20c9a Mon Sep 17 00:00:00 2001 From: septian wijaya Date: Tue, 25 Jun 2024 14:26:07 +0700 Subject: [PATCH 4/8] fix templates path & finalize the docker compose setup --- Dockerfile | 22 - Dockerfile-dev | 27 + README.md | 58 ++- docker-compose.yml => docker-compose-dev.yml | 8 +- src/main/resources/application.yml | 8 +- .../fragments/dashboard/sidebar.html | 343 ++++++------- .../resources/templates/fragments/header.html | 461 +++++++++++------- src/main/resources/templates/index.html | 224 +++++---- 8 files changed, 654 insertions(+), 497 deletions(-) delete mode 100644 Dockerfile create mode 100644 Dockerfile-dev rename docker-compose.yml => docker-compose-dev.yml (67%) diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 66955f3..0000000 --- a/Dockerfile +++ /dev/null @@ -1,22 +0,0 @@ -FROM maven:3.6.3-openjdk-17-slim AS build_stage - - -COPY pom.xml . - -RUN mvn -e -B dependency:resolve -COPY src ./src - -# Build the project and create the executable JAR -RUN mvn clean -e -B package -DskipTests - - -FROM eclipse-temurin:17.0.10_7-jre-jammy - -# Set the working directory -WORKDIR /app - -# Copy the built JAR file from the build stage -COPY --from=build_stage /target/*.jar . - -# Command to run the application -CMD ["java", "-jar", "sekolah_app.1.0.0.jar"] \ No newline at end of file diff --git a/Dockerfile-dev b/Dockerfile-dev new file mode 100644 index 0000000..36762c9 --- /dev/null +++ b/Dockerfile-dev @@ -0,0 +1,27 @@ +FROM maven:3.6.3-openjdk-17-slim AS build_stage + +WORKDIR /app_build + +COPY pom.xml . + +RUN mvn -e -B dependency:resolve +COPY src ./src + +# Build the project and create the executable JAR +RUN mvn clean -e -B package -DskipTests + +# Copy the JAR file from the container to the local machine +# Command to copy the JAR file from the container to the local machine +# CMD ["sh", "-c", "find /target -name '*.jar' -exec cp {} /target/ \\;"] + + +# FROM eclipse-temurin:17.0.10_7-jre-jammy + +# # Set the working directory +# WORKDIR /app + +# # Copy the built JAR file from the build stage +# COPY target/*.jar app.jar + +# # Command to run the application +# CMD ["java", "-jar", "app.jar"] \ No newline at end of file diff --git a/README.md b/README.md index 22a7c0a..2e67db4 100644 --- a/README.md +++ b/README.md @@ -100,30 +100,6 @@ docker run --rm \ postgres:15 ``` -or just run the command - -``` -docker-compose up -``` - -everytime doing change on application \*case like change url from - -``` -spring.datasource.url=jdbc:postgresql://localhost:5432/sekolah -``` - -to - -``` -spring.datasource.url=jdbc:postgresql://db:5432/sekolah -``` - -you need to rebuild the image using - -```shell -docker-compose build [service_app_defined_on_docker_compose_file: ex: app] -``` - ### Cara konek ke Postgres Database via Docker ```shell @@ -158,6 +134,40 @@ java -jar sekolah.jar Aplikasi bisa dilihat di Heroku: https://sekolah1.herokuapp.com/ adminnya di https://sekolah1.herokuapp.com/admin +### Cara menjalankan via Docker-Compose + +Build image untuk menjalankan container + +```shell +docker-compose -f build +``` + +jalankan container dari image yang telah dibuat + +```shell +docker-compose -f up +``` + +setiap kali ada perubahan pada konfigurasi file di aplikasi seperti + +_from_ + +```shell +spring.datasource.url=jdbc:postgresql://localhost:5432/sekolah +``` + +_to_ + +```shell +spring.datasource.url=jdbc:postgresql://db:5432/sekolah +``` + +perlu untuk melakukan _build_ ulang + +```shell +docker-compose -f build [service_app_defined_on_docker_compose_file: ex: app or db] +``` + ## Contributing Please see [CONTRIBUTING](CONTRIBUTING.md) for details. diff --git a/docker-compose.yml b/docker-compose-dev.yml similarity index 67% rename from docker-compose.yml rename to docker-compose-dev.yml index b3237ab..94e893d 100644 --- a/docker-compose.yml +++ b/docker-compose-dev.yml @@ -4,8 +4,9 @@ services: app: build: context: . - dockerfile: Dockerfile + dockerfile: Dockerfile-dev + image: spring_sekolah container_name: sekolah_app ports: - "8080:8080" @@ -14,9 +15,12 @@ services: depends_on: - db volumes: - - ./logs:/app/logs + - ./logs:/app_build/logs + - ./src:/app_build/src:delegated # mount the local source code has directly access to container dir for convenient access when has change on source code + - ./target:/app_build/target:delegated networks: - sekolah-network + command: mvn spring-boot:run db: image: postgres:15-alpine diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ed6bd28..08f7d30 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -23,9 +23,9 @@ spring: enabled: true encoding: UTF-8 mode: HTML - prefix: classpath:/templates/ - # prefix: file:src/main/resources/templates/ - # prefix: ${project.basedir}/src/main/resources/templates/ + # prefix: file:src/main/resources/templates/, classpath:/templates/ + prefix: file:src/main/resources/templates/ + # prefix: ${project.basedir}/src/main/resources/templates/ suffix: .html # excluded-view-names: # template-resolver-order: @@ -37,7 +37,7 @@ spring: enabled: true add-properties: true resources: - static-locations: file:src/main/resources/static/ + static-locations: file:src/main/resources/static/, classpath:static/ # static-locations: ${project.basedir}/src/main/resources/static/ cache: period: 0 diff --git a/src/main/resources/templates/fragments/dashboard/sidebar.html b/src/main/resources/templates/fragments/dashboard/sidebar.html index 98ce208..b39ddf8 100644 --- a/src/main/resources/templates/fragments/dashboard/sidebar.html +++ b/src/main/resources/templates/fragments/dashboard/sidebar.html @@ -1,173 +1,188 @@ -