首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >docker容器如何连接到主机上的端口?

docker容器如何连接到主机上的端口?
EN

Stack Overflow用户
提问于 2019-01-01 18:20:00
回答 2查看 863关注 0票数 0

我的问题非常类似于这个(From inside of a Docker container, how do I connect to the localhost of the machine?)。

我尝试使用--network="host"连接到主机的8118代理,但这不是我想要的。我仍然想使用网桥模式。其实我觉得docker的桥接类似于传统主机上安装的sense.The虚拟交换机docker0中的NAT,不同的容器依靠这个交换机来相互通信,容器也可以ping主机,理论上可以和主机通信,可以访问它的开放端口,但实际上不能,不知道为什么,谁能帮我?( ping协议是基于tcp的,这也意味着20/21端口在8118? reachable.why不可达?)

好的,我可能已经找到原因了,端口要被监控,我会尝试更改主机代理软件的监控。

以下是我的尝试,容器无法成功连接到主机上的8118代理端口:

The terminal on the left is my host, and on the right is my docker container

主机:

VirtualBox-centos7 (ip:192.168.125.95,shadowsocks127.0.0.1:1080,privoxy127.0.0.1:8118):wget可以。

docker:

设置http_proxy=192.168.125.95:8118的容器...wget得到一个错误:没有到主机的路由,然后我关闭防火墙并再次尝试得到另一个错误:连接被拒绝。

docker容器:

代码语言:javascript
运行
复制
root@bee1d2892df4:/go# ip route show
default via 172.17.0.1 dev eth0 
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2 
root@bee1d2892df4:/go# ip add                 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
23: eth0@if24: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
root@bee1d2892df4:/go# telnet 172.17.0.1 8118 
Trying 172.17.0.1...
telnet: Unable to connect to remote host: Connection refused
root@bee1d2892df4:/go# telnet 192.168.125.95 8118
Trying 192.168.125.95...
telnet: Unable to connect to remote host: Connection refused
root@bee1d2892df4:/go# 

主持人:(这应该是无用的,我的iptables不应该被启动。)

代码语言:javascript
运行
复制
[root@localhost shadowsocks]# iptables -A INPUT -i docker0 -j ACCEPT
[root@localhost shadowsocks]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    DOCKER-USER  all  --  0.0.0.0/0            0.0.0.0/0           
2    DOCKER-ISOLATION-STAGE-1  all  --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
4    DOCKER     all  --  0.0.0.0/0            0.0.0.0/0           
5    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
6    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
7    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
8    DOCKER     all  --  0.0.0.0/0            0.0.0.0/0           
9    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
10   ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain DOCKER (2 references)
num  target     prot opt source               destination         

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
num  target     prot opt source               destination         
1    DOCKER-ISOLATION-STAGE-2  all  --  0.0.0.0/0            0.0.0.0/0           
2    DOCKER-ISOLATION-STAGE-2  all  --  0.0.0.0/0            0.0.0.0/0           
3    RETURN     all  --  0.0.0.0/0            0.0.0.0/0           

Chain DOCKER-ISOLATION-STAGE-2 (2 references)
num  target     prot opt source               destination         
1    DROP       all  --  0.0.0.0/0            0.0.0.0/0           
2    DROP       all  --  0.0.0.0/0            0.0.0.0/0           
3    RETURN     all  --  0.0.0.0/0            0.0.0.0/0           

Chain DOCKER-USER (1 references)
num  target     prot opt source               destination         
1    RETURN     all  --  0.0.0.0/0            0.0.0.0/0        
EN

回答 2

Stack Overflow用户

发布于 2019-01-02 16:28:01

解决方案:

代码语言:javascript
运行
复制
echo 'listen-address  172.17.0.1:8118' > /usr/local/etc/privoxy/config 
service privoxy restart
netstat -nltp|grep 8118
tcp        0      0 172.17.0.1:8118         0.0.0.0:*               LISTEN      27154/privoxy       
tcp        0      0 127.0.0.1:8118          0.0.0.0:*               LISTEN      27154/privoxy

ps:我犯了一个很低级的错误,我需要学习这个系统的原理,我有很多误解,一个服务是不能说能够ping ip的可以使用的,端口的作用是用来监听和接受数据包的,pingPassing只代表tcp端口监听没有问题(可能是0.0.0.0),但是第三方服务必须要注意。

票数 1
EN

Stack Overflow用户

发布于 2019-01-01 18:24:05

如果可以,yum install strace。然后再次运行wget,将strace放在前面。所以,

strace -f wget .

查找系统级故障消息。不要忘记wget --debug也是如此。

另一件要考虑的事情是,这些示例中的每个示例都是以什么用户身份运行的。当你直接运行wget时,他们是不同的用户吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53994670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档