Docker 自定义网络

查看所有的docker网

在这里插入图片描述
网络模式:
bridge 桥接 (docker 默认)
none 不配置网络
host 和宿主机共用网络
container 容器网络连通(用的少,局限很大)

测试

#原来我们是直接启动 默认--net bridge  就是我们的docker 0
docker run -d -P --name tomcat01  tomcat:8.0
#自定义网络
[root@iZwz9f559fa6a275jj2x0sZ ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
1e22b7050b9cbb6f590d7632abb8c390dd4fb7f9c2319a73193089c7b45eeff9
[root@iZwz9f559fa6a275jj2x0sZ ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
78c33e968117   bridge    bridge    local
3b4029a38262   host      host      local
1e22b7050b9c   mynet     bridge    local
247e57f5aa94   none      null      local


在这里插入图片描述

[root@iZwz9f559fa6a275jj2x0sZ ~]# docker run -d -P --name tomcat_net_01 --net mynet tomcat:8.0
337a091ed51ddc7152b342da5ed73dbbcaa41fccfd1cc9204e5565f08c8f4ba1
[root@iZwz9f559fa6a275jj2x0sZ ~]# docker run -d -P --name tomcat_net_02 --net mynet tomcat:8.0
54f287b94940e25bd1930be1edafdbf0f6c0629e9434c1602415545cc3e1e700
#mynet Containers 包括了tomcat_net_01 ,tomcat_net_02 
[root@iZwz9f559fa6a275jj2x0sZ ~]# docker network inspect mynet 
[
    {
        "Name": "mynet",
        "Id": "1e22b7050b9cbb6f590d7632abb8c390dd4fb7f9c2319a73193089c7b45eeff9",
        "Created": "2022-05-07T18:27:39.558567762+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "337a091ed51ddc7152b342da5ed73dbbcaa41fccfd1cc9204e5565f08c8f4ba1": {
                "Name": "tomcat_net_01",
                "EndpointID": "fd0805eac92fd65ceb3aba76c6423876ccd1b2bd5f566b9e4e6e4918d5e60ea1",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            },
            "54f287b94940e25bd1930be1edafdbf0f6c0629e9434c1602415545cc3e1e700": {
                "Name": "tomcat_net_02",
                "EndpointID": "a5ea382e9151f898ca027c6d845f8b36fa533bd4000b5b7aa55ecda6561391aa",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

#不使用--link 也可以ping 通了

[root@iZwz9f559fa6a275jj2x0sZ ~]# docker exec -it tomcat_net_01 ping tomcat_net_02
PING tomcat_net_02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat_net_02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.092 ms
64 bytes from tomcat_net_02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.082 ms
64 bytes from tomcat_net_02.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.077 ms
[root@iZwz9f559fa6a275jj2x0sZ ~]# docker exec -it tomcat_net_02 ping tomcat_net_01
PING tomcat_net_01 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat_net_01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.075 ms
64 bytes from tomcat_net_01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.084 ms

自定义网络可以不同的集群使用不同的网络

redis,mysql

在这里插入图片描述
在这里插入图片描述
测试:打通 tomcat01 mynet

docker network connect mynet tomcat01
#tomcat01 可以ping通tomcat_net_01 了
[root@iZwz9f559fa6a275jj2x0sZ ~]# docker exec -it tomcat01 ping tomcat_net_01 
PING tomcat_net_01 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat_net_01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.094 ms
64 bytes from tomcat_net_01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.082 ms
64 bytes from tomcat_net_01.mynet (192.168.0.2): icmp_seq=3 ttl=64 time=0.093 ms
64 bytes from tomcat_net_01.mynet (192.168.0.2): icmp_seq=4 ttl=64 time=0.069 ms
64 bytes from tomcat_net_01.mynet (192.168.0.2): icmp_seq=5 ttl=64 time=0.081 ms
64 bytes from tomcat_net_01.mynet (192.168.0.2): icmp_seq=6 ttl=64 time=0.091 ms
64 bytes from tomcat_net_01.mynet (192.168.0.2): icmp_seq=7 ttl=64 time=0.085 ms

直接把tomcat01 加到了mynet 里
一个容器两个ip
在这里插入图片描述
结论:假设需要跨网段操作别人可以使用docker network connec 连通

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值