diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..12dca72c3 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,39 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/go/build-context-dockerignore/ + +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.next +**/.cache +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/charts +**/docker-compose* +**/compose.y*ml +# **/target +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +**/vendor +LICENSE +README.md +**/gradle +gradlew +gradlew.bat +settings.gradle +Makefile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..cbdd9b54b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM eclipse-temurin:24.0.1_9-jre-alpine-3.21@sha256:dbc9b392f33b2aca2c3d47de4534f3453e75d3b6dd27e08a555a47369be9b49c + +RUN addgroup -S appgroup && adduser -S appuser -G appgroup + +WORKDIR /app + +COPY --chown=appuser:appgroup ./target/*.jar app.jar + +RUN chmod 500 app.jar + +USER appuser + +EXPOSE 8080 + +ENTRYPOINT [ "java", "-jar", "app.jar" ] + diff --git a/Dockerfile.multistage b/Dockerfile.multistage new file mode 100644 index 000000000..c195c45e2 --- /dev/null +++ b/Dockerfile.multistage @@ -0,0 +1,26 @@ +FROM eclipse-temurin:24.0.1_9-jdk-alpine-3.21@sha256:d729416b123cd50d4a70122328ae17d38adced6fa767062d0c0f134e5843deab AS build + +WORKDIR /app + +COPY . . + +RUN ./mvnw dependency:go-offline -B +RUN ./mvnw clean package -DskipTests + +USER nobody + +FROM eclipse-temurin:24.0.1_9-jre-alpine-3.21@sha256:dbc9b392f33b2aca2c3d47de4534f3453e75d3b6dd27e08a555a47369be9b49c as runtime + +RUN addgroup -S appgroup && adduser -S appuser -G appgroup + +WORKDIR /app + +COPY --from=build --chown=appuser:appgroup /app/target/*.jar app.jar + +RUN chmod 500 app.jar + +USER appuser + +EXPOSE 8080 + +ENTRYPOINT [ "java", "-jar", "app.jar" ] \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..b25875b0e --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +.PHONY: up down logs restart ps + +COMPOSE_FILE=docker-compose.yml + +up: + docker compose -f $(COMPOSE_FILE) --env-file .env up -d --build $(service) + +down: + docker compose -f $(COMPOSE_FILE) down $(service) + +logs: + docker compose -f $(COMPOSE_FILE) logs --tail=100 -f $(service) + +ps: + docker compose -f $(COMPOSE_FILE) ps + +restart: down up diff --git a/docker-compose.yml b/docker-compose.yml index 47579bbaf..e233eabb1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,21 +1,46 @@ services: - mysql: - image: mysql:9.1 + petclinic: + image: spring-petclinic-petclinic + build: + context: . + dockerfile: Dockerfile.multistage + container_name: petclinic + depends_on: + postgres: + condition: service_healthy + restart: always ports: - - "3306:3306" + - "8080:8080" environment: - - MYSQL_ROOT_PASSWORD= - - MYSQL_ALLOW_EMPTY_PASSWORD=true - - MYSQL_USER=petclinic - - MYSQL_PASSWORD=petclinic - - MYSQL_DATABASE=petclinic - volumes: - - "./conf.d:/etc/mysql/conf.d:ro" + SPRING_JPA_HIBERNATE_DDL_AUTO: update + SPRING_PROFILES_ACTIVE: postgres + POSTGRES_URL: ${DATABASE_URL} + POSTGRES_PASSWORD: ${DATABASE_PASS} + POSTGRES_USER: ${DATABASE_USER} + postgres: image: postgres:17.0 + container_name: postgres + restart: always ports: - "5432:5432" environment: - - POSTGRES_PASSWORD=petclinic - - POSTGRES_USER=petclinic - - POSTGRES_DB=petclinic + POSTGRES_PASSWORD: ${DATABASE_PASS} + POSTGRES_USER: ${DATABASE_USER} + POSTGRES_DB: ${DATABASE} + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${DATABASE_USER} -d ${DATABASE}"] + interval: 10s + timeout: 10s + retries: 5 + start_period: 10s + +volumes: + postgres_data: + + + + + diff --git a/src/main/resources/application-mysql.properties b/src/main/resources/application-mysql.properties index e23dfa605..453aa83e7 100644 --- a/src/main/resources/application-mysql.properties +++ b/src/main/resources/application-mysql.properties @@ -5,3 +5,7 @@ spring.datasource.username=${MYSQL_USER:petclinic} spring.datasource.password=${MYSQL_PASS:petclinic} # SQL is written to be idempotent so this is safe spring.sql.init.mode=always +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver + +# spring.jpa.show-sql=true + diff --git a/src/main/resources/application-postgres.properties b/src/main/resources/application-postgres.properties index 60889b43c..ad953821d 100644 --- a/src/main/resources/application-postgres.properties +++ b/src/main/resources/application-postgres.properties @@ -2,5 +2,7 @@ database=postgres spring.datasource.url=${POSTGRES_URL:jdbc:postgresql://localhost/petclinic} spring.datasource.username=${POSTGRES_USER:petclinic} spring.datasource.password=${POSTGRES_PASS:petclinic} -# SQL is written to be idempotent so this is safe +# # SQL is written to be idempotent so this is safe spring.sql.init.mode=always +spring.datasource.driver-class-name=org.postgresql.Driver +