Last active
September 16, 2022 13:38
-
-
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
This file contains 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 | |
# 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