docker enable ipv6

背景

昨天在服务器上装了一个检测线路质量的软件,需要对特定的ipv6地址发出ping请求。但是docker默认不启动ipv6,需要手动开启。同时,chatgpt给出解决方案过于离谱,所以在此记录一下。

开启IPV6

修改配置文件

编辑/etc/docker/daemon.json,添加如下内容。注意"fixed-cidr-v6"可以设置为私有地址,也可以设置为IDC分配给你的ipv6前缀。

1
2
3
4
5
6
{
"ipv6": true,
"fixed-cidr-v6": "fd00::/80",
"experimental": true,
"ip6tables": true
}

手动创建network

由于某些原因,docker还不支持自动从已经拥有的私网再次划分网段,所以我们需要手动设置网段和网关地址。

1
2
3
4
docker network create --ipv6 \
--subnet="fd00:1::/64" \
--gateway="fd00:1::1" \
main

使用Docker-compose

如果我们需要使用docker-compose 来配置容器的话,我们可以指定 docker-compose 中的网络为外部网络

1
2
3
4
5
6
7
8
9
10
version: "3.9"
services:
alp1:
image: alpine:latest
command: ping6 -c 4 2001:db8:1::1
networks:
- main
networks:
main:
external: true

检测IPV6是否开启

我们通过docker network inspect xxx命令查看container是否获得了ipv6地址。如果获得了,应该显示如下的结果。

1
2
3
4
5
6
7
8
9
"Containers": {
"0cf565c8ae86ec8586b191a84d35095e9abb3b4ad91ac5008f32350dc0f37251": {
"Name": "xxx",
"EndpointID": "xxx",
"MacAddress": "02:42:ac:15:00:04",
"IPv4Address": "172.21.0.4/16",
"IPv6Address": "fd00:1::4/64"
}
}