I have two images that use a two-stage build to build Scala code and copy the artifacts to a final image. To speed up the build, I copy my local ~/.ivy2 to the context directory and from there to the images (~1GB). Unfortunately this means that even when nothing has changed and the images don't need to be re-built, docker-compose build (or docker build) hangs for quite a while to copy Docker context. This happens twice of course, once for each image.
Is there any cleverer way to do this?
Dockerfile:
FROM openjdk:8
RUN apt-get update &&\
apt-get install -y apt-transport-https gnupg2 &&\
echo "deb https://dl.bintray.com/sbt/debian /" | tee -a /etc/apt/sources.list.d/sbt.list &&\
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 642AC823 &&\
apt-get update &&\
apt-get install -y sbt=1.1.6
COPY ivy-cache/ /root/.ivy2
COPY app/source/ /app/source
RUN cd /app/source &&\
sbt assembly &&\
cp target/scala-2.11/my-app-*.jar /app/my-app.jar
FROM gettyimages/spark:2.3.1-hadoop-3.0
COPY --from=0 /app/my-app.jar /app/my-app.jar
CMD ["spark-submit", "--master", "local", "/app/my-app.jar"]