To show only running containers use the given command:
docker psTo show all containers use the given command:
docker ps -aTo show the latest created container (includes all states) use the given command:
docker ps -lTo show n last created containers (includes all states) use the given command:
docker ps -n=-1To display total file sizes use the given command:
docker ps -sThe content presented above is from docker.com.
In the new version of Docker, commands are updated, and some management commands are added:
docker container lsIt is used to list all the running containers.
docker container ls -aAnd 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 -fTo remove all unused images, including those not associated with any containers, use:
docker image prune -aThis 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 daysIf 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
-aflag to include all unused images:docker system prune -a
-
Be cautious when using
prunecommands, especially with the-aflag, as they may remove images you want to keep. -
Running
docker system prunecan remove stopped containers and unused volumes, so check what is safe to delete.