To show only running containers use the given command:
docker ps
To show all containers use the given command:
docker ps -a
To show the latest created container (includes all states) use the given command:
docker ps -l
To show n last created containers (includes all states) use the given command:
docker ps -n=-1
To display total file sizes use the given command:
docker ps -s
The content presented above is from docker.com.
In the new version of Docker, commands are updated, and some management commands are added:
docker container ls
It is used to list all the running containers.
docker container ls -a
And then, if you want to clean them all,
docker rm $(docker ps -aq)
This used to list all the containers created irrespective of its state.
And to stop all the Docker containers (force)
docker rm -f $(docker ps -a -q)
Here, the container is the management command.
To prune old Docker images and free up space, you can use the following commands based on your requirements.
To remove all unused images (dangling and unreferenced):
docker image prune
- Dangling images: These are layers of images not associated with any container.
- The command will prompt for confirmation by default.
Use the -f
(force) flag to skip the confirmation:
docker image prune -f
To remove all unused images, including those not associated with any containers, use:
docker image prune -a
This will remove images that are not currently being used by any running or stopped container.
If you want to prune images based on their size or time since last use, you can use the --filter
flag:
docker image prune --filter "until=24h" # Removes images not used in the last 24 hours
docker image prune --filter "until=7d" # Removes images not used in the last 7 days
If you know the image ID or name and want to remove it directly:
- List all images:
docker images
- Remove a specific image:
docker rmi <image_id_or_name>
To remove all unused data, including unused images, containers, volumes, and networks:
docker system prune
- Use the
-a
flag to include all unused images:docker system prune -a
-
Be cautious when using
prune
commands, especially with the-a
flag, as they may remove images you want to keep. -
Running
docker system prune
can remove stopped containers and unused volumes, so check what is safe to delete.