sbt-native-packager icon indicating copy to clipboard operation
sbt-native-packager copied to clipboard

DockerPlugin doesn't work with multiple main classes by default

Open kubukoz opened this issue 3 years ago • 0 comments

Expected behaviour

sbt Docker/publishLocal produces a runnable image or fails at build time in the presence of multiple main classes

Actual behaviour

The image fails startup

Information

  • What sbt-native-packager are you using: 1.9.7
  • What sbt version: 1.6.1
  • What is your build system: sbt
  • What package are you building docker
  • What version has your build tool: ???
  • What is your target system: macOS, also confirmed on Linux

Reproduction

  1. Make an sbt project using NP:
val root = project
  .in(file("."))
  .enablePlugins(JavaAppPackaging)
  .enablePlugins(DockerPlugin)
  1. Add multiple main classes
object Foo extends App
object Bar extends App
  1. sbt Docker/publishLocal

  2. Try to run the generated image:

$ docker run --rm -it sha256:46c53661a82306d95dbd9e9db28fa7e98d7f5ae4553d5c0091f0f71dfdbb8c97
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/opt/docker/bin/root": stat /opt/docker/bin/root: no such file or directory: unknown.

Docker/dockerEntrypoint points to /opt/docker/bin/root, and setting it to a specific binary helps. Clearly the names are set by a heuristic documented in the docs.

However, I don't think the Docker plugin's behavior is intuitive, because if you add a new main class to a working build you'll suddenly be unable to run the images.

Maybe Docker/stage should fail if there are many main classes and none of them is explicitly chosen (by Docker/mainClass or dockerEntrypoint, I'm not sure)?

kubukoz avatar Jan 17 '22 17:01 kubukoz