How to clear the logs properly for a Docker container?

前端 未结 15 2474
谎友^
谎友^ 2020-12-12 09:00

I use docker logs [container-name] to see the logs of a specific container.

Is there an elegant way to clear these logs?

15条回答
  •  执笔经年
    2020-12-12 09:35

    Docker4Mac, a 2018 solution:

    LOGPATH=$(docker inspect --format='{{.LogPath}}' )
    docker run -it --rm --privileged --pid=host alpine:latest nsenter -t 1 -m -u -n -i -- truncate -s0 $LOGPATH
    

    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 ))
    

    and is the service name from your docker-compose.yml file.

    Thanks to https://github.com/justincormack/nsenter1 for the nsenter trick.

提交回复
热议问题