Skip to content

Instantly share code, notes, and snippets.

@diyfr
Created June 7, 2019 06:55
Show Gist options
  • Save diyfr/81d144def9d423c801735efb1da07260 to your computer and use it in GitHub Desktop.
Save diyfr/81d144def9d423c801735efb1da07260 to your computer and use it in GitHub Desktop.
Clean container logs with docker image (if you don't have permission...)
#!/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)
PATH_DOCKER="/home/docker"
log=${log#$PATH_DOCKER}
docker run -it --rm -v $PATH_DOCKER:/data --name mycontainer myalpineimage -c "truncate -s 0 /data${log}"
FROM alpine:latest
RUN mkdir /data
VOLUME ["/data"]
ENTRYPOINT ["/bin/sh"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment