17

I want to get rid of huge container log files on my docker env.

I have problem finding them when running native Docker on a Mac. I am not using docker-machine (virtualbox) thing. My docker version is 1.13.1.

When I do

docker inspect <container-name>

I see there is

"LogPath": "/var/lib/docker/containers/<container-id>/<container-id>-json.log

But there is not even directory /var/lib/docker on my mac (host).

I have also looked in

~/Library/Containers/com.docker.docker/

but didn't find any container specific loggings there.

I could use tail, but it is not that convenient always to me.

So the question is, how can I clear the log files of my containers on my native Docker Mac environment.

Axel Janduneaz
  • 175
  • 1
  • 6

5 Answers5

16

Docker daemon runs in a separate VM, so in order to clear logs you should do the following steps:


First, you can find the log path inside the VM, with:

docker inspect --format='{{.LogPath}}' NAME|ID


You can connect to the VM with screen

screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty


Here you can simply use output redirection to clear the log

> /var/lib/docker/containers/CONTAINER_ID/CONTAINER_ID-json.log


And finally you can detach the screen with hitting Control+a d

Dániel Szabó
  • 374
  • 2
  • 5
  • awesome, I was doing a Reset > Remove all data, because one of my container overlays kept growing like crazy, but this is much better! Knew it had something to do with log files. Great find! – TrieuNomad Apr 26 '18 at 17:14
  • 2
    in my case, the vm path is ` ~/Library/Containers/com.docker.docker/Data/vms/0/tty` – Neekey Sep 13 '19 at 01:47
  • you can also use `truncate -s 0 LOG_PATH` to clear the log – Neekey Sep 13 '19 at 01:50
  • 1
    As of 3.2.2, `tty` file doesn't exist there anymore (neither the location from the answer or the comment update). – Grant Birchmeier Apr 16 '21 at 20:42
16

I added the following to my bash_profile. it gets the logpath for the docker container, opens a screen to the docker machine and deletes the logfile.

clearDockerLog(){
dockerLogFile=$(docker inspect $1 | grep -G '\"LogPath\": \"*\"' | sed -e 's/.*\"LogPath\": \"//g' | sed -e 's/\",//g')
rmCommand="rm $dockerLogFile"
screen -d -m -S dockerlogdelete ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty
screen -S dockerlogdelete -p 0 -X stuff $"$rmCommand"
screen -S dockerlogdelete -p 0 -X stuff $'\n'
screen -S dockerlogdelete -X quit
}

use as follows:

clearDockerLog <container_name>
xyno
  • 161
  • 1
  • 2
15

This will remove all your docker logs in macOS.

echo "rm /var/lib/docker/containers/*/*.log" | nc -U -w 0 ~/Library/Containers/com.docker.docker/Data/debug-shell.sock
Henry Avila
  • 151
  • 1
  • 3
7

This is the only solution that worked for macOS 10.14

docker run -it --rm --privileged --pid=host NAME nsenter -t 1 -m -u -n -i -- sh -c 'truncate -s0 /var/lib/docker/containers/*/*-json.log'

Replace NAME with your container name

Hope this helps

Raj Nandan Sharma
  • 3,694
  • 3
  • 32
  • 42
0

This worked for me, at least from the commandline: screen $(cat ~/Library/Containers/com.docker.docker/Data/vms/0/tty)

This might work better with the script if the above doesn't: screen /dev/ttys000

gist with more things to try

Kevin McFadden
  • 129
  • 1
  • 3