本文档详细说明如何使用 Docker 方式运行 DDNS 工具,包括基本用法、高级配置、多种网络模式以及常见问题解决方案。 可移植性最佳。
DDNS 官方提供了优化过的 Docker 镜像,具有以下特点:
DDNS Docker 镜像特殊版本:
latest
最新稳定版(默认)next
下一个版本(最新beta版)edge
最新开发版(不稳定), 同步master分支docker pull newfuture/ddns:latest
您也可以指定特定版本,例如:
docker pull newfuture/ddns:v2.8.0
DDNS Docker 镜像支持三种配置方式:命令行,环境变量和配置文件。
当设置了命令行参数时,容器将直接运行 DDNS 程序,而不会启用定时任务。 如果您需要定时任务,请使用环境变量或配置文件方式。
可以参考命令行参数说明获取详细的参数列表。
此时 docker run --name=ddns --network=host newfuture/ddns
就相当于 ddns
命令行,不会执行定时任务。
此方式适合需要一次性运行或调试的场景。
docker run --name=ddns --network=host newfuture/ddns -h
docker run -d \
-e DDNS_DNS=dnspod \
-e DDNS_ID=12345 \
-e DDNS_TOKEN=mytokenkey \
-e DDNS_IPV4=example.com,www.example.com \
-e DDNS_INDEX4=['public',0] \
-e DDNS_IPV6=example.com,ipv6.example.com \
--network host \
--name ddns \
newfuture/ddns
想要了解所有支持的环境变量,请参考环境变量配置说明。
docker run -d \
-v /host/config/:/ddns/ \
--network host \
--name ddns \
newfuture/ddns
其中 /host/config/
是您本地包含 config.json
的目录。Docker 容器内的工作目录是 /ddns/
,配置文件会被映射到容器内的 /ddns/config.json
。
详见 config.json
的内容可以参考 JSON 配置文件说明。
使用 --network host
可让容器直接使用宿主机的网络,这样 DDNS 可以正确获取宿主机的 IP 地址。
对于Public 或者 url 通常不需要设置 host
docker run -d \
-e DDNS_DNS=dnspod \
-e DDNS_ID=12345 \
-e DDNS_TOKEN=mytokenkey \
-e DDNS_IPV4=example.com \
--network host \
newfuture/ddns
如果您不想使用 host 网络模式,也可以使用默认的 bridge 模式,但需要注意此时容器获取的是内部 IP,您需要使用 public
模式获取公网 IP:
docker run -d \
-e DDNS_DNS=dnspod \
-e DDNS_ID=12345 \
-e DDNS_TOKEN=mytokenkey \
-e DDNS_IPV4=example.com \
-e DDNS_INDEX4=public \
newfuture/ddns
默认情况下,容器每 5 分钟执行一次 DDNS 更新。您可以通过挂载自定义的 crontab 文件来修改定时策略:
# 创建自定义 crontab 文件
echo "*/10 * * * * cd /ddns && /bin/ddns" > mycron
# 挂载自定义 crontab 文件
docker run -d \
-v /path/to/config/:/ddns/ \
-v $(pwd)/mycron:/etc/crontabs/root \
--network host \
newfuture/ddns
如果您只想执行一次更新而不启用定时任务,可以直接传递参数给容器:
docker run --rm \
-e DDNS_DNS=dnspod \
-e DDNS_ID=12345 \
-e DDNS_TOKEN=mytokenkey \
-e DDNS_IPV4=example.com \
--network host \
newfuture/ddns --debug
这里 --debug
是传递给 DDNS 程序的参数,启用调试模式。任何以 -
开头的参数都会被传递给 DDNS 程序。
环境变量方式配置多域名:
docker run -d \
-e DDNS_DNS=dnspod \
-e DDNS_ID=12345 \
-e DDNS_TOKEN=mytokenkey \
-e DDNS_IPV4='["example.com", "www.example.com", "sub.example.com"]' \
-e DDNS_IPV6='["example.com", "ipv6.example.com"]' \
--network host \
newfuture/ddns
要在Docker容器中使用IPv6,需要确保Docker守护程序配置了IPv6支持:
/etc/docker/daemon.json
:{
"ipv6": true,
"fixed-cidr-v6": "fd00::/80"
}
sudo systemctl restart docker
docker run -d \
--network host \
-e DDNS_DNS=dnspod \
-e DDNS_ID=12345 \
-e DDNS_TOKEN=mytokenkey \
-e DDNS_IPV6=example.com \
newfuture/ddns
创建 docker-compose.yml
文件:
version: '3'
services:
ddns:
image: newfuture/ddns:latest
restart: always
network_mode: host
environment:
- DDNS_DNS=dnspod
- DDNS_ID=12345
- DDNS_TOKEN=mytokenkey
- DDNS_IPV4=example.com,www.example.com
- DDNS_IPV6=example.com,ipv6.example.com
- DDNS_TTL=600
- DDNS_LOG_LEVEL=INFO
version: '3'
services:
ddns:
image: newfuture/ddns:latest
restart: always
network_mode: host
volumes:
- ./config:/ddns
运行 Docker Compose:
docker-compose up -d
如果您需要在容器中添加其他工具或自定义环境,可以基于官方镜像创建自己的 Dockerfile:
FROM newfuture/ddns:latest
# 安装额外的工具
RUN apk add --no-cache curl
# 添加自定义脚本
COPY custom-script.sh /bin/
RUN chmod +x /bin/custom-script.sh
# 覆盖默认入口点(可选)
# ENTRYPOINT ["/bin/custom-script.sh"]
查看容器输出日志:
docker logs ddns
实时跟踪日志:
docker logs -f ddns
问题: DDNS无法正确获取主机IP
解决方案:
--network host
网络模式-e DDNS_INDEX4=public
强制使用公网API获取IP问题: 容器运行但不自动更新DNS
解决方案:
docker logs ddns
docker ps -a
docker exec ddns /bin/ddns
问题: 容器启动后立即退出
解决方案:
-it
参数以交互方式运行查看问题 docker run -it --rm newfuture/ddns
问题: 容器无法连接到DNS服务商API
解决方案:
docker exec ddns ping api.dnspod.cn
-e DDNS_PROXY=http://proxy:port
为了保存DDNS的缓存数据,避免频繁API调用,可以挂载缓存目录:
docker run -d \
-e DDNS_DNS=dnspod \
-e DDNS_ID=12345 \
-e DDNS_TOKEN=mytokenkey \
-e DDNS_IPV4=example.com \
-e DDNS_CACHE=/ddns/cache.json \
-v /path/to/cache:/ddns \
--network host \
newfuture/ddns
Docker Compose配置添加健康检查:
version: '3'
services:
ddns:
image: newfuture/ddns:latest
restart: always
network_mode: host
environment:
- DDNS_DNS=dnspod
- DDNS_ID=12345
- DDNS_TOKEN=mytokenkey
- DDNS_IPV4=example.com
healthcheck:
test: ["CMD", "pgrep", "crond"]
interval: 5m
timeout: 10s
retries: 3