如何让docker支持IPv6

docker默认是不支持IPv6的需要手动开启

查看当前docker网卡

docker inspect bridge 
[
    {
        "Name": "bridge",
        "Id": "5031019abdb475a0ff5d0e62c92b39f9c3dca39245f910923ff5c8f4b7c8b229",
        "Created": "2023-05-18T15:05:09.802717149+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

可以看到,当前根本没有IPv6相关的

手动开启IPv6

修改配置文件

vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://reg-mirror.qiniu.com",
"https://registry.docker-cn.com"
],
 "exec-opts": ["native.cgroupdriver=systemd"],
 "data-root": "/data/docker",

 #在原本的基础上添加下面这四行,最后一行需要只能容器内部使用的IPv6地址段,这个2001:db8:1::/64地址段是数据局域网性质的,正好符合docker的需求
 "experimental": true,
 "ip6tables": true,
 "ipv6": true,
 "fixed-cidr-v6": "2001:db8:1::/64"
}

然后保存退出,重启docker

systemctl restart docker

这个时候再查看网卡

[
    {
        "Name": "bridge",
        "Id": "d06a5496949617265e282d37ee8b05d676be44ba39ea63bce8f10e6703ee4e16",
        "Created": "2023-05-30T16:29:16.620204142+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": true,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                },
                {
                    "Subnet": "2001:db8:1::/64"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

可以看到已经有了刚刚设置的ipv6网段了

同时,也可以在网卡上查看到,已经有了IPv6的地址

ip a show docker0 
6: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:9e:f0:ca:ba brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 2001:db8:1::1/64 scope global tentative 
       valid_lft forever preferred_lft forever
    inet6 fe80::1/64 scope link tentative 
       valid_lft forever preferred_lft forever

到这里,docker就已经支持了ipv6

但是新建了容器,容器不一定会支持ipv6

例如用docker-compose新建网络的时候

就需要在新建网卡的时候指定开启ipv6

例子

version: "3"
services:
  alp2:
    image: alpine:latest
    command: ping6 -c 4 2001:db8:a::1
    networks:
      - net2
networks:
  net2:
    enable_ipv6: true
    ipam:
      config:
        - subnet: 2001:db8:a::/64
          gateway: 2001:db8:a::1

可以看到,需要在创建虚拟网卡的时候开启IPv6,同时还要指定一个与上面docker配置的网段不在同一网段的IPv6网段,这个时候,就可以正常分配IPv6地址了

posted @ 2023-05-30 19:16  厚礼蝎  阅读(1439)  评论(0编辑  收藏  举报