Clearing/Deleting Docker Logs

Solution 1:

First, I'm using docker 1.1.2 for both client and server, this answer may be obsolete for newer versions of docker as docker evolve quickly.

Location of the file

Find your docker directory. On systems that use apt/debian style system, the package installed by the docker repository https://get.docker.com/ubuntu use /var/lib/docker. Chances are that directory is in the same place on other systems (can't confirm).

under containers/**CONTAINER_ID** you'll find infos about the container. In the file **CONTAINER_ID**-json.log in that folder, you'll find a file with all the logs for that container. It may look like a json file, it's not. It's a flow a json structures, one per line, each containing one log line (each line ends by a } and the next one starts with a {, thus it's not a valid json as a whole).

Example location: - /var/lib/docker/containers/05b6053c41a2130afd6fc3b158bda4e605b6053c41a2130afd6fc3b158bda4e6/05b6053c41a2130afd6fc3b158bda4e605b6053c41a2130afd6fc3b158bda4e6-json.log

Editing/Altering that file

I suggest you to use that path to see wether or not it's the reason why you're running out of space, but not to log rotate them.

I would rather make sure the container doesn't log too much lines (by using a CMD in the dockerfile that either redirect the output of you process to a file in a volume or to /dev/null - with logs enabled with configuration - and I would then logrotate the log files with another container)

Solution 2:

Here a simple one-liner to clean container log file:

echo "" > $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)


Solution 3:

if you run sudo lsof | grep '(deleted)', you might find that there are a lot of deleted container log files left open by docker

docker    17511             root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  2879       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  6191       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  8713       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  8726       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  9907       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  9918       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11093       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11096       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11110       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11885       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11890       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11973       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11976       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11983       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 13105       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 16737       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17139       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17516       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17517       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17518       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17519       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17520       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17525       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17574       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17584       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17586       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17637       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17643       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17644       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17659       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17666       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17669       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17680       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17884       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17895       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 21126       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 21474       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 21475       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 21519       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 21721       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 23829       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 23830       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 23833       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 24196       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 24197       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 26821       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 28597       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 28608       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 30305       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 30308       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 30594       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 30595       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)

These are logs that belong to a deleted container. One way to have these closed is by restarting the docker daemon itself. This is not always feasible.

Another way to free this space is to find the relevant file descriptor and to truncate it:

ls -lh /proc/17511/fd/ | grep e49d81fc
lr-x------ 1 root root 64 Sep  8 08:06 25 -> /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)

Where 17511 is the pid of docker and e49d81fc is part of the offending logfilename / container id. Here, the fd is /proc/17511/fd/25, so we truncate it:

cat /dev/null > /proc/17511/fd/25

Solution 4:

I am using Docker version 1.13.0, build 49bf474

If you are using the default logging format json-file, then you can specify the option to rotate your logs.

You can set up the docker daemon to do that or set it up per container.

For instance, log file size of 2 MB and maximum of 5 logfiles:

Docker daemon: Create file /etc/docker/daemon.json:

{
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "2m",
        "max-file": "5",
    }
}

Docker run:

docker run -it --log-driver json-file --log-opt max-size=2m,max-file=5 alpine ash

For details see logging and dockerd configuration


Solution 5:

A late answer, but anyone in the same boat might be better off exploring options for exporting their docker logs to somewhere sensible for managing them. logspout is useful for colelcting logs from all your docker containers and sending them off to somewhere. IF you're only just needing to consider this, then sending them to the host's syslog daemon might be a good approach, and you can use the usual mechanisms for arranging those into files and rotating them from there. For larger multi-host environments, you might look at shipping to an "ELK" stack. i.e. Elasticsearch, Logstash and Kibana.