Created
June 7, 2019 06:55
-
-
Save diyfr/81d144def9d423c801735efb1da07260 to your computer and use it in GitHub Desktop.
Clean container logs with docker image (if you don't have permission...)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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