Docker容器互连-自定义网络

本文介绍了如何在Docker中创建自定义网络,包括查看网络、创建网络及容器连接。通过自定义网络,容器能直接使用主机名进行通信,而不同网络的容器之间需要通过`connect`命令加入同一网络才能互通。
摘要由CSDN通过智能技术生成

查看docker所有网络

[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
c6cccbeca026        bridge              bridge              local
ef2aaeb91b95        host                host                local
9b941aa3bfdf        none                null                local

DRIVER
bridge :桥接模式(docker默认)
host:和宿主机共享网络
none:不配置网络

使用命令docker network create来创建网络。创建的网络中默认使用的是桥接模式,–subnet输入子网段,–gateway输入子网的网关地址。创建成功后,可以使用docker network ls查看到网络。

[root@localhost ~]# docker network create --help

Usage:  docker network create [OPTIONS] NETWORK

Create a network

Options:
      --attachable             Enable manual container attachment
      --aux-address map        Auxiliary IPv4 or IPv6 addresses used by Network driver (default map[])
  -d, --driver string          Driver to manage the Network (default "bridge")
      --gateway stringSlice    IPv4 or IPv6 Gateway for the master subnet
      --help                   Print usage
      --internal               Restrict external access to the network
      --ip-range stringSlice   Allocate container ip from a sub-range
      --ipam-driver string     IP Address Management Driver (default "default")
      --ipam-opt map           Set IPAM driver specific options (default map[])
      --ipv6                   Enable IPv6 networking
      --label list             Set metadata on a network (default [])
  -o, --opt map                Set driver specific options (default map[])
      --subnet stringSlice     Subnet in CIDR format that represents a network segment
[root@localhost ~]# docker network create --subnet 192.168.3.0/24 --gateway 192.168.3.1 mynet
3013d0f266f4e243fd7af37c8c8e3d60b6a75c1744a5aa8b8c6cb05a5d25220c
[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
c6cccbeca026        bridge              bridge              local
ef2aaeb91b95        host                host                local
3013d0f266f4        mynet               bridge              local
9b941aa3bfdf        none                null                local

使用docker network inspect 网络查看详细信息。此处可以看到该网络的网段和网关。目前刚创建,并没有容器被使用。

[root@localhost ~]# docker network inspect mynet 
[
    {
   
        "Name": "mynet",
        "Id": "3013d0f266f4e243fd7af37c8c8e3d60b6a75c1744a5aa8b8c6cb05a5d25220c",
        "Created": "2021-05-19T05:20:32.230092567-04:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
   
            "Driver": "default",
            "Options": {
   },
            "Config": [
                {
   
                    "Subnet": "192.168.3.0/24",
                    "Gateway": "192.168.3.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
   },
        "Options": {
   },
        "Labels": {
   }
    }
]

运行容器,使用自定义网络mynet。

[root@localhost ~]# docker run -dit --name centosnet1 --network mynet centos
2688d2a9c1487769e223d6f0d8c834197044ff2607e2896aa12d53a0dc0e8279
[root@localhost ~]# docker run -dit --name centosnet2 --network mynet centos
bdf99b0c14809435732551fa375ad77809f62fcdf5b01659ed741451cd41e223
[root@localhost ~]# docker network inspect mynet 
[
    {
   
        "Name": "mynet",
        "Id": "3013d0f266f4e243fd7af37c8c8e3d60b6a75c1744a5aa8b8c6cb05a5d25220c",
        "Created": 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值