# Cloudflare搭建DDNS(docker)

本文所使用的项目地址<https://github.com/oznu/docker-cloudflare-ddns>

首先准备一个域名，域名可以去[~~Freenom~~](https://my.freenom.com)~~注册一个免费域名~~，目前免费域名已经不支持api了。

* 打开[cloudflare](https://www.cloudflare.com/zh-cn/)添加你注册的这个域名
* 根据提示把域名的NS更换成Cloudflare的NS
* 在cloudflare添加一条A记录，IP随便填一个

![](https://i.loli.net/2019/06/26/5d134f415695c24799.jpg)

![](https://i.loli.net/2019/06/26/5d134ff208c2458098.jpg)

### 安装 docker

```
docker version > /dev/null || curl -fsSL get.docker.com | bash
service docker restart
```

### 快速设置：

```
docker run -d \
  -e EMAIL=hello@example.com \
  -e API_KEY=xxxxxxx \
  -e ZONE=example.com \
  -e SUBDOMAIN=subdomain \
  --restart=always oznu/cloudflare-ddns
```

### 测试

#### 首次测试

![](https://i.loli.net/2019/06/26/5d13559cdfc8d45423.jpg)

#### 重新拨号后测试

![](https://i.loli.net/2019/06/26/5d1359289422960120.jpg)

**环境变量**

* -e EMAIL             - 你的CloudFlare电子邮件地址，**使用范围API令牌时，不得设置此变量**。
* -e API\_KEY           - 您的CloudFlare Global API Key。在此处获取: [API密钥获取](https://dash.cloudflare.com/profile/api-tokens)
* -e ZONE              - 你的域名（一级）
* -e SUBDOMAIN         - 你设置的二级域名（只用填写前缀），如果未填写，将使用一级。
* -e PROXIED           - 设置为true使流量通过CloudFlare CDN。默认为false.
* -e RRTYPE=A          - 设置为AAAA使用设置IPv6记录而不是IPv4记录。默认A为IPv4记录.
* -e DELETE\_ON\_STOP    - 设置为true在容器停止时删除dns记录。默认为false
* -e INTERFACE=tun0       - 设置为tun0从名为的网络接口提取IP tun0。如果未提供，则将使用公网IP。需要--network host运行参数。

**使用范围API令牌**

> 任何时候都不建议直接使用Global API Key，为了安全建议使用API Tokens

创建CloudFlare API令牌，请访问<https://dash.cloudflare.com/profile/api-tokens>执行以下步骤： 1. 点击Create Token创建令牌 2. 设置令牌名称,例如 `cloudflare-ddns` 3. 授予令牌以下权限：

* Zone - Zone Settings - Read
* Zone - Zone - Read
* Zone - DNS - Edit
  1. 将Zone Resources设置为：
* Include - All zones
  1. 完成向导并将生成的令牌复制到 `API_KEY` 容器的变量中

如下图所示&#x20;

![](https://i.loli.net/2020/02/25/WEOY5Pe786BDAcl.png)

Zone Resources可选All zones或Specific zone


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aipeach.gitbook.io/blogbackup/cloudflare-da-jian-ddnsdocker.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
