diff options
| author | Szymon Szukalski <szymon@skas.io> | 2024-07-25 11:56:13 +1000 |
|---|---|---|
| committer | Szymon Szukalski <szymon@skas.io> | 2024-07-25 11:56:13 +1000 |
| commit | 1df19fd955eb951f5d31fb205963b2f4e794c039 (patch) | |
| tree | 8de7fe9f20c069d4e9907a669e792d4e6c0f75ea | |
| parent | bac742fe816a349d75db28fc44e5cca22776bb1a (diff) | |
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.
| -rw-r--r-- | Dockerfile | 6 | ||||
| -rw-r--r-- | Dockerfile.test | 15 | ||||
| -rw-r--r-- | README.md | 54 | ||||
| -rw-r--r-- | docker-compose.yml | 23 | ||||
| -rw-r--r-- | pom.xml | 24 | ||||
| -rw-r--r-- | src/main/resources/application.properties | 2 | ||||
| -rw-r--r-- | src/test/resources/application-test.properties | 2 |
7 files changed, 117 insertions, 9 deletions
@@ -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 @@ -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: @@ -65,6 +65,11 @@ </dependency> <dependency> + <groupId>org.postgresql</groupId> + <artifactId>postgresql</artifactId> + </dependency> + + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-math3</artifactId> <version>3.6.1</version> @@ -73,7 +78,7 @@ <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> - <scope>runtime</scope> + <scope>test</scope> </dependency> </dependencies> @@ -83,7 +88,22 @@ <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <showWarnings>false</showWarnings> + </configuration> + </plugin> </plugins> </build> -</project> + <profiles> + <profile> + <id>test</id> + <properties> + <spring.profiles.active>test</spring.profiles.active> + </properties> + </profile> + </profiles> +</project>
\ 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 |
