liveBook · Manning

Layer size

As explained in Learn Docker in a Month of Lunches, Second Edition

Layer size is the amount of storage represented by an individual container-image layer.

Layer size is the amount of storage represented by an individual container-image layer. Keeping layers small makes Docker images easier and faster to build, push, pull, and use. section 17.3 section 17.5

Reduce layer size with practical Dockerfile changes:

  • Install only required packages, combine related APT commands, use --no-install-recommends, and remove APT lists in the same optimized step. section 17.3
  • Avoid expanding an entire archive when one file is needed; extract the required path and delete the downloaded archive within the same step. section 17.3
  • Use multi-stage Dockerfiles when build or download steps temporarily require substantial disk space. section 17.3
  • Monitor image build, push, and pull times, then iterate on Dockerfile organization and layer contents. section 17.5

Compare optimized images

Build two versions of the same image, such as diamol/ch17-socat:2e-v1 and diamol/ch17-socat:2e-v2, then run docker image ls "diamol/ch17-socat*". The optimized installation reduces image size by 20% in the exercise. section 17.3

# switch to the exercise folder:
cd ch17/exercises/socat
# the v1 image installs packages using standard commands:
docker image build -t diamol/ch17-socat:2e-v1 .
# v2 installs the same packages but using optimization tweaks:
docker image build -t diamol/ch17-socat:2e-v2 -f Dockerfile.v2 .
# check the image sizes:
docker image ls "diamol/ch17-socat*"

Run the supplied build commands to compare image sizes before and after installation optimizations. section 17.3

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Explains practical Dockerfile reductions through APT cleanup, selective archive extraction, and multi-stage builds.
Examples
Compares naive and optimized APT installs, selective archive extraction, and Docker image sizes using docker image ls.
What it assumes
Assumes Docker image-building and Dockerfile basics while demonstrating optimization commands directly.
What you can do after
Apply via Dockerfile changes and verify improvements by comparing built image sizes and workflow timings.
How it connects
Connects layer size with Dockerfiles, container images, multi-stage builds, image caching, and image lifecycle operations.

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.