fricadelle
fricadelle

Reputation: 477

Dockerize a scala app: Could not find or load main class

I am trying to understand how to compile and run a scala app with Docker and I followed this example : https://github.com/codefresh-contrib/scala-hello-world-app

my project looks like:

├── Dockerfile
├── README.md
├── build.sbt
├── project
│   ├── build.properties
│   └── plugins.sbt
└── src
    └── main
        └── scala
            └── HelloWorld.scala

HelloWorld is simple:

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, world!")
  }
}

build.properties reads:

sbt.version=1.3.8

build.sbt:

name := "scala-hello-world-sample-app"

version := "1.0"

scalaVersion := "2.12.2"

and finally my Dockerfile is:

FROM openjdk:8-jre-alpine3.9 

WORKDIR /HelloWorld

COPY . /HelloWorld

CMD ["java", "-cp", "target/scala-2.12/*.jar:scala-library-2.12.2.jar", "HelloWorld"]

Yet I cannot seem to make it work, I execute the two next commands where the Dockerfile is:

docker build -t my_image .

but docker run my_image yields:

Error: Could not find or load main class HelloWorld

Any idea what I could be doing wrong?

Thanks!

PS: the output of docker build -t my_image . is:

+] Building 2.7s (8/8) FINISHED
 => [internal] load .dockerignore                                                                                                                                                 0.0s
 => => transferring context: 2B                                                                                                                                                   0.0s
 => [internal] load build definition from Dockerfile                                                                                                                              0.0s
 => => transferring dockerfile: 37B                                                                                                                                               0.0s
 => [internal] load metadata for docker.io/library/openjdk:8-jre-alpine3.9                                                                                                        2.6s
 => [1/3] FROM docker.io/library/openjdk:8-jre-alpine3.9@sha256:f362b165b870ef129cbe730f29065ff37399c0aa8bcab3e44b51c302938c9193                                                  0.0s
 => [internal] load build context                                                                                                                                                 0.1s
 => => transferring context: 7.36kB                                                                                                                                               0.1s
 => CACHED [2/3] WORKDIR /HelloWorld                                                                                                                                              0.0s
 => [3/3] COPY . /HelloWorld                                                                                                                                                      0.0s
 => exporting to image                                                                                                                                                            0.0s
 => => exporting layers                                                                                                                                                           0.0s
 => => writing image sha256:f9b47f877daa755c569a34bac4b24d6f08b55746a5df24d605fe9878ab124822                                                                                      0.0s
 => => naming to docker.io/library/my_image

Upvotes: 1

Views: 803

Answers (1)

John Joker
John Joker

Reputation: 82

Add this in plugins.sbt

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1")

run

sbt clean compile dist

then you will found a zipFile in ./target/universal create Dockerfile like this

FROM openjdk:8
COPY ./ /opt
WORKDIR /opt
ENTRYPOINT ["./bin/your-app"]

there you go

Upvotes: 1

Related Questions