Docker - how to call bash command from one contain

2019-08-14 14:21发布

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条回答
Ridiculous、
2楼-- · 2019-08-14 14:44

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

查看更多
登录 后发表回答