提示:
Docker不再兼容CentOS6,在使用docker的时候请将系统进行升级
CentOS最低版本7.0 kernel最低3.10.0
注意关闭SElinux和iptables等防火墙
docker 默认的桥接网卡是docker0
它只会在本机桥接所有的容器网卡,举例来说容器的虚拟网卡在主机上看一般叫做veth*** 而docker只是把所有这些网卡桥接在一起,如veth2a4c51b:
docker服务进程在启动的时候会生成一个名为docker0的网桥,容器默认都会挂载到该网桥下,但是我们可以通过添加docker启动参数-b Birdge 或更改docker配置文件来选择使用哪个网桥。
一、更新阿里云CentOS源,安装Docker
#备份yum源 # mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 下载阿里云yum源 # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #更新缓存 # yum clean all && yum makecache 下载epel # yum -y install epel-release 更新缓存 # yum clean all && yum makecache 安装docker # yum -y install docker-io 启动docker服务 # systemctl start docker 测试docker状态 # docker info
二、下载centos镜像并启动,安装网络工具后重新进行封装为镜像
2.1、下载centos镜像:
[root@aliyun home]# docker search centos INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/centos The official build of CentOS. 3857 [OK] ...... [root@aliyun home]# docker pull docker.io/centos ...... [root@aliyun home]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 3fa822599e10 6 days ago 203.5 MB
2.2、进入容器安装网络工具:
[root@aliyun home]# docker run -itd --name=centos_net 3fa /bin/bash c7bc119c8387e133b37aa5ff713074529b51f2d6303780310b6ba520eeef6a79 [root@aliyun home]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c7bc119c8387 3fa "/bin/bash" 4 seconds ago Up 4 seconds centos_net [root@aliyun home]# docker attach c7b [root@c7bc119c8387 /]# yum -y install net-tools iproute
...
等待yum安装完成(Ctrl + P + Q)快捷键回到宿主主机,容器后台继续运行
2.3、重新封装为镜像:
[root@aliyun home]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c7bc119c8387 3fa "/bin/bash" 3 minutes ago Up 3 minutes centos_net [root@aliyun home]# docker commit c7b centos_net sha256:7a04c40fc8f8a727d2d3467fc3a75d8a5c3639cf0ad27f35b53c0f67f3783afc [root@aliyun home]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_net latest 7a04c40fc8f8 10 seconds ago 288.6 MB docker.io/centos latest 3fa822599e10 6 days ago 203.5 MB [root@aliyun home]# docker stop c7b && docker rm c7b c7b c7b
三、创建虚拟网桥并设置为默认网桥
3.1、安装pipework
[root@aliyun home]# mkdir -p /home/pipework && cd /home/pipework [root@aliyun pipework]# yum -y install git ... [root@aliyun pipework]# git clone https://github.com/jpetazzo/pipework.git Cloning into 'pipework'... remote: Counting objects: 501, done. remote: Total 501 (delta 0), reused 0 (delta 0), pack-reused 501 Receiving objects: 100% (501/501), 172.97 KiB | 0 bytes/s, done. Resolving deltas: 100% (264/264), done. [root@aliyun pipework]# ls pipework [root@aliyun pipework]# ls ./pipework/ docker-compose.yml doctoc LICENSE pipework pipework.spec README.md [root@aliyun pipework]# cp -rp pipework/pipework /usr/local/bin/ 参考 http://blog.csdn.net/xu470438000/article/details/50512442
3.2、创建虚拟网桥
安装bridge管理工具
[root@aliyun pipework]# yum -y install bridge-utils
停止docker服务
[root@aliyun pipework]# systemctl stop docker
查看网卡状态
[root@aliyun pipework]# ip a s 1: lo:mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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 2: eth0: mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000 link/ether 00:16:3e:04:3b:69 brd ff:ff:ff:ff:ff:ff inet 172.31.43.125/20 brd 172.31.47.255 scope global eth0 valid_lft forever preferred_lft forever 3: docker0: mtu 1500 qdisc noqueue state DOWN link/ether 02:42:2f:aa:d4:c5 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 scope global docker0 valid_lft forever preferred_lft forever
停止自带的docker0网桥
[root@aliyun pipework]# ip link set dev docker0 down
删除该docker0网桥
[root@aliyun pipework]# brctl delbr docker0
查看,已不存在了,删除成功
[root@aliyun pipework]# ip a s 1: lo:mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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 2: eth0: mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000 link/ether 00:16:3e:04:3b:69 brd ff:ff:ff:ff:ff:ff inet 172.31.43.125/20 brd 172.31.47.255 scope global eth0 valid_lft forever preferred_lft forever
创建br0虚拟网桥
[root@aliyun pipework]# brctl addbr br0
启动br0虚拟网桥
[root@aliyun pipework]# ip link set dev br0 up
为该虚拟网桥设置ip和掩码
[root@aliyun pipework]# ip addr add 192.168.1.1/24 dev br0
查看,设置成功
[root@aliyun pipework]# ip a s 1: lo:mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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 2: eth0: mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000 link/ether 00:16:3e:04:3b:69 brd ff:ff:ff:ff:ff:ff inet 172.31.43.125/20 brd 172.31.47.255 scope global eth0 valid_lft forever preferred_lft forever 6: br0: mtu 1500 qdisc noqueue state UNKNOWN qlen 1000 link/ether 22:e9:84:47:82:c8 brd ff:ff:ff:ff:ff:ff inet 192.168.1.1/24 scope global br0 valid_lft forever preferred_lft forever
3.3、设置br为默认网桥
[root@aliyun pipework]# cat /etc/sysconfig/docker-network # /etc/sysconfig/docker-network DOCKER_NETWORK_OPTIONS="-b=br0"
启动docker服务
[root@aliyun pipework]# systemctl start docker
四、启动容器并分配IP
[root@aliyun pipework]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_net latest 7a04c40fc8f8 2 hours ago 288.6 MB docker.io/centos latest 3fa822599e10 6 days ago 203.5 MB
[root@aliyun pipework]# docker run --name=centos --net=none -itd 3fa /bin/bash 7eb78b6b4e103f0f8b0ce70559b20925890ab8b2f2b30e2a848f45e21873c01e
[root@aliyun pipework]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7eb78b6b4e10 3fa "/bin/bash" 4 seconds ago Up 3 seconds centos [root@aliyun pipework]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000000000000 no docker0 8000.0242b580373f no [root@aliyun pipework]# pipework br0 -i eth0 7eb 192.168.1.50/[email protected] [root@aliyun pipework]# brctl show bridge name bridge id STP enabled interfaces br0 8000.12056d403390 no veth0pl15082 docker0 8000.0242b580373f no [root@aliyun pipework]# docker attach 7eb [root@7eb78b6b4e10 /]# ping www.baidu.com PING www.a.shifen.com (220.181.111.188) 56(84) bytes of data. 64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=1 ttl=50 time=17.4 ms 64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=2 ttl=50 time=17.4 ms 64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=3 ttl=50 time=17.4 ms ^C --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 17.440/17.462/17.481/0.016 ms [root@7eb78b6b4e10 /]# ping 192.168.1.1 PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data. 64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.064 ms 64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.057 ms 64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.057 ms ^C --- 192.168.1.1 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 1999ms rtt min/avg/max/mdev = 0.057/0.059/0.064/0.007 ms [root@7eb78b6b4e10 /]# ifconfig eth0: flags=4163mtu 1500 inet 192.168.1.50 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::7c7c:4aff:fe15:e0df prefixlen 64 scopeid 0x20 ether 7e:7c:4a:15:e0:df txqueuelen 1000 (Ethernet) RX packets 2124 bytes 10826617 (10.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1717 bytes 116914 (114.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 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@docker1 ~]# docker version #docker的版本
Client:
Version: 1.12.6
API version: 1.24
Package version: docker-1.12.6-61.git85d7426.el7.centos.x86_64
Go version: go1.8.3
Git commit: 85d7426/1.12.6
Built: Tue Oct 24 15:40:21 2017
OS/Arch: linux/amd64
Server:
Version: 1.12.6
API version: 1.24
Package version: docker-1.12.6-61.git85d7426.el7.centos.x86_64
Go version: go1.8.3
Git commit: 85d7426/1.12.6
Built: Tue Oct 24 15:40:21 2017
OS/Arch: linux/amd64