Linux 容器无法访问其他容器端口

Linux 容器无法访问其他容器端口,linux,docker,ubuntu-16.04,jupyter,iptables,Linux,Docker,Ubuntu 16.04,Jupyter,Iptables,我有一个在443上运行jupyter hub并映射到主机上1443的容器。我正试图在另一个容器中的apache后面运行此服务器。但是,这两个容器无法相互通信 我已经尝试为每个容器设置iptables,但是没有用 sudo iptables -A INPUT -m state -s 172.17.0.2 --state NEW -mtcp -p tcp --dport 1443 -j ACCEPT 172.17.0.2是jupyterhub容器的IP。当两个容器无法相互通信时,问题是由于

我有一个在443上运行jupyter hub并映射到主机上1443的容器。我正试图在另一个容器中的apache后面运行此服务器。但是,这两个容器无法相互通信

我已经尝试为每个容器设置iptables,但是没有用

sudo iptables -A INPUT -m state -s 172.17.0.2  --state NEW  -mtcp -p tcp  --dport 1443  -j ACCEPT

172.17.0.2是jupyterhub容器的IP。

当两个容器无法相互通信时,问题是由于它们不在同一docker网络中。以下是您必须做的:

  • 列出所有可用网络:
    docker网络ls
  • 找出
    apache
    容器正在哪个网络中运行
  • 在同一网络中启动您的
    jupyter hub
    容器
    docker run--网络apache\u网络…
  • 这两个容器无法使用其容器名称相互通信

在处理容器时,请尝试使用DNS而不是IP,因为IP可能会频繁更改。

您需要创建一个用户定义的网络,允许容器相互通信。 在执行docker运行时,请为用户定义的网络提供网络参数。 请参见下面的一个简单示例

docker network create --driver bridge my_custom_network
docker run --network=my_custom_network -itd --name=container1 busybox
docker run --network=my_custom_network -itd --name=container2 busybox
docker attach container2
/ # ping container1
PING container3 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.113 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.147 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.146 ms
64 bytes from 172.19.0.2: seq=3 ttl=64 time=0.146 ms
以下链接提供了更多详细信息:

两个容器可以相互ping,但问题是apache容器无法连接1443端口上的jupyter。因为它们位于同一网络中,apache服务器应该连接443端口上的jupyter容器,而不是1443端口。只有当您想从主机访问jupyter容器时,您才会希望在端口1443访问它。这意味着apache容器无法访问jupyter容器?如果它们位于同一网络中,
apache
container可以访问端口443处的
jupyter
容器,就像
curl jupyter:443
docker network create apache_network
docker network connect apache_network <apache_container_id>
docker network create --driver bridge my_custom_network
docker run --network=my_custom_network -itd --name=container1 busybox
docker run --network=my_custom_network -itd --name=container2 busybox
docker attach container2
/ # ping container1
PING container3 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.113 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.147 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.146 ms
64 bytes from 172.19.0.2: seq=3 ttl=64 time=0.146 ms