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
