Skip to content

Commit 17b35ec

Browse files
committed
fix: docker compose deployment
1 parent 34e8376 commit 17b35ec

File tree

6 files changed

+118
-31
lines changed

6 files changed

+118
-31
lines changed

comments-app-ktor/build.gradle.kts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ kotlin {
106106
}
107107
}
108108

109+
ktor {
110+
fatJar {
111+
archiveFileName.set("${project.name}.jar")
112+
}
113+
}
114+
109115

110116
tasks {
111117
val linkReleaseExecutableLinuxX64 by getting(KotlinNativeLink::class)
@@ -116,9 +122,11 @@ tasks {
116122
// val nativeFile = linkDebugExecutableLinuxX64.binary.outputFile
117123
val linuxX64ProcessResources by getting(ProcessResources::class)
118124
val linuxArm64ProcessResources by getting(ProcessResources::class)
125+
val jvmProcessResources by getting(ProcessResources::class)
119126
val dockerLinuxX64Dir = layout.buildDirectory.file("docker-x64/Dockerfile").get().asFile
120127
val dockerLinuxArm64Dir = layout.buildDirectory.file("docker-arm64/Dockerfile").get().asFile
121128
val dockerLinuxMultiplatformDir = layout.buildDirectory.file("docker-multiplatform/Dockerfile").get().asFile
129+
val dockerJvmDir = layout.buildDirectory.file("docker-jvm/Dockerfile").get().asFile
122130

123131
val dockerDockerfileX64 by creating(Dockerfile::class) {
124132
dependsOn(linkReleaseExecutableLinuxX64)
@@ -229,14 +237,57 @@ tasks {
229237
arg("TARGETPLATFORM")
230238
copyFile("\${TARGETPLATFORM}/${nativeFileArm64.name}", "/app/")
231239
copyFile("\${TARGETPLATFORM}/application.yaml", "/app/")
232-
exposePort(8081)
240+
exposePort(8080)
233241
workingDir("/app")
234242
entryPoint("/app/${nativeFileArm64.name}", "-config=./application.yaml")
235243
}
236244

245+
val dockerDockerfileJvm by creating(Dockerfile::class) {
246+
dependsOn(buildFatJar)
247+
dependsOn(shadowJar)
248+
dependsOn(jvmProcessResources)
249+
group = "docker"
250+
destFile.set(dockerJvmDir)
251+
252+
doFirst {
253+
copy {
254+
from(layout.buildDirectory.dir("libs"))
255+
from(jvmProcessResources.destinationDir)
256+
into("${this@creating.destDir.get().dir("app")}")
257+
}
258+
}
259+
260+
from(Dockerfile.From("openjdk:17-alpine"))
261+
destDir.get().dir("app").asFile.listFiles()?.forEach {
262+
copyFile("app/${it.name}", "/app/")
263+
}
264+
265+
exposePort(8080)
266+
workingDir("/app")
267+
entryPoint("java", "-jar", ktor.fatJar.archiveFileName.get())
268+
}
269+
val dockerBuildJvmImage by creating(DockerBuildImage::class) {
270+
group = "docker"
271+
dependsOn(dockerDockerfileJvm)
272+
inputDir.set(dockerJvmDir.parentFile)
273+
images.add("$imageName-jvm:${rootProject.version}")
274+
images.add("$imageName-jvm:latest")
275+
}
276+
val dockerPushJvmImage by creating(DockerPushImage::class) {
277+
group = "docker"
278+
dependsOn(dockerBuildJvmImage)
279+
images.set(dockerBuildJvmImage.images)
280+
registryCredentials {
281+
username.set(registryUser)
282+
password.set(registryPass)
283+
url.set("https://$registryHost/v1/")
284+
}
285+
}
286+
237287

238288
val deployMultiplatform by creating(Exec::class) {
239289
group = "build"
290+
dependsOn(dockerPushJvmImage)
240291
dependsOn(dockerDockerfileMultiplatform)
241292
workingDir(dockerDockerfileMultiplatform.destDir)
242293
workingDir.list()?.forEach {
@@ -247,10 +298,10 @@ tasks {
247298
args("buildx", "build", "--platform", "linux/amd64,linux/arm64", "-t", "$imageName:${rootProject.version}", "-t", "$imageName:latest","--push", ".")
248299
}
249300

250-
251301
create("deploy") {
252302
group = "build"
253303
dependsOn(dockerPushX64Image)
254304
dependsOn(dockerPushArm64Image)
305+
dependsOn(dockerPushJvmImage)
255306
}
256307
}

comments-app-ktor/src/jvmMain/kotlin/com/crowdproj/comments/app/cassandra/CassandraConfig.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ data class CassandraConfig(
77
val port: Int = 9042,
88
val user: String = "cassandra",
99
val pass: String = "cassandra",
10-
val keyspace: String = "test_keyspace"
10+
val keyspace: String = "test_keyspace",
11+
val testing: Boolean = false
1112
) {
1213
constructor(config: ApplicationConfig) : this(
1314
host = config.property("host").getString(),
1415
port = config.property("port").getString().toInt(),
1516
user = config.property("user").getString(),
1617
pass = config.property("pass").getString(),
17-
keyspace = config.property("keyspace").getString()
18+
keyspace = config.property("keyspace").getString(),
19+
testing = config.propertyOrNull("testing")?.getString()?.toBoolean() ?: CassandraConfig().testing
1820
)
1921
}

comments-app-ktor/src/jvmMain/kotlin/com/crowdproj/comments/app/plugins/GetDatabaseConf.jvm.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ actual fun Application.getDatabaseConf(type: CommentDbType): ICommentsRepository
1818
)
1919

2020
return when(dbType) {
21-
"inmemory", "memory", "mem" -> initInMemory()
21+
"inmemory", "memory", "mem" -> initInMemory(dbConfig)
2222
"cassandra", "cass" -> initCassandra(dbConfig)
2323
else -> throw IllegalArgumentException(
2424
"$dbSettingPath.type must be set in application.yml to one of: " +
@@ -28,19 +28,20 @@ actual fun Application.getDatabaseConf(type: CommentDbType): ICommentsRepository
2828
}
2929

3030

31-
private fun Application.initCassandra(dbConfig: ApplicationConfig): ICommentsRepository {
31+
private fun initCassandra(dbConfig: ApplicationConfig): ICommentsRepository {
3232
val config = CassandraConfig(dbConfig)
3333
return CommentsRepoCassandra(
3434
keyspaceName = config.keyspace,
3535
host = config.host,
3636
port = config.port,
3737
user = config.user,
3838
pass = config.pass,
39+
testing = config.testing
3940
)
4041
}
4142

42-
private fun Application.initInMemory(): ICommentsRepository {
43-
val ttlSetting = environment.config.propertyOrNull("db.prod")?.getString()?.let {
43+
private fun initInMemory(dbConfig: ApplicationConfig): ICommentsRepository {
44+
val ttlSetting = dbConfig.propertyOrNull("ttl")?.getString()?.let {
4445
Duration.parse(it)
4546
}
4647
return CommentsRepoInMemory(ttl = ttlSetting ?: 10.minutes)

comments-app-ktor/src/jvmMain/resources/application.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ comments:
1818
type: inmemory
1919
prod:
2020
type: cassandra
21-
host: localhost
22-
port: 9042
23-
user: cassandra
24-
pass: cassandra
25-
keyspace: test_keyspace
21+
host: "$CASSANDRA_HOST:localhost"
22+
port: "$CASSANDRA_PORT:9042"
23+
user: "$CASSANDRA_USER:cassandra"
24+
pass: "$CASSANDRA_PASS:cassandra"
25+
keyspace: "$CASSANDRA_KEYSPACE:comments"
26+
testing: "$CASSANDRA_TESTING:false"

deploy/docker-compose.yml

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,58 @@
66
version: "3"
77
services:
88

9-
# app:
10-
# container_name: app
11-
# image: nginx
12-
# ports:
13-
# - 8080:80
9+
app:
10+
container_name: app
11+
image: stellalupus/crowdproj-comments-jvm
12+
ports:
13+
- 8080:8080
1414
# volumes:
1515
# # добавляем конфигурацию в контейнер
1616
# - ./nginx/nginx.conf:/etc/nginx/nginx.conf
17-
# depends_on:
18-
# - fluent-bit
19-
# logging:
20-
# # используемый драйвер логгирования
21-
# driver: "fluentd"
22-
# options:
23-
# # куда посылать лог-сообщения
24-
# # необходимо чтобы адрес совпадал с настройками плагина forward
25-
# fluentd-address: localhost:24224
26-
# # теги используются для маршрутизации лог-сообщений
27-
# tag: app.logs
28-
# networks:
29-
# - opensearch
17+
environment:
18+
CASSANDRA_HOST: cassandra
19+
CASSANDRA_TESTING: true
20+
depends_on:
21+
- fluent-bit
22+
- cassandra
23+
logging:
24+
# используемый драйвер логгирования
25+
driver: "fluentd"
26+
options:
27+
# куда посылать лог-сообщения
28+
# необходимо чтобы адрес совпадал с настройками плагина forward
29+
fluentd-address: localhost:24224
30+
# теги используются для маршрутизации лог-сообщений
31+
tag: app.logs
32+
networks:
33+
- opensearch
34+
35+
cassandra:
36+
image: cassandra:3.11.16 # better to use a specific version, if you want to control upgrades
37+
container_name: cassandra
38+
39+
healthcheck:
40+
test: [ "CMD", "cqlsh", "-e", "describe keyspaces" ]
41+
interval: 5s
42+
timeout: 5s
43+
retries: 60
44+
volumes:
45+
- ./volumes/cassandra:/var/lib/cassandra
46+
environment: # Declare and save environments variables into "environment"
47+
CASSANDRA_SNITCH: GossipingPropertyFileSnitch
48+
JVM_OPTS: -Dcassandra.skip_wait_for_gossip_to_settle=0 -Dcassandra.initial_token=0
49+
HEAP_NEWSIZE: 128M
50+
MAX_HEAP_SIZE: 1024M
51+
CASSANDRA_ENDPOINT_SNITCH: GossipingPropertyFileSnitch
52+
CASSANDRA_DC: datacenter1
53+
networks:
54+
opensearch:
55+
aliases:
56+
- cassandra
57+
deploy:
58+
resources:
59+
limits:
60+
memory: 2G # It's not strictly required, but it's better to have some memory limit
3061

3162
fluent-bit:
3263
container_name: fluent-bit

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ ktor-server-call-logging = { module = "io.ktor:ktor-server-call-logging", versio
4444
ktor-server-default-headers = { module = "io.ktor:ktor-server-default-headers", version.ref = "ktor" }
4545
ktor-server-call-id = { module = "io.ktor:ktor-server-call-id", version.ref = "ktor" }
4646
ktor-server-swagger = { module = "io.ktor:ktor-server-swagger", version.ref = "ktor" }
47+
ktor-server-forwarded-header = { module = "io.ktor:ktor-server-forwarded-header", version.ref = "ktor" }
4748
#KTOR Serialization
4849
ktor-serialization-jackson = { module = "io.ktor:ktor-serialization-jackson", version.ref = "ktor" }
4950
#KTOR Client

0 commit comments

Comments
 (0)