|
| 1 | +# |
| 2 | +# Scala and sbt Dockerfile |
| 3 | +# |
| 4 | +# https://github.com/sbt/docker-sbt |
| 5 | +# |
| 6 | + |
| 7 | +# Pull base image |
| 8 | +ARG BASE_IMAGE_TAG |
| 9 | +FROM amazoncorretto:${BASE_IMAGE_TAG:-21.0.5} |
| 10 | + |
| 11 | +# Env variables |
| 12 | +ARG SCALA_VERSION |
| 13 | +ENV SCALA_VERSION=${SCALA_VERSION:-3.3.4} |
| 14 | +ARG SBT_VERSION |
| 15 | +ENV SBT_VERSION=${SBT_VERSION:-1.10.5} |
| 16 | +ARG USER_ID |
| 17 | +ENV USER_ID=${USER_ID:-1001} |
| 18 | +ARG GROUP_ID |
| 19 | +ENV GROUP_ID=${GROUP_ID:-1001} |
| 20 | + |
| 21 | +# Install git and rpm for sbt-native-packager (see https://github.com/sbt/docker-sbt/pull/114) |
| 22 | +RUN \ |
| 23 | + yum -y update && \ |
| 24 | + yum -y install tar gzip git rpm && \ |
| 25 | + rm -rf /var/cache/yum/* && \ |
| 26 | + yum clean all |
| 27 | + |
| 28 | +# Install sbt |
| 29 | +RUN \ |
| 30 | + curl -fsL --show-error "https://github.com/sbt/sbt/releases/download/v$SBT_VERSION/sbt-$SBT_VERSION.tgz" | tar xfz - -C /usr/share && \ |
| 31 | + chown -R root:root /usr/share/sbt && \ |
| 32 | + chmod -R 755 /usr/share/sbt && \ |
| 33 | + ln -s /usr/share/sbt/bin/sbt /usr/local/bin/sbt |
| 34 | + |
| 35 | +# Install Scala |
| 36 | +RUN \ |
| 37 | + case $SCALA_VERSION in \ |
| 38 | + "3"*) URL=https://github.com/lampepfl/dotty/releases/download/$SCALA_VERSION/scala3-$SCALA_VERSION.tar.gz SCALA_DIR=/usr/share/scala3-$SCALA_VERSION ;; \ |
| 39 | + *) URL=https://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz SCALA_DIR=/usr/share/scala-$SCALA_VERSION ;; \ |
| 40 | + esac && \ |
| 41 | + curl -fsL --show-error $URL | tar xfz - -C /usr/share && \ |
| 42 | + mv $SCALA_DIR /usr/share/scala && \ |
| 43 | + chown -R root:root /usr/share/scala && \ |
| 44 | + chmod -R 755 /usr/share/scala && \ |
| 45 | + ln -s /usr/share/scala/bin/* /usr/local/bin && \ |
| 46 | + mkdir -p /test && \ |
| 47 | + case $SCALA_VERSION in \ |
| 48 | + "3"*) echo 'import java.io.FileInputStream;import java.util.jar.JarInputStream;val scala3LibJar = classOf[CanEqual[_, _]].getProtectionDomain.getCodeSource.getLocation.toURI.getPath;val manifest = new JarInputStream(new FileInputStream(scala3LibJar)).getManifest;val ver = manifest.getMainAttributes.getValue("Implementation-Version");@main def main = println(s"Scala version ${ver}")' > /test/test.scala ;; \ |
| 49 | + *) echo "println(util.Properties.versionMsg)" > /test/test.scala ;; \ |
| 50 | + esac && \ |
| 51 | + scala -nocompdaemon test/test.scala && \ |
| 52 | + rm -fr test |
| 53 | + |
| 54 | +# Symlink java to have it available on sbtuser's PATH |
| 55 | +RUN ln -s /opt/java/openjdk/bin/java /usr/local/bin/java |
| 56 | + |
| 57 | +# Add and use user sbtuser |
| 58 | +RUN groupadd --gid $GROUP_ID sbtuser && useradd -m --gid $GROUP_ID --uid $USER_ID sbtuser --shell /bin/bash |
| 59 | +USER sbtuser |
| 60 | + |
| 61 | +# Switch working directory |
| 62 | +WORKDIR /home/sbtuser |
| 63 | + |
| 64 | +# Prepare sbt (warm cache) |
| 65 | +RUN \ |
| 66 | + sbt sbtVersion && \ |
| 67 | + mkdir -p project && \ |
| 68 | + echo "scalaVersion := \"${SCALA_VERSION}\"" > build.sbt && \ |
| 69 | + echo "sbt.version=${SBT_VERSION}" > project/build.properties && \ |
| 70 | + echo "// force sbt compiler-bridge download" > project/Dependencies.scala && \ |
| 71 | + echo "case object Temp" > Temp.scala && \ |
| 72 | + sbt compile && \ |
| 73 | + rm -r project && rm build.sbt && rm Temp.scala && rm -r target |
| 74 | + |
| 75 | +# Link everything into root as well |
| 76 | +# This allows users of this container to choose, whether they want to run the container as sbtuser (non-root) or as root |
| 77 | +USER root |
| 78 | +RUN \ |
| 79 | + rm -rf /tmp/..?* /tmp/.[!.]* * && \ |
| 80 | + ln -s /home/sbtuser/.cache /root/.cache && \ |
| 81 | + ln -s /home/sbtuser/.sbt /root/.sbt && \ |
| 82 | + if [ -d "/home/sbtuser/.ivy2" ]; then ln -s /home/sbtuser/.ivy2 /root/.ivy2; fi |
| 83 | + |
| 84 | +# Switch working directory back to root |
| 85 | +## Users wanting to use this container as non-root should combine the two following arguments |
| 86 | +## -u sbtuser |
| 87 | +## -w /home/sbtuser |
| 88 | +WORKDIR /root |
| 89 | + |
| 90 | +CMD ["sbt"] |
0 commit comments