This Post assumes you are using a linux OS
Recently I was asked on how to get the size of the logs the containers were producing on a default docker installation without using any log driver.
So first, how to access the path of the docker logs of a container?
With the go formmatters
for docker this is an easy retrieval
1
docker inspect --format='' $CONTAINER_ID
But the inspect also takes multiple arguments so we could as well get all our logs for all our containers, running and stopped
1
docker inspect --format='' $(docker ps -a -q)
So by running the above command we should get a list
of the paths to the containers logs.
You should get an ouput like this:
1
2
3
4
5
6
7
8
/var/lib/docker/containers/ef7339183f954059983864efdf50ebc80d27c0e73fcc1a4ed356faefb230bbe7/ef7339183f954059983864efdf50ebc80d27c0e73fcc1a4ed356faefb230bbe7-json.log
/var/lib/docker/containers/89165e28872eedf7490c00b600a94e552f17234551121567096ec1f500f55ae0/89165e28872eedf7490c00b600a94e552f17234551121567096ec1f500f55ae0-json.log
/var/lib/docker/containers/64f87078e73f1bf85cfcaa0c5619daf6cc1f0bdef0d9b9bdae033160b19dbb50/64f87078e73f1bf85cfcaa0c5619daf6cc1f0bdef0d9b9bdae033160b19dbb50-json.log
/var/lib/docker/containers/0b529938e992553b6cf037678dc021f16677b10ac1daf857f9cef0ccf7ab5b47/0b529938e992553b6cf037678dc021f16677b10ac1daf857f9cef0ccf7ab5b47-json.log
/var/lib/docker/containers/7857572ff6e5d8f1b0cab654e79f2c2c33ce1a477a5250826c2a37e66e404432/7857572ff6e5d8f1b0cab654e79f2c2c33ce1a477a5250826c2a37e66e404432-json.log
/var/lib/docker/containers/68e29b0471d0fbbbc4da2ea3f6b55819d76a3d7a7e17a6a33b6e53827a6e9ee2/68e29b0471d0fbbbc4da2ea3f6b55819d76a3d7a7e17a6a33b6e53827a6e9ee2-json.log
/var/lib/docker/containers/1d0614d4eac3fc45dd499936c1a0d2973d337be4956b39c88365da6f71753600/1d0614d4eac3fc45dd499936c1a0d2973d337be4956b39c88365da6f71753600-json.log
Well if we have the path to the logs than we should be able to pipe
that list to du.
1
docker inspect --format='' $(docker ps -a -q) | sudo xargs -n 1 du -ah
Please do not the need for the sudo
, as the folder /var/lib/docker belongs to root
So this should give us a nice list.
Similar to below
1
2
3
4
5
6
7
8
4,0K /var/lib/docker/containers/ef7339183f954059983864efdf50ebc80d27c0e73fcc1a4ed356faefb230bbe7/ef7339183f954059983864efdf50ebc80d27c0e73fcc1a4ed356faefb230bbe7-json.log
12K /var/lib/docker/containers/89165e28872eedf7490c00b600a94e552f17234551121567096ec1f500f55ae0/89165e28872eedf7490c00b600a94e552f17234551121567096ec1f500f55ae0-json.log
1,8M /var/lib/docker/containers/64f87078e73f1bf85cfcaa0c5619daf6cc1f0bdef0d9b9bdae033160b19dbb50/64f87078e73f1bf85cfcaa0c5619daf6cc1f0bdef0d9b9bdae033160b19dbb50-json.log
124K /var/lib/docker/containers/0b529938e992553b6cf037678dc021f16677b10ac1daf857f9cef0ccf7ab5b47/0b529938e992553b6cf037678dc021f16677b10ac1daf857f9cef0ccf7ab5b47-json.log
148K /var/lib/docker/containers/7857572ff6e5d8f1b0cab654e79f2c2c33ce1a477a5250826c2a37e66e404432/7857572ff6e5d8f1b0cab654e79f2c2c33ce1a477a5250826c2a37e66e404432-json.log
0 /var/lib/docker/containers/68e29b0471d0fbbbc4da2ea3f6b55819d76a3d7a7e17a6a33b6e53827a6e9ee2/68e29b0471d0fbbbc4da2ea3f6b55819d76a3d7a7e17a6a33b6e53827a6e9ee2-json.log
8,0K /var/lib/docker/containers/1d0614d4eac3fc45dd499936c1a0d2973d337be4956b39c88365da6f71753600/1d0614d4eac3fc45dd499936c1a0d2973d337be4956b39c88365da6f71753600-json.log
So now we may also want to sort it, by piping the output again to sort on the h
human readable.
1
2
3
4
5
6
7
0 /var/lib/docker/containers/68e29b0471d0fbbbc4da2ea3f6b55819d76a3d7a7e17a6a33b6e53827a6e9ee2/68e29b0471d0fbbbc4da2ea3f6b55819d76a3d7a7e17a6a33b6e53827a6e9ee2-json.log
4,0K /var/lib/docker/containers/ef7339183f954059983864efdf50ebc80d27c0e73fcc1a4ed356faefb230bbe7/ef7339183f954059983864efdf50ebc80d27c0e73fcc1a4ed356faefb230bbe7-json.log
8,0K /var/lib/docker/containers/1d0614d4eac3fc45dd499936c1a0d2973d337be4956b39c88365da6f71753600/1d0614d4eac3fc45dd499936c1a0d2973d337be4956b39c88365da6f71753600-json.log
12K /var/lib/docker/containers/89165e28872eedf7490c00b600a94e552f17234551121567096ec1f500f55ae0/89165e28872eedf7490c00b600a94e552f17234551121567096ec1f500f55ae0-json.log
124K /var/lib/docker/containers/0b529938e992553b6cf037678dc021f16677b10ac1daf857f9cef0ccf7ab5b47/0b529938e992553b6cf037678dc021f16677b10ac1daf857f9cef0ccf7ab5b47-json.log
148K /var/lib/docker/containers/7857572ff6e5d8f1b0cab654e79f2c2c33ce1a477a5250826c2a37e66e404432/7857572ff6e5d8f1b0cab654e79f2c2c33ce1a477a5250826c2a37e66e404432-json.log
1,8M /var/lib/docker/containers/64f87078e73f1bf85cfcaa0c5619daf6cc1f0bdef0d9b9bdae033160b19dbb50/64f87078e73f1bf85cfcaa0c5619daf6cc1f0bdef0d9b9bdae033160b19dbb50-json.log
And that’s it, a simple way to get to know your containers log sizes before you reach something like this.
1
2
317M /var/lib/docker/containers/a2884d95a6bfd353cc3a31743327a4457f142f2efa5b44ad3f3e63f14f44e4cf/a2884d95a6bfd353cc3a31743327a4457f142f2efa5b44ad3f3e63f14f44e4cf-json.log
If you end up having a lot of huge sized logs eating up all the disk on your servers/local machine, consider using Docker logrotate
features.
You may read on how to accomplish that on docker’s blog post
https://success.docker.com/article/how-to-setup-log-rotation-post-installation