Scala in Docker

A container is the shortest path to a Scala toolchain that does not touch the host. The community image publishes scala-cli and a JDK together, so one docker run compiles and runs a program, and a build server gets the same version every time. This is the Docker page of the install overview. The commands below are shown and not run here: they need a Docker client and a daemon, and the page was written without either.

A one-liner

The image is virtuslab/scala-cli. Mount the folder of the sources at the work directory of the image, and the command of the course runs unchanged:

docker run --rm -v "$PWD":/work -w /work virtuslab/scala-cli:latest
    scala-cli run --server=false hello.scala

not run in this environment

--rm deletes the container when the command ends, and -w makes the mount the working directory, so a relative file name finds the file. A tag names a release or latest; a build that has to repeat pins the release, because latest moves.

A project in the image

A folder with a project.scala and a src/main/scala is run with the dot, as on the host:

docker run --rm -v "$PWD":/work -w /work virtuslab/scala-cli:latest
    scala-cli run --server=false .

not run in this environment

The image has a dependency cache of its own. A project that needs a library downloads it on the first run unless the cache is a volume, which is the same trade a Maven or Gradle cache makes:

docker run --rm -v scala-cli-cache:/root/.cache/coursier -v "$PWD":/work -w /work
    virtuslab/scala-cli:latest scala-cli run --server=false .

not run in this environment

Why the commands are not run

The three commands above need a Docker client that can reach a daemon, and the page was written without one, so they are the commands a reader runs on a machine with Docker. The images themselves are what the section below is about. The Docker section has the pages about the images themselves.

Related

Article author: Arthur Isaev