4

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?

Neo Anderson
  • 5,957
  • 2
  • 12
  • 29
amos-baron
  • 87
  • 5

1 Answers1

5

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
Neo Anderson
  • 5,957
  • 2
  • 12
  • 29
  • Nicely explained. In essence,"UpperDir": "/var/lib/docker/overlay2/0654e44ddf13ebd2a0feb2ac6261e62f6c83a8be1937a71c544f69eb6208d93b/diff"from "apline" forms the "Lowerdir" for alpine-plus-ten-mb. Is there some picture for this ? – Nag Aug 30 '20 at 15:00
  • @Nag, [Here](https://docs.docker.com/storage/storagedriver/overlayfs-driver/#how-the-overlay-driver-works), plus a practical hands-on [here](https://ops.tips/notes/practical-look-into-overlayfs/). UFS exist since 1995 ;). When I first tried to understand how these things are working, I haven't found the info I was looking for on the Docker related forums. – Neo Anderson Aug 30 '20 at 15:13