【发布时间】:2018-06-27 17:58:15
【问题描述】:
Given:我想构建一个 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}"
因为此构建始终在新 VM 中运行,所以我推送工作区映像并在下一次运行时将其拉出以从中构建缓存
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 命令的签名发生变化。
提前致谢
【问题讨论】:
标签: scala docker caching dockerfile