Categories
DevOps

How to squash Docker image

Squash Docker image to remove unnecessary layers in case you do not have a need to keep them.

Install docker-squash.

$ pip install docker-squash

Display help information.

$ docker-squash --help
usage: docker-squash [-h] [-v] [--version] [-d] [-f FROM_LAYER] [-t TAG] [-c] [--tmp-dir TMP_DIR] [--output-path OUTPUT_PATH] image

Docker layer squashing tool

positional arguments:
  image                 Image to be squashed

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         Verbose output
  --version             Show version and exit
  -d, --development     Does not clean up after failure for easier debugging
  -f FROM_LAYER, --from-layer FROM_LAYER
                        ID of the layer or image ID or image name. If not specified will squash all layers in the image
  -t TAG, --tag TAG     Specify the tag to be used for the new image. If not specified no tag will be applied
  -c, --cleanup         Remove source image from Docker after squashing
  --tmp-dir TMP_DIR     Temporary directory to be created and used
  --output-path OUTPUT_PATH
                        Path where the image should be stored after squashing. If not provided, image will be loaded into Docker
                        daemon

Inspect our sample DokuWiki image that will be used in this example.

$ docker image ls dokuwiki:latest
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
dokuwiki     latest    81fa953242ea   32 hours ago   51.2MB
$ docker history dokuwiki:latest
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
81fa953242ea   32 hours ago   /bin/sh -c #(nop)  CMD ["unitd" "--no-daemon…   0B        
aad5ffa6cd31   32 hours ago   /bin/sh -c #(nop) WORKDIR /opt/dokuwiki         0B        
a98a1806e8ec   32 hours ago   /bin/sh -c #(nop)  USER dokuwiki                0B        
cbe7109019c4   32 hours ago   /bin/sh -c #(nop)  EXPOSE 8080                  0B        
e58156317f72   32 hours ago   |2 GID=5000 UID=5000 /bin/sh -c chown -R dok…   672B      
162700362dc1   32 hours ago   /bin/sh -c #(nop) COPY file:25cd54fde2f41817…   672B      
86190f8bf11d   32 hours ago   /bin/sh -c #(nop) COPY file:c44cf51c56219ee0…   366B      
e675b4d5cd31   32 hours ago   /bin/sh -c #(nop) COPY file:8f17ce87a068c7b4…   172B      
311cba329257   32 hours ago   /bin/sh -c #(nop) COPY file:a18b36cd71c66d32…   327B      
5f266a479729   32 hours ago   /bin/sh -c #(nop) COPY file:47074ff4abd6297f…   228B      
d8239e54107a   32 hours ago   |2 GID=5000 UID=5000 /bin/sh -c apk add --no…   16.9MB    
9bda04b7bdec   32 hours ago   |2 GID=5000 UID=5000 /bin/sh -c apk add --no…   14.3MB    
feb2d2025277   32 hours ago   /bin/sh -c #(nop) COPY dir:fb9054dc9d2ccd9af…   14.3MB    
92a7fd2f9644   32 hours ago   /bin/sh -c #(nop)  LABEL application=Dokuwiki   0B        
02390e6d72a2   32 hours ago   /bin/sh -c #(nop)  LABEL build_date=            0B        
0403cc96ee02   32 hours ago   /bin/sh -c #(nop)  LABEL maintainer=Milosz G…   0B        
ab011b78698c   32 hours ago   /bin/sh -c #(nop)  ARG BUILD_DATE               0B        
b106775f33c9   32 hours ago   /bin/sh -c #(nop)  ARG GID=5000                 0B        
48a608d4aec2   32 hours ago   /bin/sh -c #(nop)  ARG UID=5000                 0B        
14119a10abf4   13 days ago    /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B        
<missing>      13 days ago    /bin/sh -c #(nop) ADD file:aad4290d27580cc1a…   5.6MB 

Squash all layers and tag the new image.

$ docker-squash --tag dokuwiki:squashed dokuwiki:latest
2021-09-10 08:37:47,291 root         INFO     docker-squash version 1.0.8, Docker 75249d8, API 1.41...
2021-09-10 08:37:47,291 root         INFO     Using v2 image format
2021-09-10 08:37:47,297 root         INFO     Old image has 21 layers
2021-09-10 08:37:47,298 root         INFO     Checking if squashing is necessary...
2021-09-10 08:37:47,298 root         INFO     Attempting to squash last 21 layers...
2021-09-10 08:37:47,298 root         INFO     Saving image sha256:81fa953242eac0c98f35d5400e5921be0d83981e0280b37d09fcfbc9145d1a58 to /tmp/docker-squash-6e276eur/old directory...
2021-09-10 08:37:48,160 root         INFO     Image saved!
2021-09-10 08:37:48,161 root         INFO     Squashing image 'dokuwiki:latest'...
2021-09-10 08:37:48,161 root         INFO     Starting squashing...
2021-09-10 08:37:48,161 root         INFO     Squashing file '/tmp/docker-squash-6e276eur/old/70a2035b0eef207d0c40d3b06b3e67f69a97a52b67398b31262a39bbe4b30043/layer.tar'...
2021-09-10 08:37:48,163 root         INFO     Squashing file '/tmp/docker-squash-6e276eur/old/eabb7e8485249875a46a6e310a0a8efaae9481b0749648c33167d7378df9ad62/layer.tar'...
2021-09-10 08:37:48,163 root         INFO     Squashing file '/tmp/docker-squash-6e276eur/old/13e58f6558c933cab47f2453763a86db042fe1fe39091d5da303f46abfbe1818/layer.tar'...
2021-09-10 08:37:48,164 root         INFO     Squashing file '/tmp/docker-squash-6e276eur/old/cfebac32493419e94a16ae0980c5319bcf6b1279c26d095017ee38b6dbad9cd9/layer.tar'...
2021-09-10 08:37:48,164 root         INFO     Squashing file '/tmp/docker-squash-6e276eur/old/e23937289f7323fbbd4c0d3a49a83e07caa1e19ab87e0a9cc601b6095767ae39/layer.tar'...
2021-09-10 08:37:48,165 root         INFO     Squashing file '/tmp/docker-squash-6e276eur/old/d67fd73b1b4dabf8e354855661bdfd111e8db71d197750cbc57c4f6a8ee3b161/layer.tar'...
2021-09-10 08:37:48,165 root         INFO     Squashing file '/tmp/docker-squash-6e276eur/old/3bb511c620d893a55d29914ca10e0d2b00a7fac7914d4fcf6353f37c440d421a/layer.tar'...
2021-09-10 08:37:48,225 root         INFO     Squashing file '/tmp/docker-squash-6e276eur/old/eeb4ab19d86bc7fdaf6959f899da2d8d4a0c1570e8dd062b22a503344cb61e2c/layer.tar'...
2021-09-10 08:37:49,086 root         INFO     Squashing file '/tmp/docker-squash-6e276eur/old/1951918a94180fd1570e050a251a4a4e61883fb8e7d35cdd2c62aa0a6bf35f9b/layer.tar'...
2021-09-10 08:37:49,456 root         INFO     Squashing file '/tmp/docker-squash-6e276eur/old/5dee72d2797c533813effb8e9de26377300d29f418ebb06bfe814f950a7aa02b/layer.tar'...
2021-09-10 08:37:49,571 root         INFO     Squashing finished!
2021-09-10 08:37:49,702 root         INFO     Original image size: 57.36 MB
2021-09-10 08:37:49,702 root         INFO     Squashed image size: 39.64 MB
2021-09-10 08:37:49,702 root         INFO     Image size decreased by 30.88 %
2021-09-10 08:37:49,702 root         INFO     New squashed image ID is 3f89c7a43637b1c3e60186252371d6d653ddbcfb03b3c648adde8942050a9c50
2021-09-10 08:37:49,816 root         INFO     Image registered in Docker daemon as dokuwiki:squashed
2021-09-10 08:37:49,823 root         INFO     Done

Inspect squashed image.

$ docker image ls dokuwiki:squashed
REPOSITORY   TAG        IMAGE ID       CREATED          SIZE
dokuwiki     squashed   3f89c7a43637   18 seconds ago   36.8MB
$ docker history dokuwiki:squashed
IMAGE          CREATED          CREATED BY   SIZE      COMMENT
3f89c7a43637   31 seconds ago                36.8MB    

Additional notes

Check out docker-squash source code.