Skip to content

Instantly share code, notes, and snippets.

@rithask
Last active December 30, 2023 06:54
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 rithask/51d263f5d10ffa25b1794fc27fc423ad to your computer and use it in GitHub Desktop.
Save rithask/51d263f5d10ffa25b1794fc27fc423ad to your computer and use it in GitHub Desktop.
Docker aliases
#!/bin/bash
read -r -d '' aliases << EOM
alias dps="docker ps"
alias dcu="docker compose up -d"
alias dcd="docker compose down"
alias dcf="docker compose up -d --force-recreate"
alias dcl="docker compose logs -f"
alias dcp="docker compose pull"
alias dsp="docker system prune --all"
alias dstats='docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"'
alias dkps="docker ps --format '{{.ID}} ~ {{.Names}} ~ {{.Status}} ~ {{.Image}}'"
EOM
# Check the SHELL environment variable
current_shell=$SHELL
# if the shell is bash or zsh, then add the aliases to the .bashrc or .zshrc file
if [ "$current_shell" = "/bin/bash" ]; then
echo "Your current shell is bash"
file=~/.bashrc
elif [ "$current_shell" = "/bin/zsh" ]; then
echo "Your current shell is zsh"
file=~/.zshrc
else
echo "Your current shell is not bash or zsh"
exit 1
fi
if ! grep -q "Docker aliases - rithask" "$file"; then
echo "Adding aliases to $file"
echo "" >>"$file"
echo "# Docker aliases - rithask" >>"$file"
echo "$aliases" >>"$file"
echo "Done"
echo -n "Do you want to reload your shell? (y/n) [default: y] "
read -r reload_shell
if [ "$reload_shell" = "y" ] || [ -z "$reload_shell" ]; then
exec "$current_shell"
fi
else
echo "Aliases already added to $file"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment