From 1df19fd955eb951f5d31fb205963b2f4e794c039 Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Thu, 25 Jul 2024 11:56:13 +1000 Subject: Separate Docker Compose services for build, test, run Refactor Docker Compose setup to include distinct services for building, testing, and running the application. Use separate Dockerfiles and configurations for each phase to isolate concerns and improve workflow. Quiet down the maven logs and set log levels to WARN so there is less noise in the logs. Update README file with information on running the project. --- Dockerfile | 6 +-- Dockerfile.test | 15 +++++++ README.md | 54 +++++++++++++++++++++++++- docker-compose.yml | 23 +++++++++-- pom.xml | 24 +++++++++++- src/main/resources/application.properties | 2 + src/test/resources/application-test.properties | 2 + 7 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 Dockerfile.test diff --git a/Dockerfile b/Dockerfile index 63481f7..3381117 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,13 @@ -# Stage 1: Build the JAR file +# Stage 1: BUILD FROM maven:3.8.5-openjdk-17 AS build WORKDIR /app COPY mvnw pom.xml ./ COPY .mvn .mvn COPY src src RUN chmod +x mvnw -RUN ./mvnw clean package -Dmaven.test.skip=true +RUN ./mvnw clean package -q -Dmaven.test.skip=true -# Stage 2: Run the JAR file +# Stage 2: RUN FROM openjdk:17-alpine RUN addgroup -S spring && adduser -S spring -G spring USER spring:spring diff --git a/Dockerfile.test b/Dockerfile.test new file mode 100644 index 0000000..9a82cc5 --- /dev/null +++ b/Dockerfile.test @@ -0,0 +1,15 @@ +# Stage 1: BUILD +FROM maven:3.8.5-openjdk-17 AS build +WORKDIR /app +COPY mvnw pom.xml ./ +COPY .mvn .mvn +COPY src src +RUN chmod +x mvnw +RUN ./mvnw clean package -q -Dmaven.test.skip=true + +# Stage 2: TEST +FROM maven:3.8.5-openjdk-17 AS test +WORKDIR /app +COPY --from=build /app /app +RUN chmod +x mvnw +CMD ["./mvnw", "test", "-P", "test", "-q"] \ No newline at end of file diff --git a/README.md b/README.md index ade89f4..7e170e4 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,55 @@ # Markr - Marking as a Service -**Szymon Szukalski's Submission to the Stile Coding Challenge - July 2024** \ No newline at end of file +**Szymon Szukalski's Submission to the Stile Coding Challenge - July 2024** + +## Running the Project + +This project uses Docker for building, testing, and running the application. Below are the instructions to incorporate +this project into your build system using Docker. + +### Prerequisites + +- Docker (version 20.10 or later) +- Docker Compose (version 1.29 or later) + +### Build + +To build the Docker image for the application, run the following command: + +```shell +docker compose build +``` + +This command will build the application image using the Dockerfile defined in the project. + +### Test + +To run the tests using Docker, use the following command: + +```shell +docker compose run --rm markr_tests +``` + +This command will build the Docker image (if not already built), start a container for testing, and run the tests. The +--rm flag ensures that the test container is removed after the tests complete. + +### Run + +To run the application, use the following command: + +```shell +docker compose up postgres markr +``` + +This command will start the application and its dependencies (like PostgreSQL) as defined in the docker-compose.yml +file. + +### Cleanup + +To stop the running containers and remove them along with associated volumes, use: + +```shell +docker compose down -v +``` + +This command will stop and remove the containers and any associated volumes. \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 0f45dc0..cb7bc7e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,9 +11,11 @@ services: volumes: - postgres_data:/var/lib/postgresql/data - markr_service: - build: . - container_name: markr_service + markr: + build: + context: . + dockerfile: Dockerfile + container_name: skasio_markr ports: - "8080:8080" depends_on: @@ -23,5 +25,20 @@ services: SPRING_DATASOURCE_USERNAME: markr_prod SPRING_DATASOURCE_PASSWORD: CpfzDA3nR3jH9Ky4 + markr_tests: + build: + context: . + dockerfile: Dockerfile.test + container_name: skasio_markr_tests + environment: + SPRING_DATASOURCE_URL: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE + SPRING_DATASOURCE_USERNAME: markr_test + SPRING_DATASOURCE_PASSWORD: DekqFAQnRIjH2Kz1 + SPRING_JPA_DATABASE_PLATFORM: org.hibernate.dialect.H2Dialect + SPRING_JPA_HIBERNATE_DDL_AUTO: update + SPRING_JPA_SHOW_SQL: false + SPRING_PROFILES_ACTIVE: test + command: ./mvnw test -P test -q + volumes: postgres_data: diff --git a/pom.xml b/pom.xml index 55b3297..0422efd 100644 --- a/pom.xml +++ b/pom.xml @@ -64,6 +64,11 @@ spring-boot-starter-data-jpa + + org.postgresql + postgresql + + org.apache.commons commons-math3 @@ -73,7 +78,7 @@ com.h2database h2 - runtime + test @@ -83,7 +88,22 @@ org.springframework.boot spring-boot-maven-plugin + + org.apache.maven.plugins + maven-compiler-plugin + + false + + - + + + test + + test + + + + \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3600371..e838ed8 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,6 @@ spring.application.name=markr +logging.level.root=warn +logging.level.org.springframework.web=warn # PostgreSQL Configuration spring.datasource.url=jdbc:postgresql://localhost:5432/markr spring.datasource.username=markr_prod diff --git a/src/test/resources/application-test.properties b/src/test/resources/application-test.properties index 87666a1..3c690d8 100644 --- a/src/test/resources/application-test.properties +++ b/src/test/resources/application-test.properties @@ -1,3 +1,5 @@ +logging.level.root=warn +logging.level.org.springframework.web=info # H2 Configuration spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.driver-class-name=org.h2.Driver -- cgit v1.2.3