From 276319e6ccdd3b109de2e9362f0f2694e7b50791 Mon Sep 17 00:00:00 2001 From: Andrii Honchar Date: Fri, 26 Dec 2025 16:09:20 +0200 Subject: [PATCH 1/3] added docker --- .env.template | 8 ++++ .gitignore | 1 + Dockerfile | 15 +++++++ docker-compose.yml | 55 +++++++++++++++++++++++ pom.xml | 6 +++ src/main/resources/application.properties | 17 ++++--- 6 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 .env.template create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..ac85abb --- /dev/null +++ b/.env.template @@ -0,0 +1,8 @@ +MYSQLDB_ROOT_PASSWORD=your_root_password +MYSQLDB_USERNAME=your_username +MYSQLDB_DATABASE=your_database_name +MYSQLDB_LOCAL_PORT=3307 +MYSQLDB_DOCKER_PORT=3306 + +APP_LOCAL_PORT=8081 +APP_DOCKER_PORT=8080 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 667aaef..bceaed0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ target/ .mvn/wrapper/maven-wrapper.jar !**/src/main/**/target/ !**/src/test/**/target/ +.env ### STS ### .apt_generated diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cd98233 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM eclipse-temurin:17-jdk-alpine AS builder +WORKDIR application +ARG JAR_FILE=target/*.jar +COPY ${JAR_FILE} application.jar +RUN java -Djarmode=layertools -jar application.jar extract + + +FROM eclipse-temurin:17-jdk-alpine +WORKDIR application +COPY --from=builder application/dependencies/ ./ +COPY --from=builder application/spring-boot-loader/ ./ +COPY --from=builder application/snapshot-dependencies/ ./ +COPY --from=builder application/application/ ./ +ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"] +EXPOSE 8080 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d810e80 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,55 @@ +services: + mysqldb: + image: mysql:9.5.0 + container_name: mysqldb + restart: unless-stopped + env_file: + - .env + environment: + MYSQL_ROOT_PASSWORD: ${MYSQLDB_ROOT_PASSWORD} + MYSQL_DATABASE: ${MYSQLDB_DATABASE} + + ports: + - "${MYSQLDB_LOCAL_PORT}:${MYSQLDB_DOCKER_PORT}" + + volumes: + - mysql_data:/var/lib/mysql + + healthcheck: + test: [ "CMD-SHELL", "mysqladmin", "ping", "-h", "localhost" ] + interval: 30s + timeout: 30s + retries: 3 + networks: + - bookstore-network + + app: + image: bookstore + build: . + container_name: bookstore + depends_on: + - mysqldb + + ports: + - "${APP_LOCAL_PORT}:${APP_DOCKER_PORT}" + - "${DEBUG_PORT}:${DEBUG_PORT}" + + env_file: + - .env + + environment: + SPRING_APPLICATION_JSON: '{ + "spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE", + "spring.datasource.username" : "$MYSQLDB_USERNAME", + "spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD", + "spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQLDialect" + }' + JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005" + networks: + - bookstore-network + +volumes: + mysql_data: +networks: + bookstore-network: + driver: bridge \ No newline at end of file diff --git a/pom.xml b/pom.xml index e11ce6f..53fbe81 100644 --- a/pom.xml +++ b/pom.xml @@ -142,6 +142,12 @@ runtime + + org.springframework.boot + spring-boot-docker-compose + 3.1.1 + + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6f4ffb4..abff433 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,9 @@ -spring.application.name=spring -spring.datasource.url=jdbc:mysql://localhost:3306/db?serverTimezone=UTC -spring.datasource.username=root -spring.datasource.password=andriiw010107aA +spring.application.name=bookstore + +spring.datasource.url=jdbc:mysql://\ + localhost:${MYSQLDB_DOCKER_PORT}/${MYSQLDB_DATABASE}?serverTimezone=UTC +spring.datasource.username=${MYSQLDB_USERNAME} +spring.datasource.password=${MYSQLDB_ROOT_PASSWORD} spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=validate @@ -9,6 +11,9 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect spring.jpa.show-sql=true spring.jackson.deserialization.fail-on-unknown-properties=true +spring.config.import=optional:file:.env + +spring.docker.compose.enabled=false -jwt.expiration=500000 -jwt.secret=imustspendmoretimeonmateacademytillendofthe2025 +jwt.expiration=${JWT_EXPIRATION} +jwt.secret=${JWT_SECRET} \ No newline at end of file From 287e33ef32f1d81c208b8df3ca588193ef93e7a7 Mon Sep 17 00:00:00 2001 From: Andrii Honchar Date: Fri, 26 Dec 2025 16:15:39 +0200 Subject: [PATCH 2/3] checkstyle fix --- src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index abff433..d316b7e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -16,4 +16,4 @@ spring.config.import=optional:file:.env spring.docker.compose.enabled=false jwt.expiration=${JWT_EXPIRATION} -jwt.secret=${JWT_SECRET} \ No newline at end of file +jwt.secret=${JWT_SECRET} From 93305ff1f73d570c40a363e0fc584266f5658e2b Mon Sep 17 00:00:00 2001 From: Andrii Honchar Date: Sun, 28 Dec 2025 09:29:54 +0200 Subject: [PATCH 3/3] little fixes --- docker-compose.yml | 2 +- pom.xml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index d810e80..cab1f4a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -44,7 +44,7 @@ services: "spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD", "spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQLDialect" }' - JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005" + JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:$DEBUG_PORT" networks: - bookstore-network diff --git a/pom.xml b/pom.xml index 53fbe81..f7c1ebe 100644 --- a/pom.xml +++ b/pom.xml @@ -145,7 +145,6 @@ org.springframework.boot spring-boot-docker-compose - 3.1.1