Does docker reuse images when multiple containers

2019-05-03 11:15发布

问题:

My understanding is that Docker creates an image layer at every stage of a dockerfile.

If I have X containers running on the same machine (where X >=2) and every container has a common underlying image layer (ie. debian), will docker keep only one copy of the base image on that machine, or does it have multiple copies for each container?

Is there a point this breaks down, or is it true for every layer in the dockerfile?

How does this work?

Does Kubernetes affect this in any way?

回答1:

Dockers Understand images, containers, and storage drivers details most of this.

From Docker 1.10 onwards, all the layers that make up an image have an SHA256 secure content hash associated with them at build time. This hash is consistent across hosts and builds, as long as the content of the layer is the same.

If any number of images share a layer, only the 1 copy of that layer will be stored and used by all images on that instance of the Docker engine.

A tag like debian can refer to multiple SHA256 image hash's over time as new releases come out. Two images that are built with FROM debian don't necessarily share layers, only if the SHA256 hash's match.

Anything that runs the Docker Engine underneath will use this storage setup.

This sharing also works in the Docker Registry (>2.2 for the best results). If you were to push images with layers that already exist on that registry, the existing layers are skipped. Same with pulling layers to your local engine.