docker 默认有4种网络模式,详细可查看我曾经写的 "docker几种网络模式"
docker run 新建并启动容器时,不指定网络,默认是使用桥接模式,容器内部IP是通过DHCP获取,容器网桥名为docker0,docker容器重启后,其容器IP会变化。有没有办法来固定容器内的IP呢?答案是肯定的。可以使用脚本或工具来固定容器内部IP。
一、如何查看容器内部IP?:
方法一:
通过docker inspect查看容器的详细信息来查看,可以使用grep来匹配查询
[root@localhost ~]# docker inspect centos7 | grep -aiE ipaddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
[root@localhost ~]# docker inspect centos7 | grep -aiE ipaddress | tail -1
"IPAddress": "172.17.0.2",
[root@localhost ~]# docker inspect centos7 | grep -aiE ipaddress | tail -1 | grep -aiowE "([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3})"
172.17.0.2
[root@localhost ~]#
[root@localhost ~]# docker inspect centos7 | grep -aiE ipaddress | tail -1 | grep -aiowE "([0-9]{1,3}\.){3}[0-9]{1,3}"
172.17.0.2
[root@localhost ~]#
方法二:
通过docker exec 来查看
[root@localhost ~]# docker exec centos7 ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 15026 bytes 35185160 (33.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9009 bytes 503286 (491.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]#
方法三
docker exec登录容器内查看
[root@localhost ~]# docker exec -it centos7 /bin/bash
[root@b7f8b1907403 ansible]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 15026 bytes 35185160 (33.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9009 bytes 503286 (491.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@b7f8b1907403 ansible]#
二、固定容器内部IP
1、使用pipework工具
[root@localhost src]# ls
debug default.conf harbor jforum-2.5.0.war kernels nginx.conf pipework
[root@localhost src]# mv pipework /usr/local/bin
[root@localhost src]# pipework -h
-bash: /usr/local/bin/pipework: 权限不够
[root@localhost src]# chmod +x /usr/local/bin/pipework
[root@localhost src]# pipework -h
Syntax:
pipework <hostinterface> [-i containerinterface] [-l localinterfacename] [-a addressfamily] <guest> <ipaddr>/<subnet>[@default_gateway] [macaddr][@vlan]
pipework <hostinterface> [-i containerinterface] [-l localinterfacename] <guest> dhcp [macaddr][@vlan]
pipework mac:<hostinterface_macaddress> [-i containerinterface] [-l localinterfacename] [-a addressfamily] <guest> <ipaddr>/<subnet>[@default_gateway] [macaddr][@vlan]
pipework mac:<hostinterface_macaddress> [-i containerinterface] [-l localinterfacename] <guest> dhcp [macaddr][@vlan]
pipework route <guest> <route_command>
pipework rule <guest> <rule_command>
pipework tc <guest> <tc_command>
pipework --wait [-i containerinterface]
[root@localhost src]#
把容器内部IP固定为:172.17.0.2
[root@localhost src]# pipework docker0 centos7 172.17.0.2/16@172.17.0.1
[root@localhost src]#
docker0为网桥名。容器内网桥名。172.17.0.1为容器的网关。
重启容器确认IP变化
[root@localhost src]# docker restart centos7
centos7
[root@localhost src]# docker inspect centos7 | grep -aiE ipaddress | tail -1
"IPAddress": "172.17.0.2",
[root@localhost src]#
[root@localhost src]# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.114 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.192 ms
^C
--- 172.17.0.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 0.114/0.153/0.192/0.039 ms
[root@localhost src]#
发现,多次重启容器,IP始终为172.17.0.2不再变化。