How to delete all Docker local Docker images

2020-05-10 16:27发布

I recently started using Docker and never realized that I should use docker-compose down instead of ctrl-c or docker-compose stop to get rid of my experiments. I now have a large number of unneeded docker images locally.

Is there a flag I can run to delete all the local docker images & containers?

Something like docker rmi --all --force --all flag does not exist but I am looking for something with similar idea.

13条回答
闹够了就滚
2楼-- · 2020-05-10 17:04

Easy and handy commands

To delete all images

docker rmi $(docker images -a)

To delete containers which are in exited state

docker rm $(docker ps -a -f status=exited -q)

To delete containers which are in created state

docker rm $(docker ps -a -f status=created -q)

NOTE: Remove all the containers then remove the images

查看更多
Explosion°爆炸
3楼-- · 2020-05-10 17:05

To delete all containers including its volumes use,

docker rm -vf $(docker ps -a -q)

To delete all the images,

docker rmi -f $(docker images -a -q)

Remember, you should remove all the containers before removing all the images from which those containers were created.

In case you are working on Windows (Powershell),

$images = docker images -a -q
foreach ($image in $images) { docker image rm $image -f }
查看更多
Emotional °昔
4楼-- · 2020-05-10 17:07

To delete all images :

docker rmi $(docker images -a -q)

where -a is all, and -q is return only image ids

To remove unused images, and containers :

docker system prune

beware as if you are using docker swarm, and your local machine is joining remote swarm (as manager/worker), your local will be the deployed repo. executing this thus removes the deployed images.

查看更多
在下西门庆
5楼-- · 2020-05-10 17:09

docker image prune -a

Remove all unused images, not just dangling ones. Add -f option to force.

Local docker version: 17.09.0-ce, Git commit: afdb6d4, OS/Arch: darwin/amd64

$ docker image prune -h
Flag shorthand -h has been deprecated, please use --help

Usage:  docker image prune [OPTIONS]

Remove unused images

Options:
  -a, --all             Remove all unused images, not just dangling ones
      --filter filter   Provide filter values (e.g. 'until=<timestamp>')
  -f, --force           Do not prompt for confirmation
      --help            Print usage
查看更多
祖国的老花朵
6楼-- · 2020-05-10 17:15

To simple clear everything do:

$ docker system prune --all

Everything means:

  • all stopped containers
  • all networks not used by at least one container
  • all images without at least one container associated to them
  • all build cache
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-05-10 17:16

Another way with xargs

docker image ls -q | xargs -I {} docker image rm -f {}
查看更多
登录 后发表回答