当前位置: 移动技术网 > 网络运营>服务器>虚拟主机 > Docker 网络工作原理详解

Docker 网络工作原理详解

2019年04月20日  | 移动技术网网络运营  | 我要评论

docker 网络工作原理

当docker server也就是docker daemon启动时,会自动创建一个名字是docker0的bridge,每当docker创建一个container时,会在主机上面创建一个名字是veth*的ethernet 端口,并把这个eth*加入到docker0的bridge,在container中会自动创建一个名字是eth0的ethernet端口,这个eth0和veth*会形成一个类似管道的对,一一对应。

配置dns

docker是如何分配每个container的hostname和dns配置的,可以通过在contain中mount命令看出一些东西:

mount

...
/dev/disk/by-uuid/5f3d0920-98a8-434a-9c02-8163dccf6c62 on /etc/resolv.conf type ext4 (rw,relatime,errors=remount-ro,data=ordered)
/dev/disk/by-uuid/5f3d0920-98a8-434a-9c02-8163dccf6c62 on /etc/hostname type ext4 (rw,relatime,errors=remount-ro,data=ordered)
/dev/disk/by-uuid/5f3d0920-98a8-434a-9c02-8163dccf6c62 on /etc/hosts type ext4 (rw,relatime,errors=remount-ro,data=ordered)
...

docker run的-h 可以配置container的hostname,可以通过-h hostname或者--hostname=hostname来配置,docker会把hostname写入/etc/hostname

例如:

xiaogang@ubuntu:~/shadowsocks$ sudo docker run -t -i --hostname ubuu --rm ubuntu:14.04 /bin/bash

root@ubuu:/# cat /etc/hostname
ubuu

--link=conntainer_nameorid:alias,通过这个选项会在/etc/hosts中添加一个alias,指向container_nameorid, 无需知道具体的ip地址,可以直接使用alias代替。

例如:

--dns=ip_address, 会在/etc/resolv.conf的server标签中添加一个ip_address

--dns-search=domain,会在/etc/resolv.conf的search标签中添加一个domain,如果一个example.com添加到search标签中,当container需要查找一个host的ip时,host.example.com同时也会被查找。

container中的/etc/resolv.conf是从主机中的/etc/resolv.conf拷贝而来,只不过是把主机本地的nameserver过滤掉,如果过滤之后没有任何的nameserver,docker会把google公共nameserver,8.8.8.8和8.8.4.4加入namerserver中。当主机的resolv.conf修改之后会通知container修改

container之间的通信和container和外部之间的通信

1.需要设置ip_forward系统参数,必须设置为1

$ sysctl net.ipv4.conf.all.forwarding
net.ipv4.conf.all.forwarding = 0
$ sysctl net.ipv4.conf.all.forwarding=1
$ sysctl net.ipv4.conf.all.forwarding
net.ipv4.conf.all.forwarding = 1

2.需要设置iptables,允许它们之间的通信

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网