我有2个正在运行的Docker容器(A和B)。
需要从容器A内调用->容器B内的bash命令。
如何实现呢?
如果您的B容器看起来像这样:
docker run -d --name b_container --rm ubuntu:latest sleep 50000
并且您的A容器看起来像这样:
docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu:latest sh -c "apt-get update ; apt-get install docker.io -y ; bash"
您可以做(当您陷入A后):
$ env | grep HOSTNAME
HOSTNAME=7d146fa7caac
# # note this is the name for container A
# # and note that the name WONT look exactly like this, but very similar
$ docker exec -it b_container env | grep HOSTNAME
HOSTNAME=668838c220c0
# # and note that you are executing commands in container B and the `HOSTNAME` is different.
$ docker exec -it b_container ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 4372 672 ? Ss 14:40 0:00 sleep 50000
root 43 0.0 0.1 34420 2800 pts/0 Rs+ 14:48 0:00 ps aux
# # and here is how you know for sure that you're really hitting the other container
附言 我不建议您这样做,您想要一种方法...在那里
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句