From 852031eb7f9db6ef997c6f8c55fc77193de01ac4 Mon Sep 17 00:00:00 2001 From: Joosa Kurvinen Date: Wed, 15 Nov 2023 14:59:30 +0200 Subject: [PATCH] dockerize service --- .gitignore | 3 ++ api-gateway/src/config.ts | 2 +- compose/docker-compose.e2e.yml | 21 +++++++++++++ service/Dockerfile | 56 ++++++++++++++++++++++++++++++++++ service/build.gradle.kts | 30 ++++++++++++++++-- service/entrypoint.sh | 10 ++++++ service/gradle.properties | 2 ++ service/settings.gradle.kts | 15 ++++++++- 8 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 compose/docker-compose.e2e.yml create mode 100644 service/Dockerfile create mode 100755 service/entrypoint.sh create mode 100644 service/gradle.properties diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d484c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +*.iml +.DS_Store diff --git a/api-gateway/src/config.ts b/api-gateway/src/config.ts index ba45e28..1c7c910 100644 --- a/api-gateway/src/config.ts +++ b/api-gateway/src/config.ts @@ -1,3 +1,3 @@ export const httpPort = 3000 export const csrfCookieName = 'XSRF-TOKEN' -export const serviceUrl = 'http://localhost:8080' +export const serviceUrl = process.env.SERVICE_URL || 'http://localhost:8080' diff --git a/compose/docker-compose.e2e.yml b/compose/docker-compose.e2e.yml new file mode 100644 index 0000000..4cce316 --- /dev/null +++ b/compose/docker-compose.e2e.yml @@ -0,0 +1,21 @@ +version: '3.5' + +services: + service: + build: + context: ../service/ + ports: + - "8080:8080" + environment: + JAVA_OPTS: -server -Djava.security.egd=file:/dev/./urandom -Xms1024m -Xss512k -Xmx1024m -XX:TieredStopAtLevel=1 + SPRING_DATASOURCE_URL: jdbc:postgresql://oppivelvollisuus-db:5432/oppivelvollisuus + SPRING_DATASOURCE_USERNAME: oppivelvollisuus + SPRING_DATASOURCE_PASSWORD: postgres + + api-gateway: + build: + context: ../api-gateway/ + ports: + - "3000:3000" + environment: + SERVICE_URL: "http://service:8080" diff --git a/service/Dockerfile b/service/Dockerfile new file mode 100644 index 0000000..b694e1b --- /dev/null +++ b/service/Dockerfile @@ -0,0 +1,56 @@ +FROM eclipse-temurin:17-jammy as base + +LABEL maintainer="https://github.com/espoon-voltti/oppivelvollisuus" + +ENV LC_ALL C.UTF-8 +ENV LANG C.UTF-8 +ENV LANGUAGE C.UTF-8 +RUN apt-get update \ + && apt-get -y dist-upgrade \ + && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + tzdata \ + ca-certificates \ + curl \ + unzip \ + && ln -fs /usr/share/zoneinfo/Europe/Helsinki /etc/localtime \ + && dpkg-reconfigure --frontend noninteractive tzdata \ + && rm -rf /var/lib/apt/lists/* + +FROM base as builder + +WORKDIR /app + +COPY ./gradle/ ./gradle/ +COPY ./gradlew ./build.gradle.kts ./gradle.properties ./settings.gradle.kts ./ + +RUN ./gradlew --no-daemon resolveDependencies + +COPY . . + +# --offline is used to be sure that all dependencies are installed in previous steps +RUN ./gradlew --offline --no-daemon assemble \ + && unzip -oq build/libs/oppivelvollisuus-service-boot.jar -d target + +FROM base + +WORKDIR /app + +COPY ./entrypoint.sh entrypoint.sh +ENTRYPOINT ["./entrypoint.sh"] + +ENV USERNAME oppivelvollisuus +ENV HOME_DIR /home/${USERNAME} +ENV USER_ID 1000 + +RUN adduser ${USERNAME} --gecos "" -q --home ${HOME_DIR} --uid ${USER_ID} --disabled-password + +COPY --from=builder /app/target/ . + +USER ${USERNAME} + +ARG build=none +ARG commit=none +ENV APP_BUILD "$build" +ENV APP_COMMIT "$commit" +LABEL fi.espoo.build="$build" \ + fi.espoo.commit="$commit" diff --git a/service/build.gradle.kts b/service/build.gradle.kts index b1c0920..a82ccb8 100644 --- a/service/build.gradle.kts +++ b/service/build.gradle.kts @@ -1,4 +1,5 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.springframework.boot.gradle.tasks.bundling.BootJar plugins { id("org.springframework.boot") version "3.1.5" @@ -9,9 +10,6 @@ plugins { id("org.jlleitschuh.gradle.ktlint") version "11.6.1" } -group = "fi.espoo" -version = "0.0.1-SNAPSHOT" - java { sourceCompatibility = JavaVersion.VERSION_17 } @@ -53,6 +51,32 @@ tasks.withType { useJUnitPlatform() } +tasks.getByName("jar") { + archiveClassifier.set("") +} + +tasks.getByName("bootJar") { + archiveClassifier.set("boot") +} + +tasks.register("resolveDependencies") { + description = "Resolves all dependencies" + doLast { + configurations + .matching { it.isCanBeResolved } + .map { + val files = it.resolve() + it.name to files.size + } + .groupBy({ (_, count) -> count }) { (name, _) -> name } + .forEach { (count, names) -> + println( + "Resolved $count dependency files for configurations: ${names.joinToString(", ")}" + ) + } + } +} + flyway { url = "jdbc:postgresql://localhost:5432/oppivelvollisuus" user = "oppivelvollisuus" diff --git a/service/entrypoint.sh b/service/entrypoint.sh new file mode 100755 index 0000000..a2a3601 --- /dev/null +++ b/service/entrypoint.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# For log tagging (with a default value and error logging without crashing) +# shellcheck disable=SC2155 +export HOST_IP=$(curl --silent --fail --show-error http://169.254.169.254/latest/meta-data/local-ipv4 || printf 'UNAVAILABLE') + +# shellcheck disable=SC2086 +exec java -cp . -server $JAVA_OPTS org.springframework.boot.loader.JarLauncher "$@" diff --git a/service/gradle.properties b/service/gradle.properties new file mode 100644 index 0000000..33242c1 --- /dev/null +++ b/service/gradle.properties @@ -0,0 +1,2 @@ +org.gradle.jvmargs=-Xmx3g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 +org.gradle.vfs.watch=true diff --git a/service/settings.gradle.kts b/service/settings.gradle.kts index ca60b4c..a888962 100644 --- a/service/settings.gradle.kts +++ b/service/settings.gradle.kts @@ -1 +1,14 @@ -rootProject.name = "oppivelvollisuus" +rootProject.name = "oppivelvollisuus-service" + +dependencyResolutionManagement { + repositories { + mavenCentral() + maven("https://build.shibboleth.net/maven/releases") { + content { + includeGroup("net.shibboleth") + includeGroup("net.shibboleth.utilities") + includeGroup("org.opensaml") + } + } + } +}