Sign inSign up

fedeabella/cloudflare-ddns

By fedeabella

Updated 19 days ago

A simple, to the point python script to update Cloudflare DNS records with a dynamic IP.

Image
Networking
Web servers
0

1.4K

fedeabella/cloudflare-ddns repository overview

Cloudflare Dynamic IP Updater

A simple, to the point python script to update Cloudflare DNS records with a dynamic IP. Also possible using Docker.

I recently set up my first homelab, using Cloudflare to reverse proxy into it from my own domain. But having an ISP that only provides dynamic IPs, I needed some way to update the IP in the DNS records. There's several solutions for this around, but none that I found were completely to my liking, so I decided to make my own.

Features

  • Simple python script which does the bare minimum
  • Lean Docker image when built, with no network or exposed ports needed
  • Minimal pip requirements
  • Uses Cloudflare's own python library to perform Cloudflare operations
  • Uses the Cloudflare Batch API operation to update all DNS records in a single API call
  • Caches Cloudflare DNS records so Cloudflare API only needs to be called when something changes
  • Only to 2 API calls per update cycle: one to retrieve current DNS status, and one to perform updates (if needed)

How to use

First, you'll need:

  • A Cloudflare API token, and the Zone ID, which can be provided in one of two ways:
    • A .env file which defines:
      • An API_TOKEN environment variable
      • A ZONE_ID environment variable
    • Using Docker secrets, passing the file paths to the secrets to the environment variables API_TOKEN_FILE and ZONE_ID_FILE
  • (Optional) a RUN_EVERY environment variable which contains the number of seconds to wait before the next IP check. If not included, checks every 60s.
  • A domains.json file which defines an array of DNS names to update:
    • Names should only contain the subdomain part (e.g.: if your zone is mybestcfzone.org and you want to update the bestest subdomain, only add bestest to the array)
    • The root DNS record can be added the same way as in the Cloudflare dashboard, by using @ as the subdomain name
  1. Create a docker-compose.yml file with the following content:

    services:
      app:
        image: fedeabella/cloudflare-ddns
        container_name: cloudflare-ddns
        env_file: .env
        environment:
          - TZ=America/Montevideo
        read_only: true
        restart: always
        volumes:
          - type: bind
            source: ./config
            target: /app/config
    
  2. Put the .env in the same directory as the docker-compose.yml file and create a ./config directory where the domains.json file will be put. Then run

    docker compose up -d
    

You can check the docker logs to see if the container is running the script properly:

docker logs cloudflare-ddns

Limitations

This was very much built in a couple afternoons to satisfy my personal needs, so it may not have everything you need. For example:

  • Only updates A records
  • Only updates IP values in the records, no comments, proxiable values, etc.
  • Only works on a single Cloudflare Zone
  • Does not create new records, only updates existing ones

If you find something you need, feel free to create an Issue on github and I might look into it, but no promises.

Tag summary

Content type

Image

Digest

sha256:d8cabbc86

Size

30.5 MB

Last updated

19 days ago

docker pull fedeabella/cloudflare-ddns