Docker exec on all running containers

前端 未结 3 1415
一生所求
一生所求 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
    
    0 讨论(0)
  • 2021-01-24 05:56

    Try running git command with the absolute path, sometimes helps on docker to use the absolute path to a binary:

    Also have a look at this cheatsheet:(I always use this when using docker) https://gist.github.com/ruanbekker/4e8e4ca9b82b103973eaaea4ac81aa5f

    I would suggest that you download the code from github and put it in a volume where the volume points to a directory on your local machine. That way you can work directly on code on your own computer and interact with git while working with the code. That way the docker container can directly access the code from github via the volume. Also have a look at some good practices for using git with docker, you probably wanna delete all .git files before deploying your code for security reasons. Let me know if you need help with putting the source code in a volume.

    0 讨论(0)
  • 2021-01-24 06:03

    I'd assume you have more than one container running, and $(docker ps -q) is expanding to some_container1 606a1083d0be and it's treating 606a1083d0be as the command you want to run, which doesn't exist. Can you post the output of docker ps -q alone to confirm please? Also if you want just the latest container id, try substituting $(docker ps -ql) instead.

    Edit: in response to your confirmation, exactly what I said is happening. As for why it worked before, you likely only had one container running then.

    0 讨论(0)
提交回复
热议问题