Search code examples
scalasbtsbt-plugin

SBT always downloads the packages/scala libraries on Docker, docker-compose


I have recently installed SBT on Docker Ubuntu machine to get started with Scala. When I started docker initially, it started grabbing all the Java, sbt JAR's from the remote locations (https://repo.scala-sbt.org/scalasbt/debian/sbt-0.13.17.deb).

But, whenever I run sbt command, it again starts downloading the sbt JAR. Is there a way of maintaining a global cache whereby artifacts are only downloaded once and not every time I remote to docker container?


Solution

  • My solution to this was a multi stage build.

    Have a “base” docker image.

    Copy in only build.sbt, projects.sbt and the file which sets out the sbt version from your project.

    That defines the required dependencies. The last line in that base image is “sbt update” - I.e fetch them. That “base image” has the dependencies in it… and is reuseable. Just remember to run it when you change library versions etc to rebuild it.

    In the ”build” image… copy over the project and proceed as normal… make sure sbt is resolving from maven-local, and it should use the “cache”… which is already in place from the paragraph above.

    I’d be interested to hear other answers, but that’s my solution… YMMV :-).

    That works for me on a cloud / Kube pipeline.