配置docker

编辑/etc/docker/daemon.json

{
  "ipv6": true,
  "fixed-cidr-v6": "****:****:****:****::/64",
  "experimental": true,
  "ip6tables": true
}

****:****:****:**** 替换成你自己的ipv6地址。

重启docker

systemctl restart docker

查看bridge网络

docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "bd143c082602194bee4862546204b13acbced66bf313dd93e349c771fd83591a",
        "Created": "2024-01-26T12:06:57.360041776+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": true,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "**.**.**.0/24",
                    "Gateway": "**.**.**.1"
                },
                {
                    "Subnet": "****:****:****:****::/64",
                    "Gateway": "****:****:****:****::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": {}
    }
]

配置docker-compose

docker-compose文件修改,增加以下内容

version: '3.1'

services:
  ubuntu:
    image: ubuntu:latest
    container_name: ubuntu
    restart: always
    ports:
      - '2222:22'
    networks:
      - ipv6net
networks:
  ipv6net:
    driver: bridge
    enable_ipv6: true
    ipam:
      driver: default
      config:
        - subnet: ****:****:****:****::/64 

注意:这里的ipv6地址与/etc/docker/daemon.json里的一样。

重新启动容器

docker-compose up -d

查看容器详细,就能看到容器被分配ipv6地址了

docker inspect ubuntu

测试公网ipv6是否可以访问

apt update
apt install inetutils-ping
ping6 公网ipv6地址