Docker – How to analyze a container's disk usage?

I’ve been searching for a while, but couldn’t find a way to analyze the disk usage of a Docker container / volume.

I can see that Docker takes 12GB of my filesystem:

  • How to get the URL of Swarm agent load balancer in Azure container service
  • docker extra_host parameter expects a dictionary value for hostname, how can I use a variable?
  • Raspberry PI docker container not starting
  • Is it possible to import OSM data from inside psql?
  • Fig up: Cannot find module - docker run works
  • Running docker with pgadmin on mac getting Error: Unable to initialize GTK+, is DISPLAY set properly?
  • 2.7G    /var/lib/docker/vfs/dir
    2.7G    /var/lib/docker/vfs
    2.8G    /var/lib/docker/devicemapper/mnt
    6.3G    /var/lib/docker/devicemapper/devicemapper
    9.1G    /var/lib/docker/devicemapper
    12G     /var/lib/docker
    

    But, how do I know how this is distributed over the containers?

    I tried to attach to the containers by running (the new v1.3 command)

    docker exec -it <container_name> bash
    

    and then running ‘df -h’ to analyze the disk usage. It seems to be working, but not with containers that use ‘volumes-from’.

    For example, I use a data-only container for MongoDB, called ‘mongo-data’.

    When I run ‘docker run -it –volumes-from mongo-data busybox’, and then ‘df -h’ inside the container, It says that the filesystem mounted on ‘/data/db’ (my ‘mongo-data’ data-only container) uses 11.3G, but when I do ‘du -h /data/db’, it says that it uses only 2.1G.

    So, how do I analyze a container/volume disk usage? Or, in my case, how do I find out the ‘mongo-data’ container size?

    Many thanks,

  • Installing Docker.io on Ubuntu 14.04LTS
  • Trying docker in docker getting TCP connection refused error
  • node.JS application in docker container
  • iptables redirect to different host/tproxy in docker container
  • Debug set always restart container docker
  • Docker run error: “Thin Pool has free data blocks which is less than minimum required”
  • 8 Solutions collect form web for “Docker – How to analyze a container's disk usage?”

    To see the file size of your containers, you can use the -s argument of docker ps:

    docker ps -s
    

    After 1.13.0, Docker includes a new command docker system df to show docker disk usage.

    $ docker system df
    TYPE            TOTAL        ACTIVE     SIZE        RECLAIMABLE
    Images          5            1          2.777 GB    2.647 GB (95%)
    Containers      1            1          0 B         0B
    Local Volumes   4            1          3.207 GB    2.261 (70%)
    

    To show more detailed information on space usage

    $ docker system df -v
    

    Posting this as an answer because my comments above got hidden:

    List the size of a container:

    du -d 2 -h /var/lib/docker/devicemapper | grep `docker inspect -f "{{.Id}}" <container_name>`
    

    List the sizes of a container’s volumes:

    docker inspect -f "{{.Volumes}}" <container_name> | sed 's/map\[//' | sed 's/]//' | tr ' ' '\n' | sed 's/.*://' | xargs sudo du -d 1 -h
    

    Edit:
    List all running containers’ sizes and volumes:

    for d in `docker ps -q`; do
        d_name=`docker inspect -f {{.Name}} $d`
        echo "========================================================="
        echo "$d_name ($d) container size:"
        sudo du -d 2 -h /var/lib/docker/devicemapper | grep `docker inspect -f "{{.Id}}" $d`
        echo "$d_name ($d) volumes:"
        docker inspect -f "{{.Volumes}}" $d | sed 's/map\[//' | sed 's/]//' | tr ' ' '\n' | sed 's/.*://' | xargs sudo du -d 1 -h
    done
    

    NOTE: Change ‘devicemapper’ according to your Docker filesystem (e.g ‘aufs’)

    The volume part did not work anymore so if anyone is insterested I just change the above script a little bit:

    for d in `docker ps | awk '{print $1}' | tail -n +2`; do
        d_name=`docker inspect -f {{.Name}} $d`
        echo "========================================================="
        echo "$d_name ($d) container size:"
        sudo du -d 2 -h /var/lib/docker/aufs | grep `docker inspect -f "{{.Id}}" $d`
        echo "$d_name ($d) volumes:"
        for mount in `docker inspect -f "{{range .Mounts}} {{.Source}}:{{.Destination}}                                                                                                                                                      
        {{end}}" $d`; do
            size=`echo $mount | cut -d':' -f1 | sudo xargs du -d 0 -h`
            mnt=`echo $mount | cut -d':' -f2`
            echo "$size mounted on $mnt"
        done
    done
    

    (this answer is not useful, but leaving it here since some of the comments may be)

    docker images will show the ‘virtual size’, i.e. how much in total including all the lower layers. So some double-counting if you have containers that share the same base image.

    documentation

    If you want to reduce the size of many-layered images, I can recommend Jason Wilder’s docker-squash utility. Get it from GitHub here: https://github.com/jwilder/docker-squash

    Keep in mind that docker ps --size may be an expensive command, taking more than a few minutes to complete. The same applies to container list API requests with size=1. It’s better not to run it too often.

    Take a look at alternatives we compiled, including the du -hs option for the docker persistent volume directory.

    You can use

    docker history IMAGE_ID
    

    to see how the image size is ditributed between its various sub-components.

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