无法从同一局域网中的另一台主机ping Docker容器 [英] Can not ping docker container from another host which is in the same LAN

查看:66
本文介绍了无法从同一局域网中的另一台主机ping Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一局域网中有两个主机,都运行docker:

I have two hosts, in the same LAN, both running docker:

  • 主持人A:172.16.0.113
  • 主持人B:172.16.0.114

我可以从主机B ping主机A.但是,我不能从主机B ping主机A上的Docker容器.

I can ping host A from host B. But, I can not ping a docker container on host A from host B.

我在docker默认网络(网桥)中创建了一个运行ubuntu的docker容器.默认docker0的IP地址是172.17.0.1.主机(172.16.0.113)中的容器(ubuntu)的IP地址是172.17.0.2.我使用的命令在这里:

I created a docker container running ubuntu, in the docker default network (bridge). The default docker0's IP address is 172.17.0.1. And the IP address of the container (ubuntu) in the host (172.16.0.113) is 172.17.0.2. The command I used is here:

sudo docker run --name container01 -it -p 8080:80 ubuntu /bin/bash

我可以从其主机(主机A)ping容器01;但是我无法从另一台主机B ping ping container01.请注意,我已经暴露了container01的端口.

I can ping container01 from its host, host A; but I can not ping container01 from the other host, host B. Note that I have exposed the port of container01.

我认为当我从主机B ping container01时,数据包被发送到该主机中的容器.但是我想知道如何从主机B连接到主机A的container01.

I think when I ping container01 from the host B, packets are sent to a container in that host. But I want to know how to connect to host A's container01 from host B.

推荐答案

Docker容器在通常无法从其他主机访问的私有地址上运行.如果您有多个Docker主机,则应设置重叠网络将容器放置在它们可以相互访问的同一专用网络上;可以使用群集模式自动完成.

Docker containers run on private addresses that aren't normally accessible from other hosts. If you have multiple docker hosts, then you should setup overlay networking to place containers on the same private network where they can access each other; this can be done automatically with swarm mode.

要从docker外部访问容器,您需要发布端口.使用 docker run 时,这是 -p 8080:80 选项,该选项会将主机端口8080映射到容器端口80(您可以调整这些端口号).使用docker-compose.yml,有一个类似的 ports 部分.

For reaching containers from outside of docker, you need to publish a port. With docker run that's the -p 8080:80 option that would map the host port 8080 to the container port 80 (you can adjust these port numbers). With a docker-compose.yml, there's a similar ports section.

这篇关于无法从同一局域网中的另一台主机ping Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆