Docker squash behavior

I have a big docker image A, and i create a new Dockerfile

FROM A

RUN rm /big-folder

I’ve tried to build the image with:

  • Can't access env variables in RUN script.sh Dockerfile
  • Debugging Django on Docker on Vagrant with IDE
  • Web console for docker container
  • Where is information from dockerfile located?
  • When to use auto mapped Docker data volume
  • How can I pass secret data to a container
  • docker build --squash -t B:latest .
    

    Shouldn’t the image size reduce by the dimension of /big-folder directory?

    In my actual test both the image A and B maintain the same size of 1,26 GB.

    Am I doing something wrong or haven’t I understood the actual behavior of squash option?

  • How to debug seg fault in docker container?
  • How to connect to local process from docker
  • How should I pass sensitive environment variables to Amazon ECS tasks?
  • Wrong credentials when connecting to Docker MySQL container
  • Cannot pipe docker pid to bash command
  • Rabbitmq cluster setup in Kubernetes
  • One Solution collect form web for “Docker squash behavior”

    Note, this feature is experimental and therefore may change in future releases. Testing was done on version 17.03.

    From my testing, doing a --squash will compress the layers added only in the current image, without changing or merging any FROM image layers.

    $ # squash-a is a sample base image like your image A
    $ cat df.squash-a
    FROM busybox:latest
    ADD alpinetest.tgz /data
    
    $ # squash-b is built off of squash-a, and uses the squash flag when built
    $ cat df.squash-b
    FROM test-squash-a:latest
    RUN rm -rf /data
    
    $ # squash-c merges the dockerfiles for A and B
    $ cat df.squash-c
    FROM busybox:latest
    ADD alpinetest.tgz /data
    RUN rm -rf /data
    
    $ # standard build for A
    $ docker build -f df.squash-a -t test-squash-a:latest .            
    Sending build context to Docker daemon 23.25 MB
    Step 1/2 : FROM busybox:latest
     ---> 7968321274dc
    Step 2/2 : ADD alpinetest.tgz /data
     ---> a417cffcd3d2
    Removing intermediate container f15e9cd57375
    Successfully built a417cffcd3d2
    
    $ # build B with --squash
    $ docker build -f df.squash-b --squash -t test-squash-b:latest .
    Sending build context to Docker daemon 23.25 MB
    Step 1/2 : FROM test-squash-a:latest
     ---> a417cffcd3d2
    Step 2/2 : RUN rm -rf /data
     ---> Running in 961044b8ee10
     ---> f5939d65a51a
    Removing intermediate container 961044b8ee10
    Successfully built f5939d65a51a
    
    $ # build C with --squash
    $ docker build -f df.squash-c --squash -t test-squash-c:latest .   
    Sending build context to Docker daemon 23.25 MB
    Step 1/3 : FROM busybox:latest
     ---> 7968321274dc
    Step 2/3 : ADD alpinetest.tgz /data
     ---> Using cache
     ---> a417cffcd3d2
    Step 3/3 : RUN rm -rf /data
     ---> Using cache
     ---> f5939d65a51a
    Successfully built f5939d65a51a
    
    $ # comparing images, A is large as expected, B didn't shrink, but C did
    $ docker images | grep squash | sort
    test-squash-a                           latest              a417cffcd3d2        6 minutes ago       68 MB
    test-squash-b                           latest              74e05f2130be        2 minutes ago       68 MB
    test-squash-c                           latest              7562dac62351        2 minutes ago       1.11 MB
    
    $ # start inspecting the layers with the busybox base
    $ docker inspect -f '{{ .RootFS.Layers }}' busybox:latest
    [sha256:38ac8d0f5bb30c8b742ad97a328b77870afaec92b33faf7e121161bc78a3fec8]
    
    $ # layers in A adds one as expected
    $ docker inspect -f '{{ .RootFS.Layers }}' test-squash-a
    [sha256:38ac8d0f5bb30c8b742ad97a328b77870afaec92b33faf7e121161bc78a3fec8
     sha256:3f4d2549e582599f3059fe3c869d4d353794b9064f8548c3a68629a4a93038d7]
    
    $ # layers in B includes both busybox and A despite being squashed
    $ docker inspect -f '{{ .RootFS.Layers }}' test-squash-b
    [sha256:38ac8d0f5bb30c8b742ad97a328b77870afaec92b33faf7e121161bc78a3fec8
     sha256:3f4d2549e582599f3059fe3c869d4d353794b9064f8548c3a68629a4a93038d7 
     sha256:68e9b1791c54b45c785c04440ba9de6eefd566a09890821296d46aec9609d2de]
    
    $ # layers in C only adds a single layer on top of busybox despite 2 run commands
    $ docker inspect -f '{{ .RootFS.Layers }}' test-squash-c
    [sha256:38ac8d0f5bb30c8b742ad97a328b77870afaec92b33faf7e121161bc78a3fec8 
     sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef]
    
    $ # the history output shows that the squash is over a range of layers
    $ docker history test-squash-c
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    7562dac62351        10 minutes ago                                                      0 B                 merge sha256:f5939d65a51a45b9c30f71a53a878e42be3135a1e0696615aa3b34ed39e5dd99 to sha256:7968321274dc6b6171697c33df7815310468e694ac5be0ec03ff053bb135e768
    <missing>           10 minutes ago      /bin/sh -c rm -rf /data                         0 B                 
    <missing>           13 minutes ago      /bin/sh -c #(nop) ADD file:8993cc512c40cc0...   0 B                 
    <missing>           2 months ago        /bin/sh -c #(nop)  CMD ["sh"]                   0 B                 
    <missing>           2 months ago        /bin/sh -c #(nop) ADD file:707e63805c0be1a...   1.11 MB    
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.