Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do docker images share identical layers? [duplicate]

I know that each container is an image with a readable/writeable layer on top of a bunch of read-only layers, and that multiple containers can share the read-only layers of the image. Do two images that were created from the same base image share their identical images?

Example:

  • Image A has 5 layers and weighs 1GB.
  • Image B is created with A as its base image and adds another layer and weighs 1.1GB.
  • Image C is created with A as its base image and adds another layer and weighs 1.5GB

Is the total disk space now 3.6GB or 1.6GB?

like image 903
amos-baron Avatar asked Aug 31 '25 15:08

amos-baron


1 Answers

Short answer: 1.6GB


This is an interesting experiment you can perform:

Pull dummy image:

docker pull alpine

Prepare a Dockerfile for a child image alpine(here I created a 10MB file in the image using dd)

FROM alpine
RUN dd if=/dev/zero of=file.txt count=10000 bs=1024

Build the child image

docker build -t alpine-plus-ten-mb .

Then inspect the two images and have a look at the layers.

  • The lower directory can be read-only or could be an overlay itself.
  • The upper directory is normally writable.
  • The merged directory is the unified view between upper and lower
  • The work directory is used to prepare files as they are switched between the layers.
docker image inspect --format='{{json .GraphDriver.Data}}' alpine
{
    "MergedDir": "/var/lib/docker/overlay2/0654e44ddf13ebd2a0feb2ac6261e62f6c83a8be1937a71c544f69eb6208d93b/merged",
    "UpperDir": "/var/lib/docker/overlay2/0654e44ddf13ebd2a0feb2ac6261e62f6c83a8be1937a71c544f69eb6208d93b/diff",
    "WorkDir": "/var/lib/docker/overlay2/0654e44ddf13ebd2a0feb2ac6261e62f6c83a8be1937a71c544f69eb6208d93b/work"
}

docker image inspect --format='{{json .GraphDriver.Data}}' alpine-plus-ten-mb
{
    "LowerDir": "/var/lib/docker/overlay2/0654e44ddf13ebd2a0feb2ac6261e62f6c83a8be1937a71c544f69eb6208d93b/diff",
    "MergedDir": "/var/lib/docker/overlay2/5ca936630339967105c28d4d8c9669d99f0f449a307c43c09d60f6341cf56271/merged",
    "UpperDir": "/var/lib/docker/overlay2/5ca936630339967105c28d4d8c9669d99f0f449a307c43c09d60f6341cf56271/diff",
    "WorkDir": "/var/lib/docker/overlay2/5ca936630339967105c28d4d8c9669d99f0f449a307c43c09d60f6341cf56271/work"
}

Note that the UpperDir of the base alpine image (...d93b/diff) appears to be LowerDir for the derived image alpine-plus-ten-mb.

One important aspect: the layer ...d93b/diff is read-only for the child image alpine-plus-ten-mb. In other words, that layer is guaranteed to be immutable and this allows other derived images to reuse it and build their own deltas on top of it, without duplicating(creating a copy) it.

These can be explored on the host system as well. Here is the ~10MB delta that I artificially added with dd when I built the child image.

sudo du -sh "/var/lib/docker/overlay2/5ca936630339967105c28d4d8c9669d99f0f449a307c43c09d60f6341cf56271/diff"
9.8M    /var/lib/docker/overlay2/5ca936630339967105c28d4d8c9669d99f0f449a307c43c09d60f6341cf56271/diff
like image 53
Neo Anderson Avatar answered Sep 02 '25 06:09

Neo Anderson