0

Is there a practical difference and preferable option between:

COPY folderOrFile1 /app/folderOrFile1
COPY folderOrFile2 /app/folderOrFile2
....    
COPY folderOrFile10 /app/folderOrFile10

vs

COPY folderOrFile1 folderOrFile2 ... folderOrFile10  /app/

In docker each COPY command creates a layer, so is there a real reason to try to minimize the number of COPY commands and layers in this particular case?

WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
  • The number of layers in an image is also restricted, see here for example: https://stackoverflow.com/questions/39382518/whats-the-reason-for-the-42-layer-limit-in-docker/39383801#39383801 – Henry Nov 28 '17 at 14:22
  • Are you likely to add or remove individual lines? If so, having them as separate layers makes sense, as it'll save on build time. Is it rather much more likely these files are always copied as one single unit? Then it makes more sense to keep them as one layer. What you mean by a "real" reason is entirely subjective, though. There are "real" reasons to minimize the number of layers, as the linked question explains. – Jeroen Mostert Nov 28 '17 at 14:23

1 Answers1

1

Personally I prefer to do a:

COPY app/ /app/

when possible, and have all the folders organized how I want them in the build context. It's more maintainable for me and makes the Dockerfile easier to read.

With your two options, there's not a huge difference. As long as each layer is independent of each other, the image size won't show a noticeable increase to have multiple layers.

It is good to reduce the number of layers in general to avoid the limits. But you'd need to be doing a lot of other things to the image to hit that limit, or have a large number of COPY commands, to hit those limits. Aufs with it's 42 layer limit is being phased out in favor of overlay2 which has up to 128 layers in it's limit. You could see a minor performance impact from lots of layers as the filesystem driver needs to search each layer for a read operation. So fewer is better, but more so when you get to the extremes.

BMitch
  • 231,797
  • 42
  • 475
  • 450