Docker Scala應用程序示例

Docker允許我們執行Scala應用程序。 在這裏,我們將演示如何創建一個Scala文件,並使用docker進行執行,此示例包括以下步驟。

  1. 創建一個目錄用來組織應用程序文件。
$ mkdir - /home/yiibai/docker/scala-docker-app
  1. 更改進入工作目錄
$ cd /home/yiibai/docker/scala-docker-app
  1. 創建一個名稱爲 Dockerfile 的 Dockerfile 文件
FROM williamyeh/java7  
MAINTAINER Hema ([email protected])
ENV SCALA_VERSION 2.10.4  
ENV SCALA_TARBALL http://www.scala-lang.org/files/archive/scala-$SCALA_VERSION.deb  
RUN \  
echo "==> Install curl helper tool..."&& \  
apt-get update && \  
DEBIAN_FRONTEND=noninteractive apt-get install -y --force-yes curl && \  
\  
\  
\  
echo "===> install from Typesafe repo (contains old versions but they have all dependencies we need later on)"&& \  
curl -sSL http://apt.typesafe.com/repo-deb-build-0002.deb -o repo-deb.deb && \  
dpkg -i repo-deb.deb && \  
apt-get update && \  
\  
\  
\  

echo "===> install Scala"&& \  
DEBIAN_FRONTEND=noninteractive \  
apt-get install -y --force-yes libjansi-java && \  
curl -sSL $SCALA_TARBALL -o scala.deb && \  
dpkg -i scala.deb && \  
\  
\  
\  
echo "===> clean up..."&& \  
rm -f *.deb && \  
apt-get remove -y --auto-remove curl && \  
apt-get clean && \  
rm -rf /var/lib/apt/lists/*  
COPY . /home/yiibai/src
WORKDIR /home/yiibai/src
CMD ["scala", "/home/yiibai/src/index.scala"]
  1. 創建Scala文件: /home/yiibai/docker/scala-docker-app/index.scala
object MainObject{  
    def main(args:Array[String]){  
        println("Hello by Scala");  
    }  
}

現在,創建這個Scala應用程序的Docker映像。 以下命令用於創建Docker映像。

yiibai@ubuntu:~/docker/scala-docker-app$ sudo docker build -t scala-app .

執行上面代碼,輸出結果如下 -

yiibai@ubuntu:~/docker/scala-docker-app$ sudo docker build -t scala-app .
Sending build context to Docker daemon  3.584kB
.......
Removing intermediate container a551438ab33a
......
 ---> Running in bc8b748316cc
 ---> 1d786cf20101
Removing intermediate container bc8b748316cc
Successfully built 1d786cf20101
Successfully tagged scala-app:latest
yiibai@ubuntu:~/docker/scala-docker-app$
  1. 運行Scala Docker映像
yiibai@ubuntu:~/docker/scala-docker-app$ sudo docker run scala-app

執行上面代碼,得到以下結果 -

yiibai@ubuntu:~/docker/scala-docker-app$ sudo docker run scala-app
Hello by Scala
yiibai@ubuntu:~/docker/scala-docker-app$ sudo docker run scala-app