Since docker layers are just filesystems is it possible to reuse a specific layer directly outside of cache build.
I mean imagesA has some layers that are layers that can be used by imageX. Is such a sharing/reuse even possible?

- 3,845
- 3
- 22
- 47
-
The layers are shared, but must be downloaded either from the container repository, cache, or local image resource each time they're used. If you want to use a specific layer, you should probably build an intermediate image that subsequent builds can base `FROM`. – tadman Jun 21 '23 at 15:02
-
@tadman: you are talking about building imageA and imageX at the same machine and reusing the build cache? I was asking e.g. `imageA` is some image in the registry and somehow reuse layers from that for `imageX` – Jim Jun 21 '23 at 15:10
-
It won't happen automatically except when building locally. Now if you've pulled image A, and it contains layer X, then you're building B that also involves X, but as an intermediate step, there's no way for Docker to know where your build will go until it starts building, it starts at layer 0 each time, the `FROM` base, unless it can reasonably start mid-way. This is why expressing those common intermediate layers as an image with a name, and presumably a repository entry, is key. – tadman Jun 21 '23 at 20:25
-
Now the only way two different images have a common layer is if they have the same precursor steps, or in other words, they use a common base, even if it's not expressed as such. To streamline builds, split out those common steps as a base for these other images to use. – tadman Jun 21 '23 at 20:25
2 Answers
You cannot just copy-paste layers from one image to another.
The closest options you can do in order to reuse Docker layers are:
reuse an image by specifying the
FROM <image_name:version>
in the Dockerfile and use it as base image to add your layers.use the
--cache-from
option during the build to force to use a specific image, such as the commanddocker build --cache-from=<registry>/image:version
.--cache-from
allows to consider a specific image as part of it's build cache and Docker will use that specified image in case the current build have layers in common.

- 1,913
- 3
- 18
- 22
-
I was asking about specific layers. Not extending an image and hence having all is layers available – Jim Jun 21 '23 at 15:45
-
An image is basically a group of layers. You can create images that contain the layers you need and: 1) use them as base images or 2) --cache-from them. If you want just copy-paste layers you cannot. I edited my answer with this information. – Alez Jun 21 '23 at 16:49
Is it possible to share layers between multiple images? Yes, that's how base images work.
The question you didn't ask (probably because it would be off topic): is there tooling that lets you pick specific layers to add to another image? Not that I'm aware of, and supporting that tooling would be a nightmare of user requests to know why the image they created this way is broken. E.g. there's nothing stopping you from building a manifest that merges the layers of a debian and alpine image together. But there's zero guarantee that such an image would work and not have unexpected results.
If you wanted to do this, it would be up to you to do the blob copy/mount between repositories (on the same registry that's a server side link). And then you would create the image config and manifest with the layers listed.
The closest I've seen to do something like this are tools that rebase images, swapping out the layers of one base image for another, typically for a rapid upgrade without going through the full image build process. That's seen with tools like buildpacks, crane, and regclient.

- 231,797
- 42
- 475
- 450