Why would anyone use the same network namespace for two docker containers?

天大地大妈咪最大 提交于 2019-12-06 03:39:30

One reason I can think of is for using a tool or command that is not available in your container. This example below comes directly from the docker run docs:

NETWORK: CONTAINER

Example running a Redis container with Redis binding to localhost then running the redis-cli command and connecting to the Redis server over the localhost interface.

$ docker run -d --name redis example/redis --bind 127.0.0.1
$ # use the redis container's network stack to access localhost
$ docker run --rm -it --network container:redis example/redis-cli -h 127.0.0.1

In a similar way, one can use this technique to debug a container. For example, if your container doesn't have tcpdump, you can create an image which has it:

docker build -t tcpdump - <<EOF 
FROM ubuntu 
RUN apt-get update && apt-get install -y tcpdump 
CMD tcpdump -i eth0 
EOF

and run a container to debug your app:

docker run --rm --net=container:my-app tcpdump

If your question was more about Kubernetes, a few interesting links are:

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