Created
January 17, 2023 20:14
-
-
Save mycargus/16650e0740c118e4c0a23f342bbf7de8 to your computer and use it in GitHub Desktop.
I use this script to quickly clean my docker environment as needed. See script comments for an explanation. I usually add this script as a file to ~/bin/docker-reset , and add `alias dr='docker-reset'` to my ~/.bashrc
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
#!/usr/bin/env bash | |
# Purpose: reset docker environment | |
# Argument (optional): all | networks | |
# | |
# "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 kill and remove all containers (including | |
# dinghy-http-proxy) and remove all networks, too. | |
# | |
# "docker-reset networks" will remove all networks. | |
# | |
# 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() { | |
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 | |
} | |
flag=$1 | |
if [[ "${flag}" == "all" ]]; then | |
docker-kill-all | |
echo "Running docker-clean ..." | |
docker-clean --containers --images --volumes --networks --log | |
elif [[ "${flag}" == "networks" ]]; then | |
echo "Removing docker networks ..." | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment