Andrea Baccega
Andrea Baccega

Reputation: 27613

Docker: How Do I remove all images used to build a container?

Is there a way to identify all the images used to run a container?

I'm referring to all the layers that compose a container.

I'd like to remove those images but cant find a way to specify that I want to delete only those associated to a container.

Upvotes: 0

Views: 389

Answers (1)

zigarn
zigarn

Reputation: 11595

You first need to understand the difference between image and container (you are mixing 'run' and 'build' about a container in your question): you build an image then run a container based on an image.

If you want to reclaim some space be removing unused images and layers (from old builds of image), as @oliver-charlesworth said, use docker image prune.
There is also docker container prune to remove stopped containers.

To go further:

To get the image of a container:

docker container inspect --format '{{.Image}}' <CONTAINER_ID_OR_NAME>

Then to get the layers of the image, use docker image history:

docker image history <RESULT_OF_PREVIOUS_COMMAND>

BUT each layer is built on the previous one so you won't be allowed to remove the layers composing a container

docker image rm <IMAGE_ID>
Error response from daemon: conflict: unable to delete <IMAGE_ID> (cannot be forced) - image has dependent child images
# OR
Error response from daemon: conflict: unable to remove repository reference "<IMAGE_NAME>" (must force) - container <CONTAINER_ID is using its referenced image <IMAGE_ID>

Upvotes: 1

Related Questions