Docker - how to call bash command from one container into another from the same host? [closed]

隐身守侯 提交于 2019-12-11 11:06:30

问题


I have 2 docker containers running (A and B).

Have a need to call from within container A -> a bash command inside container B.

How to achieve this?


回答1:


If your B container looks something like this:

docker run -d --name b_container --rm ubuntu:latest sleep 50000

and your A container looks something like this:

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"

you can do (while you're bashed into 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

Ps. i don't suggest you do this, you wanted a way... there you are



来源:https://stackoverflow.com/questions/48990341/docker-how-to-call-bash-command-from-one-container-into-another-from-the-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!