码迷,mamicode.com
首页 > 其他好文 > 详细

docker 创建容器时指定容器ip

时间:2020-01-30 19:32:34      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:ever   cbe   搭建集群   自己的   options   查看   glob   option   bridge   

Docker创建容器时默认采用bridge网络,自行分配ip,不允许自己指定。

在实际部署中,我们需要指定容器ip,不允许其自行分配ip,尤其是搭建集群时,固定ip是必须的。

我们可以创建自己的bridge网络 : mynet,创建容器的时候指定网络为mynet并指定ip即可。

查看网络模式

navy@DEEPIN:~/Desktop$ sudo docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
93c432885f6f        bridge              bridge              local
284eae12c833        host                host                local
1b6b1ef2c3d7        none                null                local

 创建一个新的bridge网络

navy@DEEPIN:~/Desktop$ sudo docker network create --driver bridge --subnet=172.18.12.0/16 --gateway=172.18.1.1 mynet
efcbc104e9d14f2c1ae79ad17185f5de14f08f2d7a688642e9483370174221fb

 查看网络信息

navy@DEEPIN:~/Desktop$ sudo docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "efcbc104e9d14f2c1ae79ad17185f5de14f08f2d7a688642e9483370174221fb",
        "Created": "2020-01-30T17:58:41.05151242+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.12.0/16",
                    "Gateway": "172.18.1.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

 创建容器并指定容器ip

navy@DEEPIN:~/Desktop$ sudo docker run -e TZ="Asia/Shanghai" --privileged -itd -h hadoop01.com --name hadoop01 --network=mynet --ip 172.18.12.1 centos /usr/sbin/init
c8558c3b358a97df5b6d0e4deecc81c8102a179a7742878d7cb4b6034d8cbeaa

 运行容器

navy@DEEPIN:~/Desktop$ sudo docker exec -it hadoop01 /bin/bash
[root@hadoop01 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:12:0c:01 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.18.12.1/16 brd 172.18.255.255 scope global eth0
       valid_lft forever preferred_lft forever

  

docker 创建容器时指定容器ip

标签:ever   cbe   搭建集群   自己的   options   查看   glob   option   bridge   

原文地址:https://www.cnblogs.com/navysummer/p/12243350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!