Created
August 17, 2016 09:13
-
-
Save timhodson/ea11c76424e5b3f36c017d9d0ca7ad10 to your computer and use it in GitHub Desktop.
Run a command on all docker containers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
@bison92 you can do something like this:
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 bash -c 'rm -rf /app/Log*';
done
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
Does this work when the command has arguments? I'm trying to perform a massive Log deletion with "rm -rf /app/Log*" instead of "date" but it is not working.