├── config ├── cronjob ├── s6 │ ├── log │ │ └── run │ ├── set-permissions │ │ ├── run │ │ └── set_permissions.sh │ └── cron │ │ └── run └── cronjob-eg ├── time ├── version ├── docker-compose-eg.yml ├── README.md ├── Dockerfile └── .github └── workflows └── main.yml /config/cronjob: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /time: -------------------------------------------------------------------------------- 1 | 2025-11-22 03:42:30 2 | -------------------------------------------------------------------------------- /version: -------------------------------------------------------------------------------- 1 | 54ec3699ae96224fff57289420b5bfc196cd471b 2 | -------------------------------------------------------------------------------- /config/s6/log/run: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 这个脚本将实时显示 cf2dns.log 的内容 3 | exec tail -f /tmp/cf2dns.log 4 | -------------------------------------------------------------------------------- /config/s6/set-permissions/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 启动赋予权限的脚本 4 | exec /etc/s6/services/set-permissions/set_permissions.sh 5 | -------------------------------------------------------------------------------- /config/cronjob-eg: -------------------------------------------------------------------------------- 1 | 52 6-23 * * * /home/www/venv/bin/python /home/www/cf2dns/cf2dns-v4.py 2>&1 | /usr/bin/tee -a /tmp/cf2dns.log 2 | 23 7-23 * * * /home/www/venv/bin/python /home/www/cf2dns/cf2dns-v6.py 2>&1 | /usr/bin/tee -a /tmp/cf2dns.log 3 | -------------------------------------------------------------------------------- /config/s6/cron/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 复制 cronjob 文件 4 | cp -f /home/www/cronjob /etc/cron.d/cronjob 5 | 6 | # 设置权限 7 | chmod 0644 /etc/cron.d/cronjob 8 | 9 | # 加载 cron 任务 10 | /usr/bin/crontab /etc/cron.d/cronjob 11 | 12 | # 创建日志文件 13 | touch /tmp/cf2dns.log 14 | 15 | # 启动 cron 服务 16 | exec /usr/sbin/cron -f -L 15 17 | -------------------------------------------------------------------------------- /config/s6/set-permissions/set_permissions.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 检查标志文件是否存在 4 | if [ ! -f /tmp/cf2dns_initialized ]; then 5 | # 赋予执行权限 6 | chmod +x /home/www/cf2dns/cf2dns.py 7 | chmod +x /home/www/cf2dns/cf2dns-v4.py 8 | chmod +x /home/www/cf2dns/cf2dns-v6.py 9 | 10 | # 创建标志文件 11 | touch /tmp/cf2dns_initialized 12 | fi 13 | -------------------------------------------------------------------------------- /docker-compose-eg.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | cf2dns: 4 | restart: always 5 | image: aaronlee/cf2dns:latest 6 | container_name: cf2dns 7 | volumes: 8 | - ./config/cronjob:/home/www/cronjob 9 | - ./config/cf2dns-v4.py:/home/www/cf2dns/cf2dns-v4.py 10 | - ./config/cf2dns-v6.py:/home/www/cf2dns/cf2dns-v6.py 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 使用说明 2 | * 下载[docker-compose-eg.yml](https://raw.githubusercontent.com/aaro-n/cf2dns-docker/main/docker-compose-eg.yml)并重命名为docker-compose.yml。 3 | * 执行`mkdir config`创建配置文件夹。 4 | * 进入`config`文件夹,创建`cf2dns.py`和`cronjob`文件。 5 | ## 配置文件说明 6 | * `cf2dns.py`可以从[cf2dns源码仓库](https://raw.githubusercontent.com/ddgth/cf2dns/master/cf2dns.py)下载并按照说明修改,可以将修改后的文件另存为`cf2dns-v4.py`和`cf2dns-v6.py` 7 | * 定时任务`cronjob` 8 | ``` 9 | 52 6-23 * * * /home/www/venv/bin/python /home/www/cf2dns/cf2dns-v4.py 2>&1 | /usr/bin/tee -a /tmp/cf2dns.log 10 | 23 7-23 * * * /home/www/venv/bin/python /home/www/cf2dns/cf2dns-v6.py 2>&1 | /usr/bin/tee -a /tmp/cf2dns.log 11 | ``` 12 | ## 定时说明 13 | 以`52 6-23 * * * /home/www/venv/bin/python /home/www/cf2dns/cf2dns-v4.py 2>&1 | /usr/bin/tee -a /tmp/cf2dns.log`为例 14 | 15 | `52 6-23 * * *`定时任务运行时间,镜像使用的时间时北京时间。 16 | 17 | `/home/www/venv/bin/python` 虚拟python环境安装路径。 18 | 19 | `/home/www/cf2dns/cf2dns-v4.py` cf2dns脚本文件绝对路径。 20 | 21 | `2>&1 | /usr/bin/tee -a /tmp/cf2dns.log` 将cf2dns运行日志输出到/tmp/cf2dns.log,注意必须输出到/tmp/cf2dns.log,否则控制台无法滚动显示cf2dns运行记录,必须通过日志查看。 22 | 23 | 要根据自身需求修改 24 | 25 | ## 注意事项 26 | * 要按照说明赋予创建的文件所需的权限,可以通过`docker exec -it cf2dns /bin/sh`进入容器,查看查看运行日志。 27 | * 容器里的时间是北京时间 28 | 29 | ## 更新日志 30 | 24-10-03 主要有以下修改: 31 | * 将基础镜像切换为Debian 32 | * 控制台能显示运行日志 33 | * 将进程管理软件由supervisor改为s6 34 | 35 | 23-09-09 主要有以下修改: 36 | * 删除源码文件,每次构建镜像时自动从`ddgth/cf2dns`获取源码。 37 | * 添加定时任务,每月运行一次镜像构建。 38 | * 镜像标签除`aaronlee/cf2dns:latest`外,还有`aaronlee/cf2dns:镜像构建时间` 和`aaronlee/cf2dns:源码仓库SHA值前7位` 39 | 构建的镜像已经测试,可以正常运行。 40 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:latest 2 | 3 | # 准备运行环境 4 | RUN apt-get update && \ 5 | apt-get install -y --no-install-recommends \ 6 | curl \ 7 | git \ 8 | cron \ 9 | libcap2 \ 10 | python3 \ 11 | python3-pip \ 12 | python3-venv \ 13 | tzdata \ 14 | s6 execline \ 15 | coreutils && \ 16 | apt-get purge -y systemd && \ 17 | apt-get clean && \ 18 | rm -rf /var/cache/apt/* && \ 19 | groupadd -r www && useradd -r -g www www && \ 20 | # 设置时区为北京时间 21 | ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ 22 | echo "Asia/Shanghai" > /etc/timezone && \ 23 | # Clone cf2dns 24 | git clone https://github.com/ddgth/cf2dns.git /home/www/cf2dns && \ 25 | # 创建 s6 服务目录 26 | mkdir -p /etc/s6/services 27 | 28 | # 复制 s6 服务配置 29 | COPY config/s6/ /etc/s6/services/ 30 | 31 | # 复制 cron 任务文件 32 | COPY config/cronjob /home/www/cronjob 33 | COPY config/cronjob /etc/cron.d/cronjob 34 | 35 | # 创建虚拟环境并安装依赖 36 | RUN python3 -m venv /home/www/venv && \ 37 | /home/www/venv/bin/pip install --upgrade pip && \ 38 | /home/www/venv/bin/pip install --no-cache-dir --upgrade -r /home/www/cf2dns/requirements.txt && \ 39 | # 设置权限 40 | chown -R www:www /home/www/ && \ 41 | chmod -R +x /etc/s6/services/* && \ 42 | find /etc/s6/services -name run -exec chmod +x {} \; && \ 43 | # 设置 cron 任务文件的权限 44 | chmod 0644 /etc/cron.d/cronjob && \ 45 | crontab /etc/cron.d/cronjob 46 | 47 | # Add application 48 | WORKDIR /home/www/ 49 | 50 | # 使用 s6 启动 51 | ENTRYPOINT ["/usr/bin/s6-svscan", "/etc/s6/services"] 52 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: 发布镜像 2 | 3 | on: 4 | push: 5 | branches: 6 | - 'main' 7 | schedule: 8 | - cron: "13 3 22 * *" 9 | 10 | jobs: 11 | docker: 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: 检出仓库 16 | uses: actions/checkout@v2 17 | 18 | - name: 设置构建时间戳 19 | id: set-timestamp 20 | run: echo "::set-output name=timestamp::$(date +'%Y%m%d%H%M%S')" 21 | 22 | - name: 获取最新提交的 SHA 23 | id: get_commit_sha 24 | run: | 25 | commit_sha=$(curl -s "https://api.github.com/repos/ddgth/cf2dns/commits/master" | jq -r '.sha') 26 | echo "::set-output name=commit_sha::$commit_sha" 27 | 28 | - name: 检查存储的版本 29 | id: check_version 30 | run: | 31 | stored_version=$(cat version) 32 | echo "::set-output name=stored_version::$stored_version" 33 | 34 | - name: 比较提交的 SHA 35 | id: compare_shas 36 | run: | 37 | if [[ "${{ steps.get_commit_sha.outputs.commit_sha }}" == "${{ steps.check_version.outputs.stored_version }}" ]]; then 38 | echo "提交的 SHA 相同,跳过镜像构建。" 39 | echo "::set-output name=build_image::false" 40 | else 41 | echo "提交的 SHA 不同,开始构建镜像。" 42 | echo "::set-output name=build_image::true" 43 | fi 44 | 45 | - name: 提取版本前缀 46 | id: extract_version_prefix 47 | run: | 48 | commit_sha=${{ steps.get_commit_sha.outputs.commit_sha }} 49 | version_prefix=${commit_sha:0:7} 50 | echo "::set-output name=version_prefix::$version_prefix" 51 | 52 | - name: 构建并推送 Docker 镜像 53 | if: ${{ steps.compare_shas.outputs.build_image == 'true' }} 54 | env: 55 | DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} 56 | DOCKER_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }} 57 | run: | 58 | echo "${{ steps.get_commit_sha.outputs.commit_sha }}" > version 59 | docker build -t aaronlee/cf2dns:latest . 60 | docker build -t aaronlee/cf2dns:${{ steps.set-timestamp.outputs.timestamp }} . 61 | docker build -t aaronlee/cf2dns:${{ steps.extract_version_prefix.outputs.version_prefix }} . 62 | echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin 63 | docker push aaronlee/cf2dns:latest 64 | docker push aaronlee/cf2dns:${{ steps.set-timestamp.outputs.timestamp }} 65 | docker push aaronlee/cf2dns:${{ steps.extract_version_prefix.outputs.version_prefix }} 66 | 67 | - name: 更新版本文件 68 | if: ${{ steps.compare_shas.outputs.build_image == 'true' }} 69 | run: | 70 | git config --global user.name "GitHub Actions" 71 | git config --global user.email "actions@github.com" 72 | git add version 73 | git commit -m "更新版本文件" 74 | git push 75 | 76 | - name: 写入时间到文件 77 | run: echo $(date +"%Y-%m-%d %H:%M:%S") > time 78 | 79 | - name: 提交和推送时间更改 80 | run: | 81 | git config --global user.name "GitHub Actions" 82 | git config --global user.email "actions@github.com" 83 | git add time 84 | git commit -m "更新时间文件" 85 | git push origin main 86 | --------------------------------------------------------------------------------