1. 程式人生 > >docker之容器網絡篇

docker之容器網絡篇

支持 status tro afa 主機名 nta creat ipa idg

一、docker網絡模式

Docker支持五種網絡模式:
A、bridge
--net=bridge
默認網絡,Docker啟動後創建一個docker0網橋,默認創建的容器也是添加到這個網橋中。
B、host
--net=host
容器不會獲得一個獨立的network namespace,而是與宿主機共用一個。這就意味著容器不會有自己的網卡信息,而是使用宿主機的。容器除了網絡,其他都是隔離的。
C、none
--net=none
獲取獨立的network namespace,但不為容器進行任何網絡配置,需要我們手動配置。
D、container
--net=container:Name/ID
與指定的容器使用同一個network namespace,具有同樣的網絡配置信息,兩個容器除了網絡,其他都還是隔離的。

E、自定義網絡
與默認的bridge原理一樣,但自定義網絡具備內部DNS發現,可以通過容器名或者主機名容器之間網絡通信。

二、實例演示

自定義網絡:

[root@localhost ~]# docker network create hello
c1ee291139357510355527cdbe44d625311d0ed4cb35828fb1d9d37fdfdca973

[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
881d091f073a        bridge              bridge              local
c1ee29113935        hello               bridge              local
8dcf04c946c5        host                host                local
d21578b3d24e        none                null                local
[root@localhost ~]# docker container run -itd --name=bs1 --net=hello busybox
Unable to find image ‘busybox:latest‘ locally
latest: Pulling from library/busybox
8c5a7da1afbc: Pull complete 
Digest: sha256:032ddd66f10483436e8a252e69fdfd20d0164e9953585c10d378183a0924db34
Status: Downloaded newer image for busybox:latest
a0d322ed58a7c0f0e38fa2405446bae4c0ac7208264a0f273f447ee0ec47c4cc
[root@localhost ~]#  docker container run -itd --name=bs2 --net=hello busybox 
ddfcb6a9f615c445a9fbd158dd339dd7ad1bc94e00197dd3224d45ab2bf0f04e
[root@localhost ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
ddfcb6a9f615        busybox             "sh"                     24 seconds ago      Up 23 seconds                            bs2
a0d322ed58a7        busybox             "sh"                     51 seconds ago      Up 50 seconds                            bs1
[root@localhost ~]# docker exec -it bs2 sh
~ # ping bs1
PING bs1 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.191 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.120 ms
64 bytes from 172.18.0.2: seq=2 ttl=64 time=0.119 ms
64 bytes from 172.18.0.2: seq=3 ttl=64 time=0.122 ms
~ # ping bs2
PING bs2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.069 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.086 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.080 ms

創建的兩個容器都加入了hello網絡,並且可以通過主機名通信。
通過inspect查看hello網絡詳細信息:

[root@localhost ~]# docker network inspect hello
[
    {
        "Name": "hello",
        "Id": "c1ee291139357510355527cdbe44d625311d0ed4cb35828fb1d9d37fdfdca973",
        "Created": "2018-08-06T04:23:28.286872431+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "a0d322ed58a7c0f0e38fa2405446bae4c0ac7208264a0f273f447ee0ec47c4cc": {
                "Name": "bs1",
                "EndpointID": "6fd7afaa55e71edb72f4ed5c22b792cfdc5fcb2aee2acd683c178f93503fcb8d",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            },
            "ddfcb6a9f615c445a9fbd158dd339dd7ad1bc94e00197dd3224d45ab2bf0f04e": {
                "Name": "bs2",
                "EndpointID": "b2a0abdcc73e66b02b737c4960f1b85844472827c99a93be2e3a55dd9629da83",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

docker之容器網絡篇