Some docker and docker-compose Commands for Reference

Prune unused Docker objects (new style with prune command)

Docs here: https://docs.docker.com/config/pruning/

Docker takes a conservative approach to cleaning up unused objects (often referred to as “garbage collection”), such as images, containers, volumes, and networks: these objects are generally not removed unless you explicitly ask Docker to do so. This can cause Docker to use extra disk space. For each type of object, Docker provides a prune command. In addition, you can use docker system prune to clean up multiple types of objects at once. This topic shows how to use these prune commands.

Prune images

Clean unused images (only affects dangling images; those not tagged and not referenced by any container):

docker image prune

Prune volumes

docker volume prune

Be careful pruning volumes, obviously.

Prune networks

Although it won't free up much disk space, use docker network prune to remove any firewall rules, bridge devices, routing table entries, etc. which aren’t associated with any containers.

Prune all

To remove it all (except volumes):

docker system prune

To remove volumes as well:

docker system prune --volumes

This will remove:

  • all stopped containers
  • all networks not used by at least one container
  • all volumes not used by at least one container
  • all dangling images
  • all build cache

Using docker Command Directly

Show containers:

docker ps

Most recently created container:

docker ps -l

Output just container IDs:

docker ps -aq

Full container hash:

docker ps -aq --no-trunc

Container names:

docker inspect --format='{{.Name}}' $(sudo docker ps -aq --no-trunc)

Stop all containers:

docker stop $(docker ps -a -q)

Stop specific container by ID:

docker stop <container-id-1> <container-id-2>

Misc

Old school way to aggressively remove all containers (including any that may be running):

docker container rm $(docker container ls -aq) -f

Tags

 docker  docker-compose  containers  reference