Docker Removing Images


Example

The docker rmi command is used to remove images:

docker rmi <image name>

The full image name must be used to remove an image. Unless the image has been tagged to remove the registry name, it needs to be specified. For example:

docker rmi registry.example.com/username/myAppImage:1.3.5

It is also possible to remove images by their ID instead:

docker rmi 693bce725149

As a convenience, it is possible to remove images by their image ID by specifying only the first few characters of the image ID, as long as the substring specified is unambiguous:

docker rmi 693

Note: Images can be removed even if there are existing containers that use that image; docker rmi simply "untags" the image.

If no containers are using an image it is garbage-collected. If a container uses an image, the image will be garbage-collected once all the containers using it are removed. For example:

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED                  STATUS                     PORTS               NAMES
5483657ee07b        hello-world         "/hello"            Less than a second ago   Exited (0) 2 seconds ago                       small_elion

$ docker rmi hello-world
Untagged: hello-world:latest

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED                  STATUS                      PORTS               NAMES
5483657ee07b        693bce725149        "/hello"            Less than a second ago   Exited (0) 12 seconds ago                       small_elion

Remove All Images With No Started Containers

To remove all local images that have no started containers, you can provide a listing of the images as a parameter:

docker rmi $(docker images -qa)

Remove All Images

If you want to remove images regardless of whether or not they have a started container use the force flag (-f):

docker rmi -f $(docker images -qa)

Remove Dangling Images

If an image is not tagged and not being used by any container, it is 'dangling' and may be removed like this:

docker images -q --no-trunc -f dangling=true | xargs -r docker rmi