Skip to content

Commit

Permalink
Add 'jvmOptions' to docker contrib configuration (#3079)
Browse files Browse the repository at this point in the history
Pull request: #3079
  • Loading branch information
megri authored Mar 11, 2024
1 parent ebd95e1 commit b233801
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions contrib/docker/readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ object docker extends DockerConfig {
def volumes = Seq("/v1", "/v2")
// Environment variables to be set in the container (ENV instructions)
def envVars = Map("foo" -> "bar", "foobar" -> "barfoo")
// JVM runtime options such as heap size settings
def jvmOptions = Seq("-Xmx1024M", "-XX:+HeapDumpOnOutOfMemoryError")
// Add RUN instructions
def run = Seq(
"/bin/bash -c 'echo Hello World!'",
Expand Down
15 changes: 14 additions & 1 deletion contrib/docker/src/mill/contrib/docker/DockerModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ trait DockerModule { outer: JavaModule =>
def baseImage: T[String] = "gcr.io/distroless/java:latest"
def pullBaseImage: T[Boolean] = T(baseImage().endsWith(":latest"))

/**
* JVM runtime options. Each item of the Seq should consist of an option and its desired value, like
* {{{
* def jvmOptions = Seq("-Xmx1024M", "-agentlib:jdwp=transport=dt_socket,server=y,address=8000", …)
* }}}
* For a full list of options consult the official documentation at
* [[https://docs.oracle.com/en/java/javase/21/docs/specs/man/java.html#overview-of-java-options]]
*/
def jvmOptions: T[Seq[String]] = Seq.empty[String]

/**
* TCP Ports the container will listen to at runtime.
*
Expand Down Expand Up @@ -113,11 +123,14 @@ trait DockerModule { outer: JavaModule =>
if (user().isEmpty) "" else s"USER ${user()}"
).filter(_.nonEmpty).mkString(sys.props("line.separator"))

val quotedEntryPointArgs = (Seq("java") ++ jvmOptions() ++ Seq("-jar", s"/$jarName"))
.map(arg => s"\"$arg\"").mkString(", ")

s"""
|FROM ${baseImage()}
|$lines
|COPY $jarName /$jarName
|ENTRYPOINT ["java", "-jar", "/$jarName"]""".stripMargin
|ENTRYPOINT [$quotedEntryPointArgs]""".stripMargin
}

final def build = T {
Expand Down
22 changes: 22 additions & 0 deletions contrib/docker/test/src/mill/contrib/docker/DockerModuleTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ object DockerModuleTest extends TestSuite {
override def user = "user1"
override def executable = testExecutable
}

object dockerJvmOptions extends DockerConfig {
override def executable = testExecutable
override def jvmOptions = Seq("-Xmx1024M")
}
}

val testArtifactName = "mill-docker-contrib-test"
Expand Down Expand Up @@ -132,6 +137,23 @@ object DockerModuleTest extends TestSuite {
)
assert(dockerfileStringRefined == expected)
}

"extra jvm options" - {
val eval = new TestEvaluator(Docker)
val Right((dockerfileString, _)) = eval(Docker.dockerJvmOptions.dockerfile)
val expected = multineRegex.replaceAllIn(
"""
|FROM gcr.io/distroless/java:latest
|COPY out.jar /out.jar
|ENTRYPOINT ["java", "-Xmx1024M", "-jar", "/out.jar"]""".stripMargin,
sys.props("line.separator")
)
val dockerfileStringRefined = multineRegex.replaceAllIn(
dockerfileString,
sys.props("line.separator")
)
assert(dockerfileStringRefined == expected)
}
}
}
}

0 comments on commit b233801

Please sign in to comment.