Docker exec on all running containers

前端 未结 3 1418
一生所求
一生所求 2021-01-24 05:36

I am running several docker containers running on my server, and need to exec a git pull for a repository that is on all of them.

I have tried using this:



        
3条回答
  •  被撕碎了的回忆
    2021-01-24 05:50

    Using Docker exec you can run the command on the container one at a time, but from your Question you want to run the command on all running container, here you go.

        for containerId in $(docker ps -q)
        do
            docker exec -it $containerId bash -c 'cd /var/www/html && git pull'
        done
    

    I assume git is already installed in all running container and all base on bash

    Or more compact form can be

    for i in `docker ps -q`; do docker exec -it $i bash -c 'cd /var/www/html && git pull'; done
    

提交回复
热议问题