给定:我想要构建一个Dockerfile来编译Scala应用程序。为了加快构建速度,我希望将依赖项下载缓存。
问题:命令./sbt -sbt-dir ./sbt-dir -ivy ./ivy update
由于某种原因无法被缓存。
FROM openjdk:8 as workspace
ARG BUILD_VERSION
WORKDIR /build
COPY ./sbt ./sbt
COPY ./sbt-dist ./sbt-dist
COPY ./build.sbt ./build.sbt
COPY ./project/build.properties ./project/build.properties
COPY ./project/plugins.sbt ./project/plugins.sbt
RUN ./sbt -sbt-dir ./sbt-dir -ivy ./ivy update
COPY ./ ./
# Embedded postgres need to be run as non-root user
RUN useradd -ms /bin/bash runner
RUN chown -R runner /build
USER runner
RUN ./sbt -sbt-dir ./sbt-dir -ivy ./ivy clean test
RUN ./sbt -sbt-dir ./sbt-dir -ivy ./ivy docker:stage -Ddocker.image.version="${BUILD_VERSION}"
因为每次都在新的虚拟机中运行此构建,我会在本次构建后推送工作空间镜像,并在下次运行时从中拉取以构建缓存。
docker build --rm=false --cache-from=workspace --build-arg BUILD_VERSION=1 -t workspace .
这里是一个输出结果的片段。
Step 2/22 : ARG BUILD_VERSION
---> Using cache
---> de98ffcfad8e
Step 3/22 : WORKDIR /build
---> Using cache
---> 253b71142240
Step 4/22 : COPY ./sbt ./sbt
---> Using cache
---> 3091fa1e1821
Step 5/22 : COPY ./sbt-dist ./sbt-dist
---> Using cache
---> f9c68659cd91
Step 6/22 : COPY ./build.sbt ./build.sbt
---> Using cache
---> d30058c451fc
Step 7/22 : COPY ./project/build.properties ./project/build.properties
---> Using cache
---> 7451eb63303f
Step 8/22 : COPY ./project/plugins.sbt ./project/plugins.sbt
---> Using cache
---> 79ac2d1e5ff5
Step 9/22 : RUN ./sbt -sbt-dir ./sbt-dir -ivy ./ivy update
---> Running in 609104e7045e
Getting org.scala-sbt sbt 1.0.3 ...
有人能解释一下为什么Docker在这里没有使用缓存吗?如果有一个解释缓存何时真正决定使用缓存的链接也可以。据我所知,在这里,Docker应该使用缓存,直到RUN命令的签名发生变化。谢谢!