docker容器无法访问宿主机-No route to host

原文链接:https://www.jianshu.com/p/96aebba5d3cc

原因分析

本文中在centos上部署docker容器,其网络模式采用的是bridger模式。

启动docker时,docker进程会创建一个名为docker0的虚拟网桥,用于宿主机与容器之间的通信。当启动一个docker容器时,docker容器将会附加到虚拟网桥上,容器内的报文通过docker0向外转发。

如果docker容器访问宿主机,那么docker0网桥将报文直接转发到本机,报文的源地址是docker0网段的地址。而如果docker容器访问宿主机以外的机器,docker的SNAT网桥会将报文的源地址转换为宿主机的地址,通过宿主机的网卡向外发送。

因此,当docker容器访问宿主机时,如果宿主机服务端口会被防火墙拦截,从而无法连通宿主机,出现No route to host的错误。

而访问宿主机所在局域网内的其他机器,由于报文的源地址是宿主机ip,因此,不会被目的机器防火墙拦截,所以可以访问。

解决方法

方法一:关闭防火墙

centos关闭防火墙的操作为

systemctl stop firewalld

方法二: 在防火墙上开发指定端口

firewall-cmd --zone=public --add-port=2181/tcp --permanent
firewall-cmd --reload

小结

这个问题是在用docker方式部署fabric网络中遇见的错误,容器技术为fabric网络部署带来了极大的便利和运维的方便,但是另一方面也带来了网络的复杂,因此,在运行fabric网络中要尤其注意docker镜像网络问题。


 

展开阅读全文

sendto的问题(no route to host)

07-24

本人写了段网络程序,其中在无限循环中调用了函数sendto,在向对方发送udp包的过程中,有时能够发送成功,有时却失败,错误号为113,错误原因为no route to host,下面是具体代码,向大侠请教错误原因rnrnrn/*rn * 在向对方的g_rport端口发送udp包的同时,还要不断监听本机的g_rport端口rn */rnvoid send_hb_udp_msg()rnrn int ret;rn int sockfd;rn int servfd;rn char msg[512];rn struct sockaddr_in toaddr, servaddr;rnrn sockfd = socket(AF_INET, SOCK_DGRAM, 0); //发送socketrn if (sockfd < 0)rn rn wlog(log_die, "%s(%d)[%s], errno %d, %s",__FILE__, __LINE__, __FUNCTION__, errno, strerror(errno));rn rn rn bzero(&toaddr, sizeof(toaddr));rn toaddr.sin_family = AF_INET;rn toaddr.sin_addr.s_addr = htonl(inet_addr(slave_ip)); //对方iprn toaddr.sin_port = htons(g_rport);rnrn rn servfd = socket(AF_INET, SOCK_DGRAM, 0); //接收socketrn if (servfd < 0)rn rn wlog(log_die, "%s(%d)[%s], errno %d, %s",__FILE__, __LINE__, __FUNCTION__, errno, strerror(errno));rn rnrn bzero(&servaddr, sizeof(servaddr));rn servaddr.sin_family = AF_INET;rn servaddr.sin_addr.s_addr = htonl(INADDR_ANY);rn servaddr.sin_port = htons(g_rport);rn rn rn ret = bind(servfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); //接收socket绑定本地地址对rn if (ret == -1)rn rn wlog(log_die, "%s(%d)[%s], errno %d, %s",__FILE__, __LINE__, __FUNCTION__, errno, strerror(errno));rn rn rn //get host name;rn char hostname[32];rn gethostname(hostname, sizeof(hostname));rnrn fd_set rset;rn FD_ZERO(&rset);rn FD_SET(servfd, &rset);rnrn while(1)rn rn snprintf(msg, sizeof(msg), "%s,%d,%d", hostname, server_status, random_num);rn rn ret = sendto(sockfd, msg, strlen(msg), 0,(struct sockaddr *)&toaddr, sizeof(toaddr));[color=#FF0000][/color]rn rn if (ret == -1)rn rn wlog(log_info, "errno:%d, %s", errno, strerror(errno));rn continue;rn rn rn struct timeval tv;rn tv.tv_sec = 1;rn tv.tv_usec = 0; rnrn ret = select(1, &rset, NULL, NULL, &tv);rn if (ret == -1)rn rn wlog(log_die, "errno %d, %s", errno, strerror(errno));rn rn else if (FD_ISSET(servfd, &rset)) rn rn // 省略rn rn rn 论坛

java.net.SocketException: No route to host报错

07-09

新手求教,报错LOG如下:rnjava.net.SocketException: No route to hostrnat org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)rnat dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)rnat org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)rnat org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:440)rnat java.net.Socket.connect(Socket.java:1013)rnat org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(HttpConnection.java:75)rnat org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(HttpConnection.java:48)rnat org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)rnat org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)rnat org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)rnat org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)rnat org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)rnrn报错的代码段:rn[code=java]URL url = new URL(validateUrl);rnconn = (HttpURLConnection) url.openConnection();rnconn.setConnectTimeout(5000);rnconn.setRequestMethod("GET"); // 请求格式rnconn.connect();//在这报错[/code]rn手机移动网开着呢, 也加上了。rn[b]出现这种情况一般是什么问题啊?[/b] 论坛

java.net.NoRouteToHostException: No route to host: connect

06-25

项目之前好好的,今天突然报错,2台电脑,一台是我电脑,一台是数据库,ping也ping的通,防火墙什么的也关了,可还是不行,我电脑要说有什么改动也就是中间重做系统了,跟这个难道有关系?rn[code=java]java.net.NoRouteToHostException: No route to host: connect rn at java.net.PlainSocketImpl.socketConnect(Native Method) rn at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) rn at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) rn at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) rn at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) rn at java.net.Socket.connect(Socket.java:519) rn at java.net.Socket.connect(Socket.java:469) rn at java.net.Socket.(Socket.java:366) rn at java.net.Socket.(Socket.java:240) rn at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:79) rn at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:121) rn at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:706) rn at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1321) rn at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:386) rn at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170) rn at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396) rn at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324) rn at cn.com.jit.assp.ias.http.HttpServerStatusChecker.isAlive(HttpServerStatusChecker.java:81) rn at cn.com.jit.assp.ias.sp.saml11.config.util.GatewayList.syncGateway(GatewayList.java:78) rn at cn.com.jit.assp.ias.sp.saml11.config.util.GatewayTimerTask.run(GatewayTimerTask.java:41) rn at java.util.TimerThread.mainLoop(Timer.java:512) rn at java.util.TimerThread.run(Timer.java:462) rn[/code] 论坛

没有更多推荐了,返回首页