Created
July 19, 2022 18:33
-
-
Save eliasdorigoni/068f3c2fac26b91a6410eefee0812d81 to your computer and use it in GitHub Desktop.
Bash function to restart Docker (MacOS only)
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
#!/usr/bin/env bash | |
# This is meant to be appended to .bash_aliases or .zsh_aliases | |
# | |
# The `docker-restart` command allows the user to restart Docker and wait for it | |
# to be ready before executing the next command. | |
function docker-is-ready() { | |
timestamp=$(date +%s) | |
while [[ -z "$(! docker stats --no-stream 2> /dev/null)" ]] | |
do | |
sleep 1 | |
now=$(date +%s) | |
echo -n -e "\r"$(($now - $timestamp))"s" | |
# In my system, over 80 seconds means problems somewhere else. | |
done | |
} | |
function docker-restart() { | |
echo "[1/3] Stopping Docker..." | |
osascript -e 'quit app "Docker"' | |
sleep 5 | |
echo "[2/3] Starting Docker..." | |
open -a Docker | |
sleep 1 | |
echo "[3/3] Waiting for ready state..." | |
docker-is-ready | |
echo "\nDone." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment