Clear a docker container's logfile
Jul 17, 2017
Docker does not currently provide a method of cleaning/truncating a container’s logs, however you can use this simple script to easily clear them (individually). The script below also supports containers started with both docker and docker-compose (thanks Edgard).
Save the following to something like /usr/local/bin/docker-clear-log
.
#!/bin/bash -e
CONTAINER=$1
if [[ -z $CONTAINER ]]; then
echo "No container specified"
exit 1
fi
if [[ "$(docker ps -aq -f name=^/"$CONTAINER"$ 2> /dev/null)" == "" ]]; then
CONTAINER="$(docker-compose ps "$CONTAINER" 2> /dev/null | awk 'END {print $1}')"
if [[ -z $CONTAINER ]]; then
echo "Container \"$1\" does not exist, exiting."
exit 1
fi
fi
LOG=$(docker inspect -f '{{.LogPath}}' "$CONTAINER" 2> /dev/null)
truncate -s 0 "$LOG"
Don’t forget to make the script executable: chmod 755 /usr/local/bin/docker-clear-log
Then just run docker-clear-log <container>
.