7

I am deleting dangling docker images.

Before removing these images, I want to see if there are any containers, which are instances from these dangling images.

If so I want to log them and abort the deletion.

So far I did not find any command for that.

My solution would be to get all containers docker ps -a and all dangling images docker images -aqf dangling=true and compare repo + tag from the image with image from the container.

I am using docker 1.12

3
  • 1
    See docker ps --format ancestor... Commented May 29, 2017 at 16:12
  • Docker would not let you remove any image that is in use by any container. There's no need to check unless you also want to remove those containers first as well... Commented May 29, 2017 at 20:26
  • @user2915097 I think you mean --filter / -f ancestor, but actually this is what I am looking for Commented May 30, 2017 at 11:43

1 Answer 1

13

How to list images and their containers?

You can edit the --format in order to fit to your needs:

docker ps -a --format="container:{{.ID}} image:{{.Image}}"

How to delete dangling images?

This command is intended to clean dangling image without touching images that are being used by containers:

$ docker image prune

WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y

But if you don't have that command in your docker version, you could try the following.

If the image is dangling, you should see a hash in the IMAGE column in docker ps. That should not be an usual case, tough.

This print the used images by running/stopped containers:

docker ps -a --format="{{.Image}}"

And this list your dangling images:

docker images -qf "dangling=true"

Take with caution:

#!/bin/bash

# Remove all the dangling images
DANGLING_IMAGES=$(docker images -qf "dangling=true")
if [[ -n $DANGLING_IMAGES ]]; then
    docker rmi "$DANGLING_IMAGES"
fi

# Get all the images currently in use
USED_IMAGES=($( \
    docker ps -a --format '{{.Image}}' | \
    sort -u | \
    uniq | \
    awk -F ':' '$2{print $1":"$2}!$2{print $1":latest"}' \
))

# Remove the unused images
for i in "${DANGLING_IMAGES[@]}"; do
    UNUSED=true
    for j in "${USED_IMAGES[@]}"; do
        if [[ "$i" == "$j" ]]; then
            UNUSED=false
        fi
    done
    if [[ "$UNUSED" == true ]]; then
        docker rmi "$i"
    fi
done
3
  • Before deleting my dangling images I want to see if there are any containers, which are instances of these dangling images. So I think as user2915097 mentioned running docker ps -qf ancestor=imageID gives me only the containers which are instances from dangling images. So I can delete all related container(s) and after that the image(s). Commented May 30, 2017 at 11:40
  • 1
    Isn't useful for you docker system prune?
    – Robert
    Commented May 30, 2017 at 11:42
  • I wasn't clear for me that you want to remove related containers too
    – Robert
    Commented May 30, 2017 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.