Run docker commands on all or some containers

  • Use the ps command with the q param to fetch the insance id and then apply the command:
    • docker {COMMAND TO RUN} $(docker ps -a -q)  -> for containers
    • docker {COMMAND TO RUN} $(docker images -q) -> for images
  • Examples:
    • Delete all stopped containers: docker rm $(docker ps -a -q)
    • Delete all unused images: docker rmi $(docker images -q)
    • Start all stopped containers: docker start $(docker ps -a -q)
    • Restart all containers: docker restart $(docker ps -a -q)
  • For more elavorated filters:
    • docker  {COMMAND TO RUN} $(docker ps -a |grep {TEXT-TO-CAPTURE}|awk ‘{ print $1 }’)  wehre awk ‘{ print $1 }’ captures the container id value.