Skip to content

Instantly share code, notes, and snippets.

@mrclay
Last active September 16, 2022 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrclay/1077288029d7f6ff7fdc17fa3178b4a2 to your computer and use it in GitHub Desktop.
Save mrclay/1077288029d7f6ff7fdc17fa3178b4a2 to your computer and use it in GitHub Desktop.
docker compose logs -f but auto restart if all containers stop and come back up
#!/bin/bash
# Like docker compose logs -f but it waits for containers to come back up
# if they stop or haven't been started.
#
# USAGE: dclogs [SERVICE]
#
function dclogs {
# If in ddev project, use ddev's full config file
if [ -d ".ddev" ]; then
local COMPOSE_ARGS="-f=.ddev/.ddev-docker-compose-full.yaml"
else
local COMPOSE_ARGS=""
fi
while :
do
# Intentionally not quoting $COMPOSE_ARGS and $1
docker compose $COMPOSE_ARGS logs -f --tail=3 $1
[ $? -eq 0 ] || break
echo "Awaiting a container to restart"
sleep 5
# wait around for startup
while :
do
# Intentionally not quoting $COMPOSE_ARGS
local DCTOP=$(docker compose $COMPOSE_ARGS top 2> /dev/null)
if [[ "$DCTOP" =~ "PID" ]]; then
# we're in business
break
else
sleep 2
fi
done
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment