docker 创建容器时指定容器ip
Posted navysummer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker 创建容器时指定容器ip相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章