docker之容器网络篇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker之容器网络篇相关的知识,希望对你有一定的参考价值。

一、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发现,可以通过容器名或者主机名容器之间网络通信。

二、实例演示

自定义网络:

[[email protected] ~]# docker network create hello
c1ee291139357510355527cdbe44d625311d0ed4cb35828fb1d9d37fdfdca973

[[email protected] ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
881d091f073a        bridge              bridge              local
c1ee29113935        hello               bridge              local
8dcf04c946c5        host                host                local
d21578b3d24e        none                null                local
[[email protected] ~]# 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
[[email protected] ~]#  docker container run -itd --name=bs2 --net=hello busybox 
ddfcb6a9f615c445a9fbd158dd339dd7ad1bc94e00197dd3224d45ab2bf0f04e
[[email protected] ~]# 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
[[email protected] ~]# 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网络详细信息:

[[email protected] ~]# 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之容器网络篇的主要内容,如果未能解决你的问题,请参考以下文章

Docker容器网络-实现篇

Docker开篇之基础概念篇

容器技术之Docker数据卷

容器技术之Docker数据卷

测试开发之系统篇-Docker容器安装

docker 容器网络篇