![如何连接在同一主机上运行的两个docker容器?](https://linux22.com/image/650822/%E5%A6%82%E4%BD%95%E8%BF%9E%E6%8E%A5%E5%9C%A8%E5%90%8C%E4%B8%80%E4%B8%BB%E6%9C%BA%E4%B8%8A%E8%BF%90%E8%A1%8C%E7%9A%84%E4%B8%A4%E4%B8%AAdocker%E5%AE%B9%E5%99%A8%EF%BC%9F.png)
我有两个 Docker 容器正在运行
docker ps
结果
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0bfd25abbfc6 f_service:latest "/usr/local/start-fl 13 seconds ago Up 2 seconds 0.0.0.0:8081->8081/tcp flume
6a1d974f4e3e h_service:latest "/usr/local/start-al 2 minutes ago Up About a minute 0.0.0.0:8080->8080/tcp hadoop
Hadoop 服务在 hadoop 容器中运行[即 datanode、namenode、jobtracker、tasktracker、secondarynamenode]
Flume 服务运行在 flume 容器上[即 flume-agent]
我想在 flume 容器上运行 hadoop 命令。[即 hadoop fs -ls /] 怎么做?有什么想法吗?
我尝试链接但未能实现。
针对容器运行命令:
docker run -it --name hadoop -p 8080:8080 h_service
jps on hadoop container shows all hadoop services
docker run -it -p 8081:8081 --name flume --link hadoop:hadoop f_service
jps on flume shows
jps and Application.[which is flume i guess]
如果我在 flume 容器内执行任何 Hadoop 命令,我会收到以下错误
mkdir: Call From 282fc55ec08d/172.17.5.236 to localhost:8020 failed on connection exception:
java.net.ConnectException: Connection refused; For more details see: http://wiki.apache.org
/hadoop/ConnectionRefused
telnet localhost 8020
无法连接到远程主机。8080 也是如此。
netstat
水槽容器内部
netstat -na
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
hadoop 容器上的 netstat 显示
netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:49096 localhost:8020 TIME_WAIT
tcp 0 0 localhost:49079 localhost:8020 ESTABLISHED
tcp 0 0 localhost:8020 localhost:49079 ESTABLISHED
tcp 0 0 c0c82bab5efd:54003 likho.canonical.com:80 TIME_WAIT
tcp6 0 0 localhost:8021 localhost:40735 ESTABLISHED
tcp6 0 0 localhost:40735 localhost:8021 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ] STREAM CONNECTED 9223040
unix 2 [ ] STREAM CONNECTED 9223013
unix 2 [ ] STREAM CONNECTED 9222782
unix 2 [ ] STREAM CONNECTED 9222116
unix 2 [ ] STREAM CONNECTED 9221761
unix 2 [ ] STREAM CONNECTED 9221758
unix 2 [ ] STREAM CONNECTED 9221302
unix 2 [ ] STREAM CONNECTED 9221284
unix 2 [ ] STREAM CONNECTED 9220884
unix 2 [ ] STREAM CONNECTED 9220877
其中 localhost:8020,我猜 8020 是从 core-site.xml 的规范中得到的
答案1
这个有一个简单的解决方案。首先,如果你想连接到你的 Hadoop 容器的 8020 端口,你应该确保该端口也暴露出来。其次,这些容器各自都有自己的环回接口(localhost)和 IP 地址。它们通过桥接网络 docker0 连接到主机的 eth0 接口。因此,你需要使用 Docker 注入 flume 容器的 IP 地址。
因此这些将正确启动容器:
docker run -it --name hadoop --expose 8080 --expose 8020 h_service
docker run -it --name flume --link hadoop:had00p -p 8081:8081 f_service
但是您需要告诉 flume 通过“had00p”而不是“localhost”连接到 Hadoop。我在这里使用 had00p 只是为了区分容器内部的别名和您为运行 Hadoop 的容器指定的容器名称。
这是一个简单的例子:
docker run -d --name container_a --expose 8080 busybox:latest nc -l 0.0.0.0 8080
docker run --rm --link container_a:dep_alias busybox:latest env
docker run --rm --link container_a:dep_alias busybox:latest cat /etc/hosts
当 Docker 创建应用程序链接时,它会注入许多环境变量,并将主机名添加到链接容器的 /etc/hosts 文件中。如果您已禁用容器间通信,它还会添加防火墙规则以允许两个容器之间进行通信。