Skip to content

Instantly share code, notes, and snippets.

@timhodson
Created August 17, 2016 09:13
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save timhodson/ea11c76424e5b3f36c017d9d0ca7ad10 to your computer and use it in GitHub Desktop.
Save timhodson/ea11c76424e5b3f36c017d9d0ca7ad10 to your computer and use it in GitHub Desktop.
Run a command on all docker containers
for container in `docker ps -q`; do
# show the name of the container
docker inspect --format='{{.Name}}' $container;
# run the command (date in the case)
docker exec -it $container date;
done
@jmonkfish
Copy link

If you replace "date" or "bash -c 'rm -rf /app/Log*'" with $1, you can pass the desired command into the script:

for container in `docker ps -q`; do 
  # show the name of the container
  docker inspect --format='{{.Name}}' $container;
  # run the command (date in the case)
  docker exec -it $container $1;
done

Examples:

  • ./runCommandAllDockers.sh "some command here"
  • ./runCommandAllDockers.sh "du -h /var/log"

Hint: Wrap your command in double-quotes so the script sees it as one parameter.

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