使用IP而不是localhost连接到docker

时间:2019-09-20 07:55:34

标签: docker

我在Windows 10 pc中运行一个docker容器(运行一个我无法修改网络设置的cloudera映像),我想使用分配的IP(即172.17.0.2)从浏览器连接到它)而不是localhost。 有办法吗?怎么样?

(我发现this post,并尝试使用--net = host选项,但分配的docker0 IP仍为172.17.0.1。)

在“ ifconfig”输出下面:

[root@quickstart /]# ifconfig
cni0      Link encap:Ethernet  HWaddr 6A:6A:D3:6A:CB:82
      inet addr:10.1.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
      inet6 addr: fe80::686a:d3ff:fe6a:cb82/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:12920 errors:0 dropped:0 overruns:0 frame:0
      TX packets:13154 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:884288 (863.5 KiB)  TX bytes:3946860 (3.7 MiB)

docker0   Link encap:Ethernet  HWaddr 02:42:65:2D:65:6C
      inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0
      inet6 addr: fe80::42:65ff:fe2d:656c/64 Scope:Link
      UP BROADCAST MULTICAST  MTU:1500  Metric:1
      RX packets:154 errors:0 dropped:0 overruns:0 frame:0
      TX packets:215 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:10104 (9.8 KiB)  TX bytes:185370 (181.0 KiB)

docker_gwbridge Link encap:Ethernet  HWaddr 02:42:FF:2D:D6:CD
      inet addr:172.18.0.1  Bcast:172.18.255.255  Mask:255.255.0.0
      inet6 addr: fe80::42:ffff:fe2d:d6cd/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:339368 errors:0 dropped:0 overruns:0 frame:0
      TX packets:339368 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:63421670 (60.4 MiB)  TX bytes:63421670 (60.4 MiB)

eth0      Link encap:Ethernet  HWaddr 02:50:00:00:00:01
      inet addr:192.168.65.3  Bcast:192.168.65.15  Mask:255.255.255.240
      inet6 addr: fe80::50:ff:fe00:1/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:131024 errors:0 dropped:0 overruns:0 frame:0
      TX packets:76313 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:157839066 (150.5 MiB)  TX bytes:8175888 (7.7 MiB)

hvint0    Link encap:Ethernet  HWaddr 00:15:5D:CD:11:09
      inet addr:10.0.75.2  Bcast:0.0.0.0  Mask:255.255.255.0
      inet6 addr: fe80::215:5dff:fecd:1109/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:257 errors:0 dropped:53 overruns:0 frame:0
      TX packets:17 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:21003 (20.5 KiB)  TX bytes:1358 (1.3 KiB)

lo        Link encap:Local Loopback
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:65536  Metric:1
      RX packets:339368 errors:0 dropped:0 overruns:0 frame:0
      TX packets:339368 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1
      RX bytes:63421670 (60.4 MiB)  TX bytes:63421670 (60.4 MiB)

veth535bb6e Link encap:Ethernet  HWaddr 16:E5:EC:F3:76:BC
      inet6 addr: fe80::14e5:ecff:fef3:76bc/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
      TX packets:31 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:0 (0.0 b)  TX bytes:2318 (2.2 KiB)

vethc291b186 Link encap:Ethernet  HWaddr 72:3A:FE:9F:40:7A
      inet6 addr: fe80::703a:feff:fe9f:407a/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:6296 errors:0 dropped:0 overruns:0 frame:0
      TX packets:6441 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:518245 (506.0 KiB)  TX bytes:1959228 (1.8 MiB)

vethc0969f7 Link encap:Ethernet  HWaddr F2:9E:81:04:9B:69
      inet6 addr: fe80::f09e:81ff:fe04:9b69/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:74394 errors:0 dropped:0 overruns:0 frame:0
      TX packets:129442 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:8037110 (7.6 MiB)  TX bytes:157544590 (150.2 MiB)

vethdba5d116 Link encap:Ethernet  HWaddr 26:D1:6D:9B:28:8E
      inet6 addr: fe80::24d1:6dff:fe9b:288e/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:6262 errors:0 dropped:0 overruns:0 frame:0
      TX packets:6457 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:516002 (503.9 KiB)  TX bytes:1960294 (1.8 MiB)

vethf1ece51f Link encap:Ethernet  HWaddr 72:A3:6A:C9:70:37
      inet6 addr: fe80::70a3:6aff:fec9:7037/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:362 errors:0 dropped:0 overruns:0 frame:0
      TX packets:351 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:30921 (30.1 KiB)  TX bytes:34060 (33.2 KiB)

1 个答案:

答案 0 :(得分:1)

我想您对docker网络有些误解。

Docker使用veth pair作为默认的桥接技术,在主机和容器之间进行通信,请参见下图:

https://img-blog.csdn.net/20180820162528508?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hid19saW51eDEyMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70

因此,172.17.0.1只是主机的网卡和容器的网卡之间的桥梁,您不能直接使用它。同时,您不应使用主机浏览器中的容器ip来访问它,因为它位于docker设置的专用网络中,您可以使用-p将内部网络服务发布到外部。

而且,当您访问localhost时,它实际上只是访问主机的网络,而docker使用iptables通过docker0网桥将您的网络流量传递到容器的端口。因此,对于您的情况,如果您不想使用localhost,则可以使用主机的IP,在您的情况下为192.168.65.3