Skip to content

Instantly share code, notes, and snippets.

@jpalala
Last active December 26, 2024 10:33
Show Gist options
  • Save jpalala/1af6e38e68794173be6d2331207aa902 to your computer and use it in GitHub Desktop.
Save jpalala/1af6e38e68794173be6d2331207aa902 to your computer and use it in GitHub Desktop.
docker hints

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.

Prune Unused Images

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.

Prune Unused Images Without Prompt

Use the -f (force) flag to skip the confirmation:

docker image prune -f

Prune All Unused Images (Including Unreferenced Images)

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.

Prune with a Size Filter

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

Remove Specific Images

If you know the image ID or name and want to remove it directly:

  1. List all images:
docker images
  1. Remove a specific image:
docker rmi <image_id_or_name>

Prune Everything (Images, Containers, Volumes, Networks)

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

Notes

  • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment