Docker-如何在同一主机上将bash命令从一个容器调用到另一个容器? [英] Docker - how to call bash command from one container into another from the same host?

查看:259
本文介绍了Docker-如何在同一主机上将bash命令从一个容器调用到另一个容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个正在运行的Docker容器(A和B).

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

需要从容器A内调用->容器B内的bash命令.

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

如何实现?

推荐答案

如果您的B容器看起来像这样:

If your B container looks something like this:

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

和您的A容器看起来像这样:

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"

您可以(在陷入A后):

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.我不建议您这样做,您想要一种方法...在那里

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

这篇关于Docker-如何在同一主机上将bash命令从一个容器调用到另一个容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆