I am very new to Docker and enjoying it very much.
I want to delete all images and containers from local as well as from docker hub. Is there any single command to do that?
I am very new to Docker and enjoying it very much.
I want to delete all images and containers from local as well as from docker hub. Is there any single command to do that?
To remove all containers,
docker rm -vf $(docker ps -a -q)
-v
: Remove all associated volumes
-f
: Forces the removal. Like, if any containers is running, you need -f to remove them.
To remove all images,
docker rmi -f $(docker images -a -q)
-a
: for all containers, even not running, (or images)
-q
: to remove all the details other than the ID of containers (or images)
This is the command to be used for your question. It deletes everything(container , images, cache, etc..)
docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N] n
to delete all containers:
docker rm $(docker ps -a -q)
to delete all images:
docker rmi $(docker images -q)
Note that you can't revert and you can't kill containers which running, you should stop them before
See all the existing images:
docker images -a
See all the existing containers:
docker ps -a
Delete single image:
docker images -a
docker rmi <IMAGE_ID>
Delete single container:
docker ps -a
docker rm <CONTAINER_ID>
Delete multiple images:
docker images -a
docker rmi <IMAGE_ID1> <IMAGE_ID2>
Delete multiple containers:
docker ps -a
docker rm <CONTAINER_ID1> <CONTAINER_ID2>
Stop all containers and remove them:
docker rm $(docker kill $(docker ps -aq))
Delete all images:
docker rmi -f $(docker images -a -q)
Delete both all stopped containers and images in a single command:
docker rm $(docker ps -a -q) && docker rmi -f $(docker images -a -q)
To prune all containers:
docker container prune
Delete all unused data (i.e., in order: containers stopped, volumes without containers and images with no containers):
docker system prune
For me the problem were restarting containers. So I did:
1. docker stack ls
2. docker stack rm <xxxxxx>
3. docker stop $(docker ps -aq)
4. docker rm $(docker ps -aq)
5. docker system prune
To delete all containers including its volumes used
docker rm -vf $(docker ps -a -q)
To delete all the images, but before this you should remove all the containers which are created from this images
docker rmi -f $(docker images -a -q)
If you get permission denied, try putting sudo in front of commands as:
sudo docker rm -vf $(sudo docker ps -a -q)
These commands are taken from this link. Please check it for a list of commands.
Using docker-compose it is easier:
ec2-user@devbox$ docker-compose down --rmi all
Reference: https://docs.docker.com/compose/reference/down/
Usage: docker-compose down [options]
Options:
--rmi type Remove images. Type must be one of:
'all': Remove all images used by any service.
'local': Remove only images that don't have a
custom tag set by the `image` field.
-v, --volumes Remove named volumes declared in the `volumes`
section of the Compose file and anonymous volumes
attached to containers.
--remove-orphans Remove containers for services not defined in the
Compose file
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
(default: 10)