【问题标题】:How to conditionally pull the latest tag of a Docker image, instead of using the cached version?如何有条件地拉取 Docker 镜像的最新标签,而不是使用缓存版本?
【发布时间】:2020-06-17 23:12:17
【问题描述】:

我的 Dockerfile 包含这一行:

COPY --from=whatwg/wattsi:latest /whatwg/wattsi/bin/wattsi /bin/wattsi

即,它正在从 Docker Hub 上可用的 whatwg/wattsi 映像复制可执行文件。这基本上直接来自multi-stage builds 上的文档。

但是,一旦我运行 Dockerfile,它就会缓存 whatwg/wattsi:latest 的本地副本。然后,任何被推送到 Docker Hub 的 whatwg/wattsi 后续更新都将被忽略,并使用缓存的副本。 (即,这整行被跳过,它创建的层被重用。)

我想要的行为是让 Docker 将远程 whatwg/wattsi:latest 与本地缓存副本进行比较,如果有差异则重新下载。这可能吗?

我想在不将whatwg/wattsi 的版本硬编码到我的Dockerfile 的情况下执行此操作,每次whatwg/wattsi revs 都需要更新。

【问题讨论】:

    标签: docker docker-multi-stage-build


    【解决方案1】:

    docker build 有一个 --pull 选项,该选项将“始终尝试拉取更新版本的图像”。

    第一次构建(没有缓存)

    Step 2/2 : COPY --from=whatwg/wattsi:latest /whatwg/wattsi/bin/wattsi /bin/watt
    latest: Pulling from whatwg/wattsi
    24f0c933cbef: Pull complete 
    69e2f037cdb3: Pull complete 
    4f7407c4e0dc: Pull complete 
    Digest: sha256:f555e4ff56b88313c7c47ca86b83367f9c1ca93552c477a96b9943e907bb7733
    Status: Downloaded newer image for whatwg/wattsi:latest
     ---> 2ca5d7a1e784
    

    第二次构建(使用缓存)

    Step 2/2 : COPY --from=whatwg/wattsi:latest /whatwg/wattsi/bin/wattsi /bin/watt
     ---> Using cache
     ---> 2ca5d7a1e784
    

    使用--pull进行第三次构建(检查更新)

    Step 2/2 : COPY --from=whatwg/wattsi:latest /whatwg/wattsi/bin/wattsi /bin/watt
    latest: Pulling from whatwg/wattsi
    Digest: sha256:f555e4ff56b88313c7c47ca86b83367f9c1ca93552c477a96b9943e907bb7733
    Status: Image is up to date for whatwg/wattsi:latest
     ---> 7d3390252ae1
    

    【讨论】:

      【解决方案2】:

      无法在 dockerfile 中写入。

      但你可以运行

      docker build --pull
      

      来自文档

      --pull 总是尝试拉取更新版本的镜像 https://docs.docker.com/engine/reference/commandline/build/#options

      和跑步一样

      docker pull whatwg/wattsi:latest
      

      在您的docker build 之前。它只是检查您的本地映像副本是否是最新的,如果不是,则拉取较新的版本。

      这个问题不仅存在于构建,也存在于运行它们。 Kubernetes 用imagePullPolicy 解决了这个问题。 (见https://kubernetes.io/docs/concepts/containers/images/#updating-images

      【讨论】:

        猜你喜欢
        • 2017-10-25
        • 1970-01-01
        • 2017-11-12
        • 1970-01-01
        • 2015-07-14
        • 2020-11-07
        • 2018-07-12
        • 1970-01-01
        • 2018-02-02
        相关资源
        最近更新 更多