Skip to content

Commit

Permalink
Update filename
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
Vitor Vieira authored and vitorsvieira committed May 13, 2018
0 parents commit c8d3c21
Show file tree
Hide file tree
Showing 41 changed files with 1,473 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Vitor S. Vieira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
133 changes: 133 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# scala-graalvm-docker

This project provides a collection of docker images to facilitate the usage of GraalVM by Scala projects, guaranteeing a consistent and reproducible environment where native images can be produced in an automated fashion.

> "Make development more productive and run programs faster anywhere." - GraalVM

## Goal

Accelerate GraalVM's adoption during develop, test, and release phases of Scala projects.


## Available

Scala: 2.11.8 and 2.12.6

SBT: From 0.13.15 until 1.1.5

GraalVM: 1.0.0-RC1

List of tags: [scalaops/scala-graalvm:{tags}](https://hub.docker.com/r/scalaops/scala-graalvm/tags/)


## How to use


Automating the development, testing, and release process will vary based on the needs of a project, but the following example could fit all cases. This repo contains a `docker-compose-example.yml` as an example of how to use an image.


```docker-compose
version: '3'
services:
app:
image: scalaops/scala-graalvm:scala-2.12.6-sbt-1.1.5-graalvm-1.0.0-rc1
command: ./generate-native-image-example.sh
volumes:
- .:/app
- ~/.ivy2:/root/.ivy2
- ~/.sbt:/root/.sbt
- ~/.coursier:/root/.coursier
```


By opening the console and running `docker-compose run app`; the docker-compose file must be in the project's root level, docker-compose will pull the version `scala-2.12.6-sbt-1.1.5-graalvm-1.0.0-rc1` and we execute the `./generate-native-image-example.sh` when the container boots up. Also, notice that the we share the current directory and known artifact folders with the container to avoid having the container's SBT loading all artificats everytime.


The `generate-native-image-example.sh`:

```shell
# Set distribution folder from first argument, otherwise default to `native`
NATIVE_IMAGE_FOLDER=native-image
if [ $# -gt 0 ]; then
NATIVE_IMAGE_FOLDER=$1
fi

# Create native-image folder if it doesn't exist, and clear the contents
mkdir -p ${NATIVE_IMAGE_FOLDER}
rm -rf ${NATIVE_IMAGE_FOLDER}/*

# Create an .jar using sbt-assembly
sbt -mem 2048 clean assembly

# Move .jar to native-image folder
find "." -name '*.jar' | while read APP_JAR; do
mv $APP_JAR $NATIVE_IMAGE_FOLDER
done

# Go to native-image folder and generate the executables for each .jar
cd $NATIVE_IMAGE_FOLDER

find "." -name '*.jar' | while read APP_JAR; do
echo "Processing .jar '$APP_JAR' on $NATIVE_IMAGE_FOLDER folder"
native-image -J-Xmx2G -J-Xms2G --no-server -H:+ReportUnsupportedElementsAtRuntime -jar ${APP_JAR}
done
```


Creates a `native-image` folder in the project folder, triggers SBT's task `sbt assembly`, moves the created `.jar` to `native-image` folder, and at last but not least, generates the native image using GRAALVM `native-image` command.


Once the executable is ready in the `native-image` folder, we can run it inside the very same container, by changing the `command` in the `docker-compose` to point to `./native-image/{GENERATED-EXECUTABLE-FILE}`, or ship it to S3, or create a new docker image and publish to AWS ECR, etc... what to do with the executable is matter of creativity.


Note that we presume that the current Scala project is using [sbt-assembly](https://github.com/sbt/sbt-assembly) plugin, but any other plugin could be used, like [sbt-pack](https://github.com/xerial/sbt-pack), [sbt-native-packager](https://www.scala-sbt.org/sbt-native-packager/), [sbt-onejar](https://github.com/sbt/sbt-onejar), and [sbt-proguard](https://github.com/sbt/sbt-proguard).


## Spark Job example


The folder `spark-job-graalvm-example` contains an example where it uses the docker-compose and shell above.


[![asciicast](https://asciinema.org/a/181283.png)](https://asciinema.org/a/181283)


## References and Inspiration

- [GraalVM](https://www.graalvm.org/)
- [An experiment running http4s as native image with Graal (+ Substrate)](https://github.com/hhandoko/http4s-graal)
- [GraphQL server built with sangria, http4s and circe which compiles and runs as a GraalVM native image.](https://github.com/OlegIlyenko/sangria-http4s-graalvm-example)
- [An example of sangria + circe app compiled with GraalVM native-image](https://github.com/OlegIlyenko/graalvm-sangria-test)
- [Running Play on GraalVM](https://blog.playframework.com/play-on-graal/)
- [Native Clojure with GraalVM](https://www.innoq.com/en/blog/native-clojure-and-graalvm/)
- [Oracles GraalVM für "Native Java"?](https://www.innoq.com/de/blog/native-java-graalvm/)
- [Running Apache Spark jobs on Azure Kubernetes Service](https://docs.microsoft.com/en-us/azure/aks/spark-job)
- [Scala and sbt Dockerfile](https://github.com/hseeberger/scala-sbt)
- [Build sbt using Docker](https://github.com/jaceklaskowski/docker-builds-sbt)


## License

MIT License

Copyright (c) 2018 Vitor S. Vieira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions docker-compose-example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'

services:
app:
image: scalaops/scala-graalvm:scala-2.12.6-sbt-1.1.5-graalvm-1.0.0-rc1
command: ./generate-native-image-example.sh
volumes:
- .:/app
- ~/.ivy2:/root/.ivy2
- ~/.sbt:/root/.sbt
- ~/.coursier:/root/.coursier
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM debian:stretch-slim

# Versions
ENV SCALA_VERSION 2.11.8
ENV SBT_VERSION 0.13.15
ENV GRAALVM_VERSION 1.0.0-rc1

# Environment variables
ENV ROOT /root
ENV BASHRC ${ROOT}/.bashrc
ENV SCALA_HOME $ROOT/scala-${SCALA_VERSION}
ENV GRAALVM_HOME $ROOT/graalvm-${GRAALVM_VERSION}
ENV SBT_HOME $ROOT/sbt-${SBT_VERSION}
ENV PATH=$SCALA_HOME/bin:$GRAALVM_HOME/bin:$SBT_HOME/bin:$PATH

# Update base image deps
RUN apt-get update && \
apt-get install -y curl gcc libz-dev && \
rm -rf /var/lib/apt/lists/*

# Install GraalVM
RUN mkdir $GRAALVM_HOME && \
curl -fsL https://github.com/oracle/graal/releases/download/vm-$GRAALVM_VERSION/graalvm-ce-$GRAALVM_VERSION-linux-amd64.tar.gz | tar -xvzf - -C $GRAALVM_HOME --strip-components=1

# Install Scala
RUN mkdir $SCALA_HOME && \
curl -fsL https://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C $SCALA_HOME --strip-components=1

# Install sbt
RUN mkdir $SBT_HOME && \
curl -fsL https://sbt-downloads.cdnedge.bluemix.net/releases/v$SBT_VERSION/sbt-$SBT_VERSION.tgz | tar xfz - -C $SBT_HOME --strip-components=1

# Add path to .bashrc
RUN echo >> ${BASHRC} && \
echo "export $PATH" >> ${BASHRC}

# Set the working directory to /app
RUN mkdir -p /app
WORKDIR /app

# Share host's current directory content with container's /app directory
ADD . /app
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM debian:stretch-slim

# Versions
ENV SCALA_VERSION 2.11.8
ENV SBT_VERSION 0.13.16
ENV GRAALVM_VERSION 1.0.0-rc1

# Environment variables
ENV ROOT /root
ENV BASHRC ${ROOT}/.bashrc
ENV SCALA_HOME $ROOT/scala-${SCALA_VERSION}
ENV GRAALVM_HOME $ROOT/graalvm-${GRAALVM_VERSION}
ENV SBT_HOME $ROOT/sbt-${SBT_VERSION}
ENV PATH=$SCALA_HOME/bin:$GRAALVM_HOME/bin:$SBT_HOME/bin:$PATH

# Update base image deps
RUN apt-get update && \
apt-get install -y curl gcc libz-dev && \
rm -rf /var/lib/apt/lists/*

# Install GraalVM
RUN mkdir $GRAALVM_HOME && \
curl -fsL https://github.com/oracle/graal/releases/download/vm-$GRAALVM_VERSION/graalvm-ce-$GRAALVM_VERSION-linux-amd64.tar.gz | tar -xvzf - -C $GRAALVM_HOME --strip-components=1

# Install Scala
RUN mkdir $SCALA_HOME && \
curl -fsL https://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C $SCALA_HOME --strip-components=1

# Install sbt
RUN mkdir $SBT_HOME && \
curl -fsL https://sbt-downloads.cdnedge.bluemix.net/releases/v$SBT_VERSION/sbt-$SBT_VERSION.tgz | tar xfz - -C $SBT_HOME --strip-components=1

# Add path to .bashrc
RUN echo >> ${BASHRC} && \
echo "export $PATH" >> ${BASHRC}

# Set the working directory to /app
RUN mkdir -p /app
WORKDIR /app

# Share host's current directory content with container's /app directory
ADD . /app
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM debian:stretch-slim

# Versions
ENV SCALA_VERSION 2.11.8
ENV SBT_VERSION 0.13.17
ENV GRAALVM_VERSION 1.0.0-rc1

# Environment variables
ENV ROOT /root
ENV BASHRC ${ROOT}/.bashrc
ENV SCALA_HOME $ROOT/scala-${SCALA_VERSION}
ENV GRAALVM_HOME $ROOT/graalvm-${GRAALVM_VERSION}
ENV SBT_HOME $ROOT/sbt-${SBT_VERSION}
ENV PATH=$SCALA_HOME/bin:$GRAALVM_HOME/bin:$SBT_HOME/bin:$PATH

# Update base image deps
RUN apt-get update && \
apt-get install -y curl gcc libz-dev && \
rm -rf /var/lib/apt/lists/*

# Install GraalVM
RUN mkdir $GRAALVM_HOME && \
curl -fsL https://github.com/oracle/graal/releases/download/vm-$GRAALVM_VERSION/graalvm-ce-$GRAALVM_VERSION-linux-amd64.tar.gz | tar -xvzf - -C $GRAALVM_HOME --strip-components=1

# Install Scala
RUN mkdir $SCALA_HOME && \
curl -fsL https://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C $SCALA_HOME --strip-components=1

# Install sbt
RUN mkdir $SBT_HOME && \
curl -fsL https://sbt-downloads.cdnedge.bluemix.net/releases/v$SBT_VERSION/sbt-$SBT_VERSION.tgz | tar xfz - -C $SBT_HOME --strip-components=1

# Add path to .bashrc
RUN echo >> ${BASHRC} && \
echo "export $PATH" >> ${BASHRC}

# Set the working directory to /app
RUN mkdir -p /app
WORKDIR /app

# Share host's current directory content with container's /app directory
ADD . /app
42 changes: 42 additions & 0 deletions docker-images/scala-2.11.8-sbt-1.0.0-graalvm-1.0.0-rc1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM debian:stretch-slim

# Versions
ENV SCALA_VERSION 2.11.8
ENV SBT_VERSION 1.0.0
ENV GRAALVM_VERSION 1.0.0-rc1

# Environment variables
ENV ROOT /root
ENV BASHRC ${ROOT}/.bashrc
ENV SCALA_HOME $ROOT/scala-${SCALA_VERSION}
ENV GRAALVM_HOME $ROOT/graalvm-${GRAALVM_VERSION}
ENV SBT_HOME $ROOT/sbt-${SBT_VERSION}
ENV PATH=$SCALA_HOME/bin:$GRAALVM_HOME/bin:$SBT_HOME/bin:$PATH

# Update base image deps
RUN apt-get update && \
apt-get install -y curl gcc libz-dev && \
rm -rf /var/lib/apt/lists/*

# Install GraalVM
RUN mkdir $GRAALVM_HOME && \
curl -fsL https://github.com/oracle/graal/releases/download/vm-$GRAALVM_VERSION/graalvm-ce-$GRAALVM_VERSION-linux-amd64.tar.gz | tar -xvzf - -C $GRAALVM_HOME --strip-components=1

# Install Scala
RUN mkdir $SCALA_HOME && \
curl -fsL https://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C $SCALA_HOME --strip-components=1

# Install sbt
RUN mkdir $SBT_HOME && \
curl -fsL https://sbt-downloads.cdnedge.bluemix.net/releases/v$SBT_VERSION/sbt-$SBT_VERSION.tgz | tar xfz - -C $SBT_HOME --strip-components=1

# Add path to .bashrc
RUN echo >> ${BASHRC} && \
echo "export $PATH" >> ${BASHRC}

# Set the working directory to /app
RUN mkdir -p /app
WORKDIR /app

# Share host's current directory content with container's /app directory
ADD . /app
42 changes: 42 additions & 0 deletions docker-images/scala-2.11.8-sbt-1.0.1-graalvm-1.0.0-rc1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM debian:stretch-slim

# Versions
ENV SCALA_VERSION 2.11.8
ENV SBT_VERSION 1.0.1
ENV GRAALVM_VERSION 1.0.0-rc1

# Environment variables
ENV ROOT /root
ENV BASHRC ${ROOT}/.bashrc
ENV SCALA_HOME $ROOT/scala-${SCALA_VERSION}
ENV GRAALVM_HOME $ROOT/graalvm-${GRAALVM_VERSION}
ENV SBT_HOME $ROOT/sbt-${SBT_VERSION}
ENV PATH=$SCALA_HOME/bin:$GRAALVM_HOME/bin:$SBT_HOME/bin:$PATH

# Update base image deps
RUN apt-get update && \
apt-get install -y curl gcc libz-dev && \
rm -rf /var/lib/apt/lists/*

# Install GraalVM
RUN mkdir $GRAALVM_HOME && \
curl -fsL https://github.com/oracle/graal/releases/download/vm-$GRAALVM_VERSION/graalvm-ce-$GRAALVM_VERSION-linux-amd64.tar.gz | tar -xvzf - -C $GRAALVM_HOME --strip-components=1

# Install Scala
RUN mkdir $SCALA_HOME && \
curl -fsL https://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C $SCALA_HOME --strip-components=1

# Install sbt
RUN mkdir $SBT_HOME && \
curl -fsL https://sbt-downloads.cdnedge.bluemix.net/releases/v$SBT_VERSION/sbt-$SBT_VERSION.tgz | tar xfz - -C $SBT_HOME --strip-components=1

# Add path to .bashrc
RUN echo >> ${BASHRC} && \
echo "export $PATH" >> ${BASHRC}

# Set the working directory to /app
RUN mkdir -p /app
WORKDIR /app

# Share host's current directory content with container's /app directory
ADD . /app
Loading

0 comments on commit c8d3c21

Please sign in to comment.