java.lang.RuntimeException:未检测到主类

时间:2017-05-22 20:36:40

标签: scala docker sbt

我正在尝试使用docker运行Scala应用程序。我已经创建了一个具有以下结构的示例项目。

build.sbt

name := "test"
version := "1.0"
scalaVersion := "2.12.2"

项目/ build.properties

sbt.version = 0.13.15

的src /主/阶/ HelloWorld.scala

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

现在,如果我做sbt run,一切正常,我有一个可爱的问候世界。

$ sbt run
...
[info] Set current project to test (in build file:/Users/yuchen/Documents/test/)
[info] Running HelloWorld 
Hello, world!
[success] Total time: 1 s, completed 22-May-2017 4:30:28 PM

我找了一个 Dockerfile

FROM  openjdk:8

ENV SCALA_VERSION 2.12.2
ENV SBT_VERSION 0.13.15

RUN touch /usr/lib/jvm/java-8-openjdk-amd64/release

RUN \
  curl -fsL http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C /root/ && \
  echo >> /root/.bashrc && \
  echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc

RUN \
  curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
  dpkg -i sbt-$SBT_VERSION.deb && \
  rm sbt-$SBT_VERSION.deb && \
  apt-get update && \
  apt-get install sbt && \
  sbt sbtVersion

WORKDIR /root

CMD sbt run

它们都是从https://hub.docker.com/r/hseeberger/scala-sbt/~/dockerfile/复制的,并附加了最后一行CMD sbt run

我尝试运行docker命令:

docker build -t test . 
docker tag test test/test:1.0
docker run test/test:1.0

但是,我一直收到这个错误:

[info] Set current project to root (in build file:/root/)
[info] Updating {file:/root/}root...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
java.lang.RuntimeException: No main class detected.
        at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) No main class detected.
[error] Total time: 0 s, completed May 23, 2017 4:00:19 AM

这是什么意思,我该如何解决?

仅供参考:我知道http://www.scala-sbt.org/sbt-native-packager/formats/docker.html,但只是想尝试编写Dockerfile,因为我正在学习这个。

1 个答案:

答案 0 :(得分:1)

我真的不知道sbt。但是,您似乎没有将项目复制到图像中(COPY中没有ADDDockerfile)。

尝试做类似的事情:

FROM  openjdk:8

ENV SCALA_VERSION 2.12.2
ENV SBT_VERSION 0.13.15

RUN touch /usr/lib/jvm/java-8-openjdk-amd64/release

RUN \
  curl -fsL http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C /root/ && \
  echo >> /root/.bashrc && \
  echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc

RUN \
  curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \
  dpkg -i sbt-$SBT_VERSION.deb && \
  rm sbt-$SBT_VERSION.deb && \
  apt-get update && \
  apt-get install sbt && \
  sbt sbtVersion

WORKDIR /myapp

CMD sbt run

COPY . /myapp

此处的添加只是COPY,可以将所有代码复制到/myapp,也可以将工作目录更改为此文件夹。这会将构建上下文复制到/myapp,然后WORKDIR应该从同一文件夹运行sbt run