This simple shell script provides an alternative to Docker's experimental --squash
option for building optimized Docker images by squashing layers.
When Docker builds an image, it utilizes a layered filesystem (AUFS). Each command in the Dockerfile adds a new layer that contains only the changes from the previous layer. This makes builds very fast since only changed files have to be copied.
However, the layered filesystem also results in larger image sizes since each layer contains duplicate files. Squashing combines these layers into a single layer, reducing storage space and often improving runtime performance by decreasing mount points.
The --squash
option provided by Docker squashes all layers into one, reducing image size. However, it also discards all previous instructions like ENV, LABEL, etc.
This script also squashes image layers but keeps ENV, ARG, LABEL and other metadata from the original image.
- Retains user, workdir, and other runtime configurations
- Results in smaller image sizes by combining redundant layers
- Faster lookups at runtime by reducing layer mount points
In other words - smaller images but preserving more of the original image definition!
You can download the docker-squash.sh script from this GitHub repository and save it to your local file system.
Alternatively, you can run these commands with superuser (root) privileges to download the script to the system directory /usr/local/bin using curl:
sudo curl -sL https://github.com/shinsenter/docker-squash/raw/main/docker-squash.sh -o /usr/local/bin/docker-squash.sh
sudo chmod +x /usr/local/bin/docker-squash.sh
docker-squash.sh <source_image> [docker build options]
Where:
source_image
- The original image ID orname:tag
to be squasheddocker build options
- Additional build options like--build-arg
,--label
, etc.
Squashing existing images or Dockerfiles is seamless while providing size and performance benefits.
docker pull php:apache
docker-squash.sh php:apache -t php:apache-squashed
docker images | grep -F 'apache'
# Original image size: 515MB
# Squashed image size: 505MB
You can add your shell script to a build argument named PRESQUASH_SCRIPTS
and execute as root user. It may be useful to clean up the source image before squashing.
docker pull php:apache
docker-squash.sh php:apache \
--build-arg PRESQUASH_SCRIPTS='rm -rf /tmp/* /var/lib/apt/lists/*; apt-get -y autoremove --purge $PHPIZE_DEPS *-dev' \
-t php:apache-squashed
docker images | grep -F 'apache'
# Original image size: 515MB
# Squashed image size: 275MB
docker pull --platform linux/arm64 ubuntu:jammy
docker-squash.sh ubuntu:jammy -t ubuntu:jammy-squashed --platform linux/arm64
docker images | grep -F 'jammy'
# Original image size: 69.2MB
# Squashed image size: 67.2MB
docker-squash.sh /home/my-project/Dockerfile -t my-project:squashed
- Smaller image size
- Faster runtime performance
- Obfuscates build details
- Retains Dockerfile metadata
- Slower rebuild time
- Can inhibit debugging capabilities
- Loss of Docker cache and remote build benefits
If you find this tool helpful and wish to support continued development, I welcome donations or other contributions:
I appreciate monetary contributions via PayPal to help fund development costs. Your support is greatly appreciated.
This is open source software. If you have suggestions, spot issues, or can contribute code changes, please open GitHub issues or pull requests on this project repository. I welcome community input to help expand capabilities and fix problems!
Together we can build better tools for working with Docker. Thank you!
From Vietnam 🇻🇳 with love.