docker 创建自定义网络,固定ip地址

1 查看容器镜像

[root@bogon ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7.6.1810            f1cb7c7d58b7        15 months ago       202MB

2、创建网络,指定网段

创建网络,指定网段
[root@lxy ~]# docker network create --subnet 10.10.88.0/16 mynetwork
cdd7507bb1a1564e06defa52c5ca3c35be19e11bb9b76497772892e88db4559f

启动容器,分配固定ip地址  22
[root@lxy ~]# docker run -it --net mynetwork --ip 10.10.88.22 --name centos centos:7.6.1810  bash

进入容器查看ip地址
[root@2a49bbb1e185 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.88.22  netmask 255.255.0.0  broadcast 10.10.255.255
        ether 02:42:0a:0a:58:16  txqueuelen 0  (Ethernet)
        RX packets 1444  bytes 10165863 (9.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1376  bytes 95060 (92.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3、查看网络名称

容器开着不退出,重新开一个终端,模拟报错,回到宿主机
查看网络名称
[root@bogon ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
a7a747e47ee3        bridge              bridge              local
cdd7507bb1a1        mynetwork           bridge              local

4、删除网络

容器开着不退出,重新开一个终端,模拟报错,回到宿主机

很显然直接删除报错,原因容器没有退出,正在运行,
[root@bogon ~]# docker network rm mynetwork
Error response from daemon: error while removing network: network mynetwork id d29a5e9a218e642951b73c74c90f74ecb412cf1029d7c61032f10f765e115280 has active endpoints
[root@bogon ~]# docker network inspect mynetwork
[
    {
	        "Name": "mynetwork",  ###参数一
        "Id": "d29a5e9a218e642951b73c74c90f74ecb412cf1029d7c61032f10f765e115280",
        "Created": "2020-06-28T23:52:33.151347426-04:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "10.10.88.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "246fe8f6c38a2790d06a726fd768037116f1381c6836254277f7d848040ffa06": {
                "Name": "centos",  ###参数二
                "EndpointID": "0e72ebe0fb7ef7fa4a2397f75298062adcbe158e5280f5aa837d435cb41b5324",
                "MacAddress": "02:42:0a:0a:58:17",
                "IPv4Address": "10.10.88.23/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
格式
docker network disconnect -f 参数一名称 参数二名称
docker network disconnect -f mynetwork centos
docker network rm mynetwork 
在去查看没有mynetwork 网络,删除成功
[root@bogon ~]# docker network ls 

你可能感兴趣的:(docker)