Skip to content

Instantly share code, notes, and snippets.

@devzom
Last active March 16, 2022 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devzom/8d535e700e11ca1dfcb893a496fc536b to your computer and use it in GitHub Desktop.
Save devzom/8d535e700e11ca1dfcb893a496fc536b to your computer and use it in GitHub Desktop.
docker: Check if Docker container exist and delete it
# Goal of tghis script is to prompt for Docker container name, if it's skipped it will used infile defined name of container and then will search for specific container and delete it.
# Set container default name as variable
CONTAINER=testContainerName
echo -n "Enter Docker container name (if skipped default from file will be used): "
read containerName
# Check if prompted value isNotEmpty
if [ $containerName ]; then
CONTAINER=$containerName
fi
# Check if container exist and delete
if [ "$(docker ps -a | grep -c $CONTAINER)" -gt 0 ]; then
echo -n "Do you wish to delete this container? y/n: "
read answer
if [ "$answer" != "${answer#[Yy]}" ]; then # this grammar (the #[] operator) means that the variable $answer where any Y or y in 1st position will be dropped if they exist.
docker rm -f $CONTAINER
echo "[---- Deleted container $CONTAINER]"
else
echo "Leaving script execution."
fi
else
echo "[---- Container with name: $CONTAINER doesn't exist. ]"
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment