1

Say we have this:

RUN go get 'github.com/foo/one'
RUN go get 'github.com/foo/two'
RUN go get 'github.com/foo/three'

and we change the order:

RUN go get 'github.com/foo/two'
RUN go get 'github.com/foo/three'
RUN go get 'github.com/foo/one'

the cache will get completely invalidated. Is there some way to tell docker build that the order of the dependencies doesn't matter. Some syntax like this:

PARALLEL START
RUN go get 'github.com/foo/two'
RUN go get 'github.com/foo/three'
RUN go get 'github.com/foo/one'
PARALLEL END
  • see https://docs.docker.com/engine/reference/commandline/build/#squash-an-images-layers---squash-experimental – LinPy Jan 21 '20 at 09:26
  • If you are worried about this, I guess it is because you have several different build scripts that do equivalent operations. You might be better off rearranging those build scripts to be consistent. You might consider refactoring them to extract common parts; you might want to have a consistent base image for all your images, for example. – Raedwald Jan 21 '20 at 12:55

2 Answers2

1

That's not possible due to how the layer system while building an image works.

An image is built by adding content on top of the existent content, so if you change the order, you're changing the structure and therefore, building a different image.

It's not that the cache is "invalidated", it's that there is no cache at all. From a docker point of view, they are different images at all.

  • I am sure the docker layers are still on the fs until garbage collection happens, so I think invalidated is the right word. –  Jan 21 '20 at 17:54
  • @MrCholo I think you didn't understand my point. Of course the layers are on the fs, but as I said, those layers are completely different if you change the order. In Docker, the order of the factors **does** change the product –  Jan 21 '20 at 20:11
  • sure, but the question is if there is a way to make independent layers. what I was saying in the previous comment is that the layers are not deleted, the layers exist on the fs still, it's only that new layers are created and the old ones are not used in the new build. it seems possible to create independent layers, where the order wouldn't matter. –  Jan 21 '20 at 20:25
1

Docker uses layered architecture to make an image. Suppose your file contains following commands:command 1 command 2 command 3

What will docker do is to execute command 1 and create a layer1 and on top of that layer it will execute other commands to add layer 2 and layer 3 respectfully. You need to understand this that by using this pattern docker makes use of caching process. If you change the commands order it will recreate layers from that position from which you changed your order.

Umar Tahir
  • 585
  • 6
  • 21