Last active
December 15, 2020 18:22
-
-
Save mycargus/cadbf28c9c6f6c5b1f47b6b016a6baa6 to your computer and use it in GitHub Desktop.
useful docker commands
This file contains hidden or 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
# 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