This Windows batch script identifies and removes all dangling Docker images from your system.
npm run import -- "Delete danglings images in cmd"FOR /f "tokens=*" %i IN ('docker images -q -f "dangling=true"') DO docker rmi %i
#!/bin/bash
# Function to remove dangling Docker images
remove_dangling_images() {
# Get a list of dangling images using Docker
dangling_images=$(docker images -q -f "dangling=true")
# Iterate over the list using while loop for better readability
while IFS= read -r image; do
# Remove each dangling image
docker rmi "${image}" || echo "Error removing image ${image}"
done <<< "$dangling_images"
}
# Call the function to remove dangling images
remove_dangling_imagesThis code snippet removes all dangling Docker images from the system.
Breakdown:
FOR /f "tokens=*" %i IN ('docker images -q -f "dangling=true"'):
FOR /f iterates over lines of input."tokens=*" tells it to capture all tokens (words) from each line.%i is a loop variable that will hold each captured token (image ID).'docker images -q -f "dangling=true"' executes the command docker images -q -f "dangling=true" and pipes its output (IDs of dangling images) to the loop.
-q (quiet) flag outputs only image IDs.-f "dangling=true" filters for images that are dangling (not associated with any running or stopped containers).DO docker rmi %i:
docker rmi %i removes the Docker image with the ID stored in %i.In essence:
docker images -q -f "dangling=true".Here's a breakdown:
FOR /f "tokens=*" %i IN ('docker images -q -f "dangling=true"') DO: This is a Windows batch script command that iterates over a list of dangling Docker images.
FOR /f: This initiates a "for each file" loop."tokens=*": This tells the loop to capture all tokens (words) from the output of the command inside the parentheses.%i: This is a placeholder variable that will hold each captured token (i.e., each dangling image ID).IN ('docker images -q -f "dangling=true"'): This is the command that generates the list of dangling images.
docker images: This command lists Docker images.-q: This flag tells docker images to output only the image IDs.-f "dangling=true": This flag filters the output to only include images that are dangling (not associated with any running or stopped containers).DO: This keyword marks the beginning of the code block that will be executed for each dangling image ID.docker rmi %i: This command removes the dangling Docker image specified by the %i variable.
In summary: This code snippet finds all dangling Docker images on your system and then removes them.