Skip to content

Instantly share code, notes, and snippets.

@mycargus
Last active December 15, 2020 18:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mycargus/cadbf28c9c6f6c5b1f47b6b016a6baa6 to your computer and use it in GitHub Desktop.
Save mycargus/cadbf28c9c6f6c5b1f47b6b016a6baa6 to your computer and use it in GitHub Desktop.
useful docker commands
# Purpose: reset docker environment
# Argument (optional): all
#
# "docker-reset" will kill and remove all containers (except dinghy-http-proxy),
# dangling images, and volumes. It won't remove networks.
#
# "docker-reset all" will do everything in 'docker-reset' and remove all
# networks, too.
#
# Prerequisite: you'll need to install docker-clean: brew install docker-clean
#
# To use dinghy-http-proxy with docker for mac, follow these instructions:
# https://github.com/codekitchen/dinghy-http-proxy#using-outside-of-dinghy
# Kill and remove all docker containers (except for dinghy-http-proxy)
function docker-kill-all-except-dinghy-http-proxy() {
PROXY=$(docker ps | grep dinghy-http-proxy | awk '{print $1}')
echo "Killing docker containers..."
docker kill $(docker ps -q | grep -v $PROXY) &> /dev/null
echo "Removing docker containers..."
docker rm $(docker ps -a --filter "status=exited" -q) &> /dev/null
}
# Kill and remove all docker containers
function docker-kill-all() {
echo "Killing and removing docker containers..."
docker rm -fv $(docker ps -a -q) &> /dev/null
}
# Stop and remove all docker containers (except for dinghy-http-proxy)
function docker-stop-all-except-dinghy-http-proxy() {
PROXY=$(docker ps | grep dinghy-http-proxy | awk '{print $1}')
echo "Stopping docker containers..."
docker stop $(docker ps -q | grep -v $PROXY) &> /dev/null
echo "Removing docker containers..."
docker rm $(docker ps -a --filter "status=exited" -q) &> /dev/null
}
function docker-reset() {
run_all=$1
if [[ "${run_all}" == "all" ]]; then
docker-kill-all-except-dinghy-http-proxy
echo "Running docker-clean ..."
docker-clean --containers --images --volumes --networks --log
else
docker-kill-all-except-dinghy-http-proxy
echo "Running docker-clean ..."
docker-clean --containers --images --volumes --log
fi
}
# Purpose: Delete all containers and images related to the provided tag name
# Example: `docker-nuke selenium node` will delete all containers and images related to "selenium" and "node"
function docker-nuke() {
args=("$@")
docker ps -a | grep ${args[0]} | cut -d ' ' -f 1 | while read ID; do docker rm $ID; done;
docker images | grep ${args[0]} | tr -s " " | cut -d ' ' -f 3 | while read ID; do docker rmi $ID; done
}
# Purpose: Remove all images
function docker-rmi-all() {
docker rmi -f $(docker images -q)
}
# Purpose: slap virtualbox
# Example: Using virtualbox as the VM for dinghy's docker host sometimes causes image download
# latency. Run this function in a separate shell window when that happens.
function slap-virtualbox() {
while true; do dinghy ssh echo "ಠ_ಠ slap"; done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment