共计 7579 个字符,预计需要花费 19 分钟才能阅读完成。
Docker 容器固定 IP 分配
我们在使用 docker run 创建 Docker 容器时,可以用 –net 选项指定容器的网络模式,Docker 有以下 4 种网络模式:
1. host 模式,使用 –net=host 指定。
2. container 模式,使用 –net=container:NAME_or_ID 指定。
3. none 模式,使用 –net=none 指定。
4. bridge 模式,使用 –net=bridge 指定,默认设置。
默认选择 bridge 的情况下,容器启动后会通过 DHCP 获取一个地址,这可能不是我们想要的,本文介绍在 Ubuntu docker 环境下使用 pipework 脚本对容器分配固定 IP。
Pipework 是由 Docker 的一个工程师设计的解决方案,它让容器能够在“任意复杂的场景”下进行连接。Pipework 是 Docker 的一个网络功能增强插件,它使用了 cgroups 和 namespacpace。
但是容器停止后在启动 ip 就的重新指定。
# step1: install plug
wget https://github.com/jpetazzo/pipework/archive/master.zip
unzip master.zip
cp pipework-master/pipework /usr/local/bin/
chmod +x /usr/local/bin/pipework
# step2: get bridge name and set other contains
brctl show |awk ‘NR==2{print $1}’
#brctl show #detail
# step3: example set fixed ip
pipework docker0 test1 172.17.0.20/24@172.17.0.1
# | | | | | | |_________ docker 容器镜像内的 gateway(以后可以划分 vlan)
# | | | | | |
# | | | | | |_____________ 固定字符
# | | | | |
# | | | | |_______________ docker 容器 ip 的子网
# | | | |
# | | | |______________________ docker 容器内网 IP
# | | |
# | | |________________________________ docker 容器的名字
# | |
# | |_______________________________________ 网桥的 name
# |
# |________________________________________________ 固有命令
root@ubuntu:/home/leco# cat /etc/issue
Ubuntu 16.10 \n \l
root@ubuntu:/home/leco# docker images |grep CentOS
centos latest a8493f5f50ff 2 weeks ago 192 MB
jdeathe/centos-ssh latest 841c208badec 2 months ago 214 MB
root@ubuntu:/home/leco# docker run -idt –net=none –name test1 centos /bin/bash
26079d0851aeed3f2dd4eb93e1db1943ac5baebebedafe16ed2d913ceaae1723
root@ubuntu:/home/leco# pipework docker0 test1 172.17.0.20/24@172.17.0.1
root@ubuntu:/home/leco# docker exec -it 26079d0851a bash
[root@26079d0851ae /]# yum install -y net-tools
Loaded plugins: fastestmirror, ovl
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/4): base/7/x86_64/group_gz | 155 kB 00:00:00
(2/4): base/7/x86_64/primary_db | 5.6 MB 00:00:01
(3/4): extras/7/x86_64/primary_db | 151 kB 00:00:02
(4/4): updates/7/x86_64/primary_db | 4.8 MB 00:00:11
Determining fastest mirrors
* base: mirrors.cn99.com
* extras: mirrors.cn99.com
* updates: mirrors.163.com
Resolving Dependencies
–> Running transaction check
—> Package net-tools.x86_64 0:2.0-0.17.20131004git.el7 will be installed
–> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================================================
Installing:
net-tools x86_64 2.0-0.17.20131004git.el7 base 304 k
Transaction Summary
=======================================================================================================================================================================
Install 1 Package
Total download size: 304 k
Installed size: 917 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.17.20131004git.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for net-tools-2.0-0.17.20131004git.el7.x86_64.rpm is not installed
net-tools-2.0-0.17.20131004git.el7.x86_64.rpm | 304 kB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : “CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>”
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-3.1611.el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : net-tools-2.0-0.17.20131004git.el7.x86_64 1/1
Verifying : net-tools-2.0-0.17.20131004git.el7.x86_64 1/1
Installed:
net-tools.x86_64 0:2.0-0.17.20131004git.el7
Complete!
[root@26079d0851ae /]# ifconfig
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.20 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::d022:6ff:feef:f089 prefixlen 64 scopeid 0x20<link>
ether d2:22:06:ef:f0:89 txqueuelen 1000 (Ethernet)
RX packets 7309 bytes 11981498 (11.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5611 bytes 432934 (422.7 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
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (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
# step restart this container
root@ubuntu:/home/leco# docker stop 26079d0851ae
26079d0851ae
root@ubuntu:/home/leco# docker start 26079d0851ae
26079d0851ae
root@ubuntu:/home/leco# docker exec -it 26079d0851ae bash
[root@26079d0851ae /]# ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (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@ubuntu:/home/leco# pipework docker0 test1 172.17.0.20/24@172.17.0.1
root@ubuntu:/home/leco# docker exec -it 26079d0851ae bash
[root@26079d0851ae /]# ifconfig
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.20 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::c893:e6ff:fede:a2ab prefixlen 64 scopeid 0x20<link>
ether ca:93:e6:de:a2:ab txqueuelen 1000 (Ethernet)
RX packets 12 bytes 1036 (1.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 620 (620.0 B)
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
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (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@26079d0851ae /]# ping baidu.com
PING baidu.com (220.181.57.217) 56(84) bytes of data.
64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=1 ttl=53 time=24.8 ms
64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=2 ttl=53 time=24.9 ms
^C
— baidu.com ping statistics —
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 24.838/24.896/24.954/0.058 ms
更多 Docker 相关教程见以下内容:
Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm
Ubuntu 14.04 安装 Docker http://www.linuxidc.com/linux/2014-08/105656.htm
Ubuntu 15.04 下安装 Docker http://www.linuxidc.com/Linux/2015-07/120444.htm
Docker 安装实例 http://www.linuxidc.com/Linux/2017-04/142666.htm
在 Ubuntu 15.04 上如何安装 Docker 及基本用法 http://www.linuxidc.com/Linux/2015-09/122885.htm
Ubuntu 16.04 上 Docker 使用手记 http://www.linuxidc.com/Linux/2016-12/138490.htm
Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-04/143190.htm