docker容器启用ipv6地址的方法流程
version: 2.* ,version: 3.* 不支持 enable_ipv6 配置如果已有旧的容器在运行(网络配置发生了变化),则需要先销毁容器 docker-compose down 然后再重新创建 docker-compose up仅需在network下添加如下内容即可,其他信息正常配置。networks: local_bridge: enable_ipv6: true driver: bridge ipam: config: - subnet: "2409:807e::/80"
2: ens3: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether fa:16:3e:67:77:58 brd ff:ff:ff:ff:ff:ff inet 10.241.102.245/24 brd 10.241.102.255 scope global dynamic noprefixroute ens3 valid_lft 63404sec preferred_lft 63404sec inet6 2409:807e:58cc:114::a2d/120 scope global noprefixroute valid_lft forever preferred_lft forever inet6 fe80::f816:3eff:fe67:7758/64 scope link noprefixroute valid_lft forever preferred_lft forever执行vim /etc/docker/daemon.json 配置文件【没有这个配置文件是正常的】,写入如下内容 fixed-cidr-v6 : 2409::/80 ,这个后面的ip是自定义的。
[root@xz-docker-tes-01 ~]# cat /etc/docker/daemon.json { "ipv6": true, "fixed-cidr-v6": "2409::/80", "experimental": true, "ip6tables": true}[root@xz-docker-tes-01 ~]#
[root@xz-docker-tes-01 ~]# systemctl restart docker[root@xz-docker-tes-01 ~]# systemctl status docker● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2023-12-01 11:40:31 CST; 9s ago Docs: https://docs.docker.com Main PID: 14470 (dockerd) Tasks: 13 Memory: 47.3M CGroup: /system.slice/docker.service └─14470 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
[root@xz-docker-tes-01 ~]# docker network create -d bridge --ipv6 --subnet "2409:807e::/80" ipv6_bridge09663034b21493f64d2484dc21923a789bc8ac51c403d422e397435df74f204b[root@xz-docker-tes-01 ~]#创建好后的网络信息如下
[root@xz-docker-tes-01 ~]# docker network listNETWORK ID NAME DRIVER SCOPEbf1937081949 bridge bridge locale98be3082c27 host host local09663034b214 ipv6_bridge bridge local7cee98cd58fe none null local[root@xz-docker-tes-01 ~]#
我这使用上面创建的一个ipv6的网络做测试测试
[root@xz-docker-tes-01 ~]# docker run -dit --name=v6 --restart=always --network=ipv6_bridge hub.c.163.com/library/centos:latest 49af16d7dd9c63afd2a43b24b6dfdb8b39d70ef8e39c1d1c067dcbe28c242efa[root@xz-docker-tes-01 ~]# [root@xz-docker-tes-01 ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES49af16d7dd9c hub.c.163.com/library/centos:latest "/bin/bash" 3 seconds ago Up 2 seconds v6[root@xz-docker-tes-01 ~]#
容器内 ping其他v6地址和网关都能通,一切正常
[root@xz-docker-tes-01 ~]# docker exec -it v6 bash[root@49af16d7dd9c /]# lsanaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var[root@49af16d7dd9c /]#[root@49af16d7dd9c /]# ping6 2409:807e:58cc:114::a2dPING 2409:807e:58cc:114::a2d(2409:807e:58cc:114::a2d) 56 data bytes64 bytes from 2409:807e:58cc:114::a2d: icmp_seq=1 ttl=64 time=0.459 ms64 bytes from 2409:807e:58cc:114::a2d: icmp_seq=2 ttl=64 time=0.093 ms64 bytes from 2409:807e:58cc:114::a2d: icmp_seq=3 ttl=64 time=0.090 ms^C--- 2409:807e:58cc:114::a2d ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2058msrtt min/avg/max/mdev = 0.090/0.214/0.459/0.173 ms[root@49af16d7dd9c /]# ping6 2409:807e:58cc:114::a01PING 2409:807e:58cc:114::a01(2409:807e:58cc:114::a01) 56 data bytes64 bytes from 2409:807e:58cc:114::a01: icmp_seq=1 ttl=63 time=10.2 ms64 bytes from 2409:807e:58cc:114::a01: icmp_seq=2 ttl=63 time=2.04 ms64 bytes from 2409:807e:58cc:114::a01: icmp_seq=3 ttl=63 time=2.23 ms64 bytes from 2409:807e:58cc:114::a01: icmp_seq=4 ttl=63 time=2.35 ms^C--- 2409:807e:58cc:114::a01 ping statistics ---4 packets transmitted, 4 received, 0% packet loss, time 3004msrtt min/avg/max/mdev = 2.048/4.208/10.200/3.461 ms[root@49af16d7dd9c /]# [root@49af16d7dd9c /]# exitexit[root@xz-docker-tes-01 ~]#
该容器的网络容器里面呢,也会有一个ipv6地址,主机虽然能ping通,但这个ip是容器专属的,
[root@xz-docker-tes-01 ~]# docker inspect v6 | grep "IPv6" "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "SecondaryIPv6Addresses": null, "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPv6Gateway": "", "IPv6Gateway": "2409:807e::1", "GlobalIPv6Address": "2409:807e::2", "GlobalIPv6PrefixLen": 80,[root@xz-docker-tes-01 ~]# [root@xz-docker-tes-01 ~]# ping 2409:807e::2PING 2409:807e::2(2409:807e::2) 56 data bytes64 bytes from 2409:807e::2: icmp_seq=1 ttl=64 time=0.695 ms64 bytes from 2409:807e::2: icmp_seq=2 ttl=64 time=0.090 ms^C--- 2409:807e::2 ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 50msrtt min/avg/max/mdev = 0.090/0.392/0.695/0.303 ms[root@xz-docker-tes-01 ~]#
容器内能ping通同样用这个网络创建的其他容器【但默认容器直接网络是隔离的哈】,具体这个v6地址的用途自行探索吧。
[root@xz-docker-tes-01 ~]# docker exec -it v6 bash[root@49af16d7dd9c /]# ping6 2409:807e::3PING 2409:807e::3(2409:807e::3) 56 data bytes64 bytes from 2409:807e::3: icmp_seq=1 ttl=64 time=0.346 ms64 bytes from 2409:807e::3: icmp_seq=2 ttl=64 time=0.108 ms64 bytes from 2409:807e::3: icmp_seq=3 ttl=64 time=0.107 ms^C--- 2409:807e::3 ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2085msrtt min/avg/max/mdev = 0.107/0.187/0.346/0.112 ms[root@49af16d7dd9c /]#
使用普通网络创建容器测试
进入以后,能ping通网关和其他v6地址,没问题。
[root@xz-docker-tes-01 ~]# docker run --name=test1 -it hub.c.163.com/library/centos[root@d866a511db84 /]# ping6 2409:807e:58cc:114::a17PING 2409:807e:58cc:114::a17(2409:807e:58cc:114::a17) 56 data bytes64 bytes from 2409:807e:58cc:114::a17: icmp_seq=1 ttl=63 time=1.85 ms64 bytes from 2409:807e:58cc:114::a17: icmp_seq=2 ttl=63 time=0.782 ms64 bytes from 2409:807e:58cc:114::a17: icmp_seq=3 ttl=63 time=0.793 ms64 bytes from 2409:807e:58cc:114::a17: icmp_seq=4 ttl=63 time=0.891 ms^C--- 2409:807e:58cc:114::a17 ping statistics ---4 packets transmitted, 4 received, 0% packet loss, time 3053msrtt min/avg/max/mdev = 0.782/1.080/1.855/0.449 ms[root@d866a511db84 /]# [root@d866a511db84 /]# ping6 2409:807e:58cc:114::a01PING 2409:807e:58cc:114::a01(2409:807e:58cc:114::a01) 56 data bytes64 bytes from 2409:807e:58cc:114::a01: icmp_seq=1 ttl=63 time=13.3 ms64 bytes from 2409:807e:58cc:114::a01: icmp_seq=2 ttl=63 time=1.85 ms^C--- 2409:807e:58cc:114::a01 ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2003msrtt min/avg/max/mdev = 1.716/5.648/13.378/5.466 ms[root@d866a511db84 /]#
默认生成的,没有这个v6地址的,但不影响使用ipv6.
[root@xz-docker-tes-01 ~]# docker inspect test1 | grep "IP" "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "GlobalIPv6Address": "2409::242:ac11:2", "GlobalIPv6PrefixLen": 80, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "2409::1", "IPAMConfig": null, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "2409::1", "GlobalIPv6Address": "2409::242:ac11:2", "GlobalIPv6PrefixLen": 80,[root@xz-docker-tes-01 ~]#
先创建一个映射端口的nginx容器
[root@xz-docker-tes-01 ~]# docker run -dit --name=nginx --restart=always -p 80:80 --network=ipv6_bridge nginx4a175fb0754961537b23111bab1251e9c9f36645e9936f07c5daeea28af4d898[root@xz-docker-tes-01 ~]# netstat -ntlp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16074/docker-proxy tcp6 0 0 :::80 :::* LISTEN 16088/docker-proxy [root@xz-docker-tes-01 ~]#
我是指定了创建的ipv6网络的【其实不指定也一样,不影响外部访问的】
[root@xz-docker-tes-01 ~]# docker inspect nginx | grep "IPv6" "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "SecondaryIPv6Addresses": null, "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPv6Gateway": "", "IPv6Gateway": "2409:807e::1", "GlobalIPv6Address": "2409:807e::3", "GlobalIPv6PrefixLen": 80,[root@xz-docker-tes-01 ~]#
直接浏览器输入ipv4的地址,不报错就行【我下面显示ccx是因为我修改过nginx的默认文件内容了】
验证ipv6地址之前,需要保证你测试的主机上已经配置有ipv6地址并且能正常使用
首先测试能否ping通目标ipv6地址【就上面搭建ipv6的主机v6地址,是主机,而非容器的啊】
然后网页直接输入v6地址【就上面搭建ipv6的主机v6地址,是主机,而非容器的啊】
访问格式[ipv6addr] 【注意,v6地址用中括号扩起来的】其实下面内容就是nginx默认的内容,因为没有放任何东西,所以就会显示nginx界面,反正没报错就是正常的。先进入nginx容器内部
[root@xz-docker-tes-01 ~]# docker exec -it nginx bashroot@4a175fb07549:/#
因为不知道容器的http默认文件在哪里,所以可以用find搜索
root@4a175fb07549:/# find / -name htmlfind: '/proc/32/map_files': Permission deniedfind: '/proc/33/map_files': Permission deniedfind: '/proc/34/map_files': Permission deniedfind: '/proc/35/map_files': Permission denied/usr/share/nginx/htmlroot@4a175fb07549:/#
通过搜索已知html路径为:/usr/share/nginx/html
那幺就可以去修改了噻【懂了吧,想要显示啥内容,替换这个index.html文件就行了】
root@4a175fb07549:/usr/share/nginx/html# ls50x.html index.htmlroot@4a175fb07549:/usr/share/nginx/html# vi index.html bash: vi: command not foundroot@4a175fb07549:/usr/share/nginx/html# vim index.html bash: vim: command not foundroot@4a175fb07549:/usr/share/nginx/html# cat index.html !DOCTYPE html html head title Welcome to nginx! /title style body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } /style /head body h1 Welcome to nginx! /h1 p If you see this page, the nginx web server is successfully installed andworking. Further configuration is required. /p p For online documentation and support please refer to a href="http://nginx.org/" rel="external nofollow" nginx.org /a .Commercial support is available at a href="http://nginx.com/" rel="external nofollow" nginx.com /a . /p p em Thank you for using nginx. /em /p /body /html root@4a175fb07549:/usr/share/nginx/html# cp index.html index.html.bakroot@4a175fb07549:/usr/share/nginx/html# echo ccx index.htmlroot@4a175fb07549:/usr/share/nginx/html#
如我上面,将ccx内容写入了index.html文件,那幺正常情况,网页就只会显示ccx这3个字母
以上就是docker容器启用ipv6地址的方法流程的详细内容,更多关于docker启用ipv6地址的资料请关注8b8.cn其它相关文章!
docker容器启用ipv6地址的方法流程