Docker中的网络

1.查看网卡

docker network ls

会看见三个默认的网卡

NETWORK ID     NAME          DRIVER    SCOPE
8a6b9600d3a9   bridge        bridge    local
6ae7e28fa2fb   host          host      local
a548af0a93b8   none          null      local

2.容器通信
容器之所以可以和宿主机以及其他容器进行通信是因为有一个默认参数指定了网卡bridge

--net bridge

但是默认的网卡有弊端:默认不能使用域名/容器名称进行通信
3.创建一个自定义网卡
–driver bridge指定是桥接模式
–subnet 配置子网(CIDER划分)
–gateway 配置网关
最后指定网卡名称

docker network create --driver bridge --subnet 192.168.89.0/24 --gateway 192.168.89.1 my-gitlab-net

创建成功后可以查看网卡详细信息

mk@mk-pc:~/Dev/gitlab$ docker inspect my-gitlab-net
[
    {
        "Name": "my-gitlab-net",
        "Id": "0205d8e6370fd65a853fc087470c62db18b90cc4a84c716a52b97655e083afd4",
        "Created": "2021-10-31T21:10:11.502466301+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.88.0/24",
                    "Gateway": "192.168.88.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

使用的时候使用 --net来指定docker的网卡

docker run -id -p 7788:8080 --name my-tomcat001 --net my-gitlab-net tomcat
docker run -id -p 7789:8080 --name my-tomcat002 --net my-gitlab-net tomcat

进入到 01 容器 ping 第二个

root@55e375dad92a:/usr/local/tomcat# ping my-tomcat002
PING my-tomcat002 (192.168.88.3) 56(84) bytes of data.
64 bytes from my-tomcat002.my-gitlab-net (192.168.88.3): icmp_seq=1 ttl=64 time=0.492 ms
64 bytes from my-tomcat002.my-gitlab-net (192.168.88.3): icmp_seq=2 ttl=64 time=0.137 ms
64 bytes from my-tomcat002.my-gitlab-net (192.168.88.3): icmp_seq=3 ttl=64 time=0.160 ms
64 bytes from my-tomcat002.my-gitlab-net (192.168.88.3): icmp_seq=4 ttl=64 time=0.058 ms
64 bytes from my-tomcat002.my-gitlab-net (192.168.88.3): icmp_seq=5 ttl=64 time=0.137 ms
64 bytes from my-tomcat002.my-gitlab-net (192.168.88.3): icmp_seq=6 ttl=64 time=0.121 ms
上一篇:每日一题 - 剑指 Offer 33. 二叉搜索树的后序遍历序列


下一篇:二叉搜索树的后序遍历序列