docker网络模式

总体介绍

graphic

docker网络的几种模式

  • bridge模式:使用--network bridge指定,默认使用docker0
  • host模式:使用--network host指定
  • none模式:使用--network none指定
  • container模式:使用--network container:NAME或者容器ID指定

容器实例内默认网络IP生产规则

最常用的是brige,每一个容器都分配、设置ip等

查看配置

docker inspect cc5b5241882b

root@ubuntu:/# docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED         STATUS                          PORTS     NAMES
cc5b5241882b   ubuntu             "/bin/bash"              2 minutes ago   Up 2 minutes                              clever_bhaskara
7f569561ca8b   centos             "/bin/bash"              25 hours ago    Up 25 hours                               adoring_yalow
c6b2169449f4   ubuntu             "bash"                   3 days ago      Up 26 hours                               ubuntu2
f19e7fbc3d18   v2fly/v2fly-core   "/usr/bin/v2ray -con…"   3 days ago      Restarting (2) 36 seconds ago             tcp
root@ubuntu:/# docker inspect cc5b5241882b|tail -n 20
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "ec8278ac6963d2f67b27468ab0dc576299afa9b3031363db4af8669dcd8fcb8f",
                    "EndpointID": "772735b884867983a80845cc7c38d14b6d0aeec4df4e8c471ab245290eade275",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:04",
                    "DriverOpts": null
                }
            }
        }
    }
]
root@ubuntu:/# docker inspect c6b2169449f4 |tail -n 20
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "ec8278ac6963d2f67b27468ab0dc576299afa9b3031363db4af8669dcd8fcb8f",
                    "EndpointID": "7aee530f602f7a61405e5e9a78df90e7a4f3130bd9368fe5d4f7452fe3581663",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

IPAddress是人人一份,相互独立的

docker rm -f cc5b5241882b
docker run -it --name ubuntu bash

再看ip,"IPAddress": "172.17.0.4"

root@ubuntu:/# docker inspect f54ae7bf8dfa |tail -n 20
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "ec8278ac6963d2f67b27468ab0dc576299afa9b3031363db4af8669dcd8fcb8f",
                    "EndpointID": "420339ad649a5470ddc54765644a9829752f7021fc5a7685db88b60f89921b8a",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:04",
                    "DriverOpts": null
                }
            }
        }
    }
]

!!! : docker容器内部的ip是有可能会发生改变的

我们在平常使用的时候注意不能固定,这是很危险的