Is there a way to add only changed files to a docker image as a new layer - without resorting to docker commit?

后端 未结 3 2022
梦如初夏
梦如初夏 2021-02-07 22:46

TL;DR

Running COPY . /app on top of an image with but slightly outdated source code creates a new layer as large as the whole source code,

3条回答
  •  情书的邮戳
    2021-02-07 23:39

    Docker caching works per layer / instruction in the Dockerfile. In this case the files used in that layer (everything in the build-context (.)) are modified, so the layer needs to be rebuilt.

    If there's specific parts of the code that don't change often, you could consider to add those in a separate layer, or even move those to a "base image"

    FROM mybaseimage
    COPY ./directories-that-dont-change-often /somewhere
    COPY ./directories-that-change-often /somewhere
    

    It may take some planning or restructuring for this to work, depending on your project, but may be worth doing.

提交回复
热议问题