Skip to content

Instantly share code, notes, and snippets.

@vincenzopalazzo
Created April 4, 2020 16:11
Show Gist options
  • Save vincenzopalazzo/2de7fae9350473d25902876e1e5db04e to your computer and use it in GitHub Desktop.
Save vincenzopalazzo/2de7fae9350473d25902876e1e5db04e to your computer and use it in GitHub Desktop.
UNIX script to clean docker after traning or test IMPORTANT this script clean all container and images
#!/bin/bash
#linux Script to clean docker containers and images
$RUN_WITH_SUDO=1 #Insert 0if you have docker with user permission
if [! RUN_WITH_SUDO]
then
for NAME in $(sudo docker ps -a)
do
sudo docker stop $NAME && sudo docker rm $NAME
done
for IMAGE_VERSION in $(sudo docker images)
do
sudo docker rmi --force $IMAGE_VERSION
done
else
for NAME in $(docker ps -a)
do
docker stop $NAME && docker rm $NAME
done
for IMAGE_VERSION in $(docker images)
do
docker rmi --force $IMAGE_VERSION
done
fi
echo 'Script end'
sudo docker ps -a
sudo docker images
@vincenzopalazzo
Copy link
Author

The commands return this response patter

sudo docker ps -a: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
sudo docker images: REPOSITORY TAG IMAGE ID CREATED SIZE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment