Skip to content

Commit

Permalink
dockerize service
Browse files Browse the repository at this point in the history
  • Loading branch information
Joosakur committed Nov 15, 2023
1 parent 9b7657a commit 852031e
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
*.iml
.DS_Store
2 changes: 1 addition & 1 deletion api-gateway/src/config.ts
Original file line number Diff line number Diff line change
@@ -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'
21 changes: 21 additions & 0 deletions compose/docker-compose.e2e.yml
Original file line number Diff line number Diff line change
@@ -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"
56 changes: 56 additions & 0 deletions service/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"
30 changes: 27 additions & 3 deletions service/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}
Expand Down Expand Up @@ -53,6 +51,32 @@ tasks.withType<Test> {
useJUnitPlatform()
}

tasks.getByName<Jar>("jar") {
archiveClassifier.set("")
}

tasks.getByName<BootJar>("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"
Expand Down
10 changes: 10 additions & 0 deletions service/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
2 changes: 2 additions & 0 deletions service/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.gradle.jvmargs=-Xmx3g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.vfs.watch=true
15 changes: 14 additions & 1 deletion service/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
}

0 comments on commit 852031e

Please sign in to comment.