Docker image larger than its filesystem

I have a docker image that I built from several layers and a debian:jessie base image that I’m working on making smaller. I added a cleanup layer than does apt-get remove a bunch of packages and deletes files from the filesystem. When I run the resulting image and do du -h inside it I can see it’s about 1.3GB. However, when I look at the image size in docker images, it’s more than 2.2GB, which is the size of the image before the cleanup layer. Why is the final image not getting smaller?

  • Making the Docker Registry HA
  • Apache httpd not behaving properly when running docker image
  • script if version in package.json has changed
  • Why does apt-get exit without an error during Dockerfile build?
  • how to link container in docker?
  • How do I push and pull from docker hub in Jenkins declarative pipeline
  • How to install old version (1.8.3) of docker on OS-X?
  • How to launch a Docker container that i've got from another person?
  • two docker machine, same web-app, two different IP, but needs to be sync
  • assign port docker issue
  • Docker custom dns resolve among containers
  • Bitbucket Pipelines - mvn clean install not working with JavaFX
  • One Solution collect form web for “Docker image larger than its filesystem”

    You can’t have a cleanup layer – the Union File System doesn’t work like that. If files exist in layer 1, and you delete them in layer 2, all Docker does when you run the container is hide the deleted files. Once files are saved into an image layer, they can’t be removed from that layer.

    You can try using the Docker squash tool to reduce the final image size, or rework your Dockerfiles in line with Titouan Freville’s comment – do all the APT commands in one RUN statement (update, install remove). That way when the image layer is saved, the files have already been cleaned up.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.