Skip to content

Commit c871954

Browse files
jropermuuki88
andauthored
Added dockerBuildInit option (#1438)
* Added dockerBuildInit option See #1257. * Move dockerLayerMappings to DockerKeysEx * Scalafmt Co-authored-by: Nepomuk Seiler <muuki88@users.noreply.github.com>
1 parent b7e3904 commit c871954

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ object DockerPlugin extends AutoPlugin {
152152
).toOption
153153
.map(_.trim)
154154
.flatMap(DockerApiVersion.parse),
155+
dockerBuildInit := false,
155156
dockerBuildOptions := Seq("--force-rm") ++ dockerAliases.value.flatMap { alias =>
156157
Seq("-t", alias.toString)
157-
},
158+
} ++ { if (dockerBuildInit.value) List("--init") else Nil },
158159
dockerRmiCommand := dockerExecCommand.value ++ Seq("rmi"),
159160
dockerBuildCommand := dockerExecCommand.value ++ Seq("build") ++ dockerBuildOptions.value ++ Seq("."),
160161
dockerAdditionalPermissions := {

src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,10 @@ private[packager] trait DockerKeysEx extends DockerKeys {
6868
)
6969
val dockerLayerMappings =
7070
taskKey[Seq[LayeredMapping]]("List of layer, source file and destination in Docker image.")
71+
val dockerBuildInit = SettingKey[Boolean](
72+
"dockerBuildInit",
73+
"Whether the --init flag should be passed to Docker when building. " +
74+
"Setting to true will cause Docker to bundle a tini in the container, to run as the init process, which is recommended for JVM apps. " +
75+
"Requires Docker API version 1.25+"
76+
)
7177
}

src/sphinx/formats/docker.rst

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,13 @@ Publishing Settings
177177
To append additional aliases to this list, you can add them by extending ``dockerAlias``.
178178
``dockerAliases ++= Seq(dockerAlias.value.withTag(Option("stable")), dockerAlias.value.withRegistryHost(Option("registry.internal.yourdomain.com")))``
179179

180+
``dockerBuildInit``
181+
Whether the ``--init`` build option should be passed to the Docker build. See :ref:`Init support` for when this may be useful.
182+
Defaults to ``false``.
183+
180184
``dockerBuildOptions``
181185
Overrides the default Docker build options.
182-
Defaults to ``Seq("--force-rm", "-t", "[dockerAlias]")``. This default is expanded if ``dockerUpdateLatest`` is set to true.
186+
Defaults to ``Seq("--force-rm", "-t", "[dockerAlias]")``. This default is expanded if either ``dockerUpdateLatest`` or ``dockerBuildInit`` is set to true.
183187

184188
``dockerExecCommand``
185189
Overrides the default Docker exec command.
@@ -423,3 +427,23 @@ Just like for :ref:`java-app-plugin`, you have the option of overriding the defa
423427
your own ``src/templates/ash-template`` file. When overriding the file don't forget to include
424428
``${{template_declares}}`` somewhere to populate ``$app_classpath $app_mainclass`` from your sbt project.
425429
You'll likely need these to launch your program.
430+
431+
Init support
432+
~~~~~~~~~~~~
433+
434+
By default, Java will run with PID 1 when you run your docker container. The JVM behaves differently when its PID is 1
435+
compared to other PIDs, most notably, it doesn't respond to some signals. These include the signals usually used to
436+
instruct a Java process to dump its threads or its heap. If you want to be able to debug a running Java container, the
437+
inability to take thread or heap dumps can be a problem.
438+
439+
Docker has a convenient solution to this, it can configure a separate init process for you. This process will start
440+
your Java process, and it will also do some other useful things that init processes are meant to do like cleaning up
441+
orphaned processes in the container. But most importantly it will ensure that your Java process is not PID 1, which
442+
will in turn ensure that your Java process is able to respond to signals for debugging. The command docker uses is
443+
`tini <https://github.com/krallin/tini>`_, which as its name suggests, is tiny, only 23kb in size.
444+
445+
To tell docker to configure a separate init process using tini, set the `dockerBuildInit` setting to `true`:
446+
447+
.. code-block:: scala
448+
449+
dockerBuildInit := true

0 commit comments

Comments
 (0)