[英]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. 我正在服务器上运行多个docker容器,并且需要对所有容器上的存储库执行git pull。
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) 它曾经工作过,但是后来突然停止工作,没有明显原因(我没有更改任何docker配置)
Note: the output of docker ps -q
is only container ids: 注意:
docker ps -q
的输出仅是容器ID:
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. 我假设您有多个容器在运行,并且
$(docker ps -q)
some_container1 606a1083d0be
$(docker ps -q)
扩展到some_container1 606a1083d0be
,并将606a1083d0be
视为要运行的命令,该命令不存在。 Can you post the output of docker ps -q
alone to confirm please? 您可以单独发布
docker ps -q
的输出以确认吗? Also if you want just the latest container id, try substituting $(docker ps -ql)
instead. 另外,如果只需要最新的容器ID,请尝试替换
$(docker ps -ql)
。
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. 使用Docker exec可以一次在容器上运行该命令,但是从您要在所有正在运行的容器上运行该命令的问题出发,就可以了。
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 我假设git已经安装在所有正在运行的容器中,并且全部基于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: 尝试使用绝对路径运行git命令,有时有助于docker使用二进制的绝对路径:
Also have a look at this cheatsheet:(I always use this when using docker) https://gist.github.com/ruanbekker/4e8e4ca9b82b103973eaaea4ac81aa5f 也看看这个备忘单:(我在使用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. 我建议您从github下载代码并将其放在一个卷中,该卷指向本地计算机上的目录。 That way you can work directly on code on your own computer and interact with git while working with the code.
这样,您可以直接在自己的计算机上处理代码,并在使用代码时与git交互。 That way the docker container can directly access the code from github via the volume.
这样,docker容器可以通过该卷直接从github访问代码。 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.
还可以查看将git与docker结合使用的一些好的做法,出于安全原因,您可能想在部署代码之前删除所有.git文件。 Let me know if you need help with putting the source code in a volume.
如果您需要帮助将源代码放入一个卷中,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.