Skip to content

Instantly share code, notes, and snippets.

@jlnarvaez
Last active November 5, 2020 09:38
Show Gist options
  • Save jlnarvaez/46c442ca0666480bb41021856e7c09c6 to your computer and use it in GitHub Desktop.
Save jlnarvaez/46c442ca0666480bb41021856e7c09c6 to your computer and use it in GitHub Desktop.
Utils functions for docker
# Delete all containers that contains a name passed by argument
# (If argument not passed, all containers will be deleted)
# Example: dockrm php
dockrm() {
docker ps --filter name="$1" -aq | xargs docker stop | xargs docker rm
}
# Exec container that contains a name passed by argument
# Example: dockexec php bash
# Can execute a command inside container too
# Example dockexec php bash "ls /var/www/html"
dockexec() {
if [ $# -gt 2 ]; then
docker exec -it $(docker ps | grep "$1" | awk '{print $1}') $2 -c "$3"
fi
if [ $# -eq 2 ]; then
docker exec -it $(docker ps | grep "$1" | awk '{print $1}') $2
fi
}
# Stop all containers that contains a name passed by argument
# (If argument not passed, all containers will be stopped)
# Example: dockstop php
dockstop() {
docker ps --filter name="$1" -aq | xargs docker stop
}
# Start all containters that contains a name passed by argument
# (If argument not passed, all containers will be started)
# Example: dockstart php
dockstart() {
docker ps --filter name="$1" -aq | xargs docker start
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment