gpt4 book ai didi

docker - 为什么有人会为两个 docker 容器使用相同的网络命名空间?

转载 作者:行者123 更新时间:2023-12-05 00:46:07 24 4
gpt4 key购买 nike

为什么要通过网络命名空间而不是仅通过一个网络连接两个 docker 容器?

据我所知,唯一的区别是您可以使用 localhost 调用另一个容器。我没有看到任何需要这样做的用例。

有人对此有经验吗?

最佳答案

我能想到的一个原因是 使用工具或命令 这在您的容器中不可用。下面这个例子直接来自 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

以类似的方式,可以使用此技术来 调试 一个容器。例如,如果您的容器没有 tcpdump ,您可以创建一个包含它的图像:
docker build -t tcpdump - <<EOF 
FROM ubuntu
RUN apt-get update && apt-get install -y tcpdump
CMD tcpdump -i eth0
EOF
run用于调试应用的容器:
docker run --rm --net=container:my-app tcpdump

如果您的问题更多是关于 Kubernetes ,一些有趣的链接是:
  • The Kubernetes Network Model
  • What is the role of a pause container?
  • Understanding kubernetes networking: pods
  • 关于docker - 为什么有人会为两个 docker 容器使用相同的网络命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55004488/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com