How to clear the logs properly for a Docker container?

前端 未结 15 2492
谎友^
谎友^ 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:53

    Here is a cross platform solution to clearing docker container logs:

    docker run --rm -v /var/lib/docker:/var/lib/docker alpine sh -c "echo '' > $(docker inspect --format='{{.LogPath}}' CONTAINER_NAME)"
    

    Paste this into your terminal and change CONTAINER_NAME to desired container name or id.

    0 讨论(0)
  • 2020-12-12 09:56

    Use:

    truncate -s 0 /var/lib/docker/containers/*/*-json.log
    

    You may need sudo

    sudo sh -c "truncate -s 0 /var/lib/docker/containers/*/*-json.log"
    

    ref. Jeff S. How to clear the logs properly for a Docker container?

    Reference: Truncating a file while it's being used (Linux)

    0 讨论(0)
  • 2020-12-12 09:57

    On Docker for Windows and Mac, and probably others too, it is possible to use the tail option. For example:

    docker logs -f --tail 100
    

    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)

    0 讨论(0)
提交回复
热议问题