The first line gets the log file path, similar to the accepted answer.
The second line uses nsenter that allows you to run commands in the xhyve VM that servers as the host for all the docker containers under Docker4Mac. The command we run is the familiar truncate -s0 $LOGPATH from non-Mac answers.
If you're using docker-compose, the first line becomes:
local LOGPATH=$(docker inspect --format='{{.LogPath}}' $(docker-compose ps -q <service>))
and <service> is the service name from your docker-compose.yml file.
On Docker for Windows and Mac, and probably others too, it is possible to use the tail option. For example:
This way, only the last 100 lines are shown, and you don't have first to scroll through 1M lines...
(And thus, deleting the log is probably unnecessary)
You can set up logrotate to clear the logs periodically.
Example file in /etc/logrotate.d/docker-logs
On my Ubuntu servers even as sudo I would get
Cannot open ‘/var/lib/docker/containers/*/*-json.log’ for writing: No such file or directory
But combing the docker inspect and truncate answers worked :
Docker for Mac users, here is the solution:
Find log file path by:
$ docker inspect | grep log
SSH into the docker machine( suppose the name is
default
, if not, rundocker-machine ls
to find out):$ docker-machine ssh default
Change to root user(reference):
$ sudo -i
Delete the log file content:
$ echo "" > log_file_path_from_step1
Use:
You may need sudo
ref. Jeff S. How to clear the logs properly for a Docker container?
Reference: Truncating a file while it's being used (Linux)
Docker4Mac, a 2018 solution:
The first line gets the log file path, similar to the accepted answer.
The second line uses
nsenter
that allows you to run commands in thexhyve
VM that servers as the host for all the docker containers under Docker4Mac. The command we run is the familiartruncate -s0 $LOGPATH
from non-Mac answers.If you're using
docker-compose
, the first line becomes:and
<service>
is the service name from yourdocker-compose.yml
file.Thanks to https://github.com/justincormack/nsenter1 for the
nsenter
trick.