Do docker images share identical layers?

Viewed 609

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?

1 Answers

Short answer: 1.6GB

This is an interesting experiment that 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"
}

The layer d93b/diff is read-only for our child image alpine-plus-ten-mb.
More over, all of these can be explored on the host system. 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
Related