简体   繁体   中英

Docker exec on all running containers

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:

docker exec $(docker ps -q) bash -c "cd /var/www/html && git pull"

but it errors out with this:

OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"606a1083d0be\": executable file not found in $PATH": unknown

It worked at one point, but then suddenly stopped working for no apparent reason (I didn't change any docker configuration)

Note: the output of docker ps -q is only container ids:

511c76a25dcc
995bd453c467

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM