Aller au contenu

« Docker scripts » : différence entre les versions

De Marmits Wiki
Page créée avec « == Docker clean == <syntaxhighlight lang="bash" line> #!/bin/bash # container stop function containersStop { echo "Stop all Containers" runningContainers=$(docker ps -q) if test -n "$runningContainers" then docker stop $runningContainers fi } # containers clean function containersClean { containersStop echo "Remove all Containers" docker container prune -f } # images clean function imagesClean { echo "Remove all Images" images=$(docker images -q)... »
 
 
(3 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
== Docker clean ==
== Docker clean ==
<syntaxhighlight lang="bash" line>
#!/bin/bash
# Arrêter tous les conteneurs en cours d'exécution
docker stop $(docker ps -q)
# Supprimer tous les conteneurs
docker rm $(docker ps -a -q)
# Supprimer toutes les images
docker rmi $(docker images -q)
# Supprimer tous les volumes
docker volume rm $(docker volume ls -q)
# clean
docker system prune -f
echo "Tous les conteneurs, images et volumes Docker ont été supprimés."
</syntaxhighlight>
== Docker clean old ==
<syntaxhighlight lang="bash" line>
<syntaxhighlight lang="bash" line>
#!/bin/bash
#!/bin/bash
Ligne 102 : Ligne 124 :


menu "$@"
menu "$@"
<a href="https://discordapp.com/users/284694598888980480" target="_blank"><img src="https://img.shields.io/badge/discord-grey?logo=discord" alt=""></a>
https://img.shields.io/badge/MediaWiki-%3E%3D1.43.0-%2336c?style=flat-square&logo=Wikipedia
[![discord](https://img.shields.io/badge/captnsharky-blue?logo=discord&logoColor=white)](https://discordapp.com/users/284694598888980480)


</syntaxhighlight>
</syntaxhighlight>
Ligne 117 : Ligne 131 :
<a href="https://discordapp.com/users/284694598888980480"><img src='https://img.shields.io/badge/captnsharky-blue?logo=discord&logoColor=white' /></a>
<a href="https://discordapp.com/users/284694598888980480"><img src='https://img.shields.io/badge/captnsharky-blue?logo=discord&logoColor=white' /></a>
‎</html>
‎</html>
[[Catégorie:Docker]]

Dernière version du 3 mars 2025 à 09:10

Docker clean

#!/bin/bash

# Arrêter tous les conteneurs en cours d'exécution
docker stop $(docker ps -q)

# Supprimer tous les conteneurs
docker rm $(docker ps -a -q)

# Supprimer toutes les images
docker rmi $(docker images -q)

# Supprimer tous les volumes
docker volume rm $(docker volume ls -q)

# clean
docker system prune -f

echo "Tous les conteneurs, images et volumes Docker ont été supprimés."

Docker clean old

#!/bin/bash

# container stop
function containersStop
{
	echo "Stop all Containers"
	runningContainers=$(docker ps -q)
	if test -n "$runningContainers"
	then
		docker stop $runningContainers
	fi
}

# containers clean
function containersClean
{
	containersStop
	echo "Remove all Containers"
	docker container prune -f
}

# images clean
function imagesClean
{
	echo "Remove all Images"
	images=$(docker images -q)
	if test -n "$images"
	then
		docker image rm -f $images
	fi
}

# volume clean : attention à la perte des données des BD !
function volumesClean
{
	echo "Remove all Volumes"
	if test "$1" != "-f"
	then
		echo "Warning : all Data from Databases will be deleted !"
		echo "Do you want to remove All Volumes (Yes/yes/Y/y/Oui/oui/O/o?"
		read rep
		rep=$(echo "$rep" | tr '[:upper:]' '[:lower:]')
	fi
	if test "$1" = "-f" -o "$rep" = 'yes' -o "$rep" = 'y' -o "$rep" = 'oui' -o "$rep" = 'o'
	then
		volumes=$(docker volume ls -q)
		if test -n "$volumes"
		then
			docker volume rm -f $volumes
		fi
	else
		echo "Volumes are kept"
	fi
}

# all clean
function allClean
{
	echo "Remove All Docker's Ressources : Container - Images - Volumes"
	containersStop
	containersClean
	imagesClean
	volumesClean $1
}

# menu
function menu
{
case $1 in
	"--volumes")
		volumesClean $2
		;;
	"--images")
		imagesClean
		;;
	"--containers")
		containersClean
		;;
	"--all")
		allClean $2
		;;
	*)
		help
		;;
esac
}

# help
function help
{
	printf "$0 [--volumes [-f] | --images | --containers | --all [-f] | --help\n"
	printf "\tClean Docker ressources from the local host\n"
	printf "Options :\n"
	printf "\t--volumes [-f]\t\tRemove all volumes, without confirmation if -f is set\n"
	printf "\t--images\t\tRemove all Images\n"
	printf "\t--containers\t\tStop and remove all containers\n"
	printf "\t--all\t\t\tStop all container, then remove all containers, images and volumes, without confirmation if -f is set\n"
	printf "\t--help\t\t\tShow this help\n"
}

menu "$@"

source :