Linux下Docker容器访问宿主机网络

0.背景

Docker的基本使用

docker启动时,如果创建了自定义的docker网络:

将容器run在该网络中时,容器访问的localhost不是宿主机的localhost.

1.解决

1.1 启动时该为host网络模式启动

--network host

示例

# host网络模式,无需映射端口. (\表示换行)
docker run -d --name konga --network host        -e "NODE_ENV=production"          -e "DB_ADAPTER=mysql"         -e "DB_URI=mysql://root:123456@172.18.0.1:5432/konga"         pantsel/konga

1.2 修改localhost为docker网络的ip

1.2.1 查看docker下的网络

# 查看docker网络信息
docker network ls

# 控制台显示
# DRIVER参数代表网络类型.
NETWORK ID     NAME            DRIVER    SCOPE
e34f9fddb6bd   bridge          bridge    local
e3e320be538b   host            host      local
8a1dd9a77350   none            null      local
e01f2aaa9bf5   test_network    bridge    local
cbb7bbd9dd54   test_network2   bridge    local

1.2.2 查看指定网络的信息

  • 查看docker0

Docker启动的时候会在主机上自动创建一个docker0网络,实际上是一个Linux网桥.

如果在docker run的时候,没有指定网络模式时.

此时都会挂载到docker0网桥上,这样容器就可以和主机甚至是其他容器之间通讯了.

# 查看docker0的ip信息
ip addr show docker0

# 控制台显示
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:8c:08:a0:f9 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:8cff:fe08:a0f9/64 scope link 
       valid_lft forever preferred_lft forever
  • 查看test_network
# docker中查看test_network网络的信息
docker network  ls | grep test

# 控制台显示
e01f2aaa9bf5   test_network    bridge    local
cbb7bbd9dd54   test_network2   bridge    local

# 其实就是NETWORKID加上了个br
ip addr show br-e01f2aaa9bf5

# 控制台显示
16: br-e01f2aaa9bf5: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:44:31:54:fc brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.1/16 brd 172.18.255.255 scope global br-e01f2aaa9bf5
       valid_lft forever preferred_lft forever

1.2.3 查看所有网络信息

# 查看所有网络信息
ifconfig

# 控制台显示
br-cbb7bbd9dd54: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.19.0.1  netmask 255.255.0.0  broadcast 172.19.255.255
        ether 02:42:aa:cb:98:a6  txqueuelen 0  (Ethernet)
        RX packets 9922240  bytes 4198752081 (3.9 GiB)
        RX errors 0  dropped 404  overruns 0  frame 0
        TX packets 10043689  bytes 2152701308 (2.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br-e01f2aaa9bf5: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:44:31:54:fc  txqueuelen 0  (Ethernet)
        RX packets 1654739  bytes 284217999 (271.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1654739  bytes 284217999 (271.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:8cff:fe08:a0f9  prefixlen 64  scopeid 0x20<link>
        ether 02:42:8c:08:a0:f9  txqueuelen 0  (Ethernet)
        RX packets 650  bytes 156927 (153.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 736  bytes 105419 (102.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1452
        inet 10.23.143.148  netmask 255.255.0.0  broadcast 10.23.255.255
        inet6 fe80::5054:ff:fe73:7e08  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:73:7e:08  txqueuelen 1000  (Ethernet)
        RX packets 9922240  bytes 4198752081 (3.9 GiB)
        RX errors 0  dropped 404  overruns 0  frame 0
        TX packets 10043689  bytes 2152701308 (2.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1654739  bytes 284217999 (271.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1654739  bytes 284217999 (271.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Linux下Docker容器访问宿主机网络

上一篇:Clickhouse升级


下一篇:Python练习task6:字典与集合