Search code examples
scaladockersbtsbt-native-packager

Scala, docker - how to set mainClass in multimodule application using sbt-native-packager?


I have created a multimodule, sbt project in Scala. For now it is:

  • main project (main-service) with build.sbt file

  • http module with Main class

In my sbt file I have:

lazy val root = (project in file("."))
  .aggregate(http)
  .settings(
    dockerBaseImage := "openjdk:jre-alpine",
    name := "main-service",
    libraryDependencies ++= Seq(

    )
  )
  .enablePlugins(JavaAppPackaging)
  .enablePlugins(DockerPlugin)
  .enablePlugins(AshScriptPlugin)

lazy val http = (project in file("http"))
  .settings(
    mainClass in Compile := Some("Main"),
    name := "main-http",
    libraryDependencies ++= Seq(    
    ))

As you can see, I want to run it with docker. Image of this project is created well, but when I made docker run then I got an error:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"/opt/docker/bin/main-service\": stat /opt/docker/bin/main-service: no such file or directory": unknown.

I think the problem could be with mainClass line. I have my Main class in main-http/src/main/scala directory, but it looks like docker does not see it.

How should I move this Main class or change path to it and run it correctly?


Solution

  • If you want to keep the main class in http subproject, you need to move the plugins to this project too as following.

    lazy val root = (project in file("."))
      .aggregate(http)
      .settings(name := "main-service")
    
    lazy val http = (project in file("http"))
      .settings(
        mainClass in Compile := Some("Main"),
        dockerBaseImage := "openjdk:jre-alpine",
        name := "main-http",
        libraryDependencies ++= Seq()
      )
      .enablePlugins(JavaAppPackaging)
      .enablePlugins(DockerPlugin)
      .enablePlugins(AshScriptPlugin)
    
    

    The plugins must be enabled at the project where the Main class is located.

    To build a docker image, do

    sbt http/docker:publishLocal