Keep a dynamic IP updated in Cloudflare DNS with Docker
Most home and small-office internet connections get a public IP address that changes now and then. If you host something at that address, such as a website, a VPN or a media server, your DNS record goes stale every time the address changes, and the service becomes unreachable until you fix it by hand.
Dynamic DNS (DDNS) solves this by checking your public IP regularly and updating the DNS record whenever it changes. If your domain's DNS is on Cloudflare, you can do this yourself with Cloudflare's API and a tiny container. No third-party DDNS service is needed. This guide builds that setup with Docker, using example.com as a placeholder for your domain.
How it works
- The updater asks the internet what your current public IP is.
- It compares that address with the record in Cloudflare.
- If they differ, it updates the record through the Cloudflare API. If the record does not exist yet, it can create it.
- It repeats on a schedule, every few minutes by default.
When your IP changes, there is a short gap, up to the check interval plus DNS caching time, before the new address takes effect. For a home server that is normally acceptable.
Requirements
- A domain whose DNS is hosted on Cloudflare
- A machine that is always on, on the same network as the service you want to reach, with Docker and Docker Compose
- A real public IP address. If your provider puts you behind CGNAT (carrier-grade NAT), the address the updater sees is shared by many customers, and inbound connections will not reach you no matter what DNS says. In that case you need a small VPS or tunnel in front of your home network.
1. Create a scoped API token
Never use your global API key. Create a token that can only edit DNS for one zone:
- In the Cloudflare dashboard, open My Profile → API Tokens → Create Token.
- Start from the Edit zone DNS template.
- Under Zone Resources, choose your specific zone,
example.com. - Create the token and copy it. You will only see it once.
If the updater later reports that it cannot find your zone, add the Zone → Zone → Read permission to the token as well.
2. Folder layout and environment file
sudo mkdir -p /opt/cloudflare
cd /opt/cloudflare
Create /opt/cloudflare/.env:
CLOUDFLARE_API_TOKEN=your_token_here
DOMAINS=example.com,www.example.com,vpn.example.com
PROXIED=false
TTL=1
IP6_PROVIDER=none
Then restrict who can read it, since it holds a credential:
sudo chown 1001:1001 .env
sudo chmod 600 .env
What the settings do:
DOMAINS: the hostnames to keep updated, separated by commas. Wildcards such as*.example.comare supported.PROXIED: whether Cloudflare's proxy sits in front of the record. More on this below.TTL=1: means "automatic" in Cloudflare. It is ignored for proxied records.IP6_PROVIDER=none: turns off IPv6 updates. Remove it if you have working IPv6 and want AAAA records managed as well.
Keep the file out of any git repository.
3. The Compose file
Create /opt/cloudflare/compose.yaml. The service and container are both simply named cloudflare:
services:
cloudflare:
image: favonia/cloudflare-ddns:latest
container_name: cloudflare
restart: unless-stopped
user: "1001:1001"
network_mode: host
read_only: true
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
env_file: .env
The hardening options matter because this container holds a token that can change your DNS:
user: "1001:1001": runs it as an unprivileged user instead of root.read_only: true: the container's filesystem cannot be written to.cap_drop: ALL: removes all Linux capabilities it does not need.no-new-privileges: stops processes from gaining extra rights.network_mode: host: lets the updater see the machine's real network interfaces, which keeps IP detection reliable, especially for IPv6.
Start it and watch the first run:
docker compose up -d
docker logs -f cloudflare
You should see it detect your public IP and then either update or create each record.
4. Proxied or DNS-only?
Each record in Cloudflare is either proxied (orange cloud) or DNS only (grey cloud):
- Proxied (
PROXIED=true): visitors connect to Cloudflare, which forwards traffic to you. You get caching, DDoS protection and your real IP stays hidden. It only works for web traffic on supported HTTP and HTTPS ports. - DNS only (
PROXIED=false): the record resolves straight to your IP. This is required for anything that is not plain web traffic, such as SSH, a WireGuard VPN, mail or a game server. It also exposes your IP address to anyone who looks it up.
If you need both, run the updater with different settings per hostname. The project supports a per-domain expression for PROXIED, which its documentation describes. Another simple approach is to run two small stacks with different DOMAINS and PROXIED values.
Also note that serving large amounts of video or other media through the free proxy is restricted by Cloudflare's terms. Use a DNS-only record for those.
5. Verify it works
Compare what the internet says your IP is with what DNS returns:
curl -s https://api.ipify.org; echo
dig +short vpn.example.com @1.1.1.1
For a DNS-only record the two should match. Proxied records return Cloudflare's addresses instead, so check those in the dashboard under DNS → Records, where the content column shows your real IP.
To test a change, you can edit the record to a wrong IP in the dashboard and wait for the next check. The updater should correct it and log the change.
Alternative: a shell script
If you prefer no container, a script plus cron does the same job. It needs curl and jq:
#!/bin/bash
TOKEN="your_token"
ZONE="example.com"
NAME="vpn.example.com"
IP=$(curl -s https://api.ipify.org)
ZID=$(curl -s -H "Authorization: Bearer $TOKEN" \
"https://api.cloudflare.com/client/v4/zones?name=$ZONE" | jq -r '.result[0].id')
RID=$(curl -s -H "Authorization: Bearer $TOKEN" \
"https://api.cloudflare.com/client/v4/zones/$ZID/dns_records?type=A&name=$NAME" | jq -r '.result[0].id')
curl -s -X PATCH -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
--data "{\"content\":\"$IP\"}" \
"https://api.cloudflare.com/client/v4/zones/$ZID/dns_records/$RID" > /dev/null
Save it as /usr/local/bin/cf-ddns.sh, make it executable, and add */5 * * * * /usr/local/bin/cf-ddns.sh with crontab -e. The record must already exist, and the script has no error handling, which is the main reason to prefer the container.
Security checklist
- Scope the token to one zone and only the permissions it needs.
- Keep the token in a file readable only by the service user, never in a compose file you share.
- Keep the container hardened as shown, and update the image from time to time.
- Remember that a DNS-only record reveals your home IP. Do not publish services you have not secured.
- If the token leaks, revoke it in the dashboard and create a new one. That takes effect immediately.
Updating
cd /opt/cloudflare
docker compose pull
docker compose up -d
Troubleshooting
- Authentication or permission errors: the token is wrong, expired, or lacks DNS edit rights for that zone. Recreate it from the Edit zone DNS template.
- "Zone not found" or similar: add Zone Read to the token, and check that the domain you listed actually belongs to the zone the token covers.
- The record shows an IP that is not yours: you may be behind CGNAT, using a VPN on that machine, or the container is detecting its own network rather than the router's.
network_mode: hostand checkingcurl https://api.ipify.orgon the host helps tell them apart. - IPv6 errors in the logs: set
IP6_PROVIDER=noneif your network has no working IPv6. - DNS still returns the old address: lookups may be cached. Wait for the TTL to pass, or query
1.1.1.1directly. A very high TTL on a DNS-only record slows recovery after an IP change. - Container restarts repeatedly: read
docker logs cloudflare. A typo in.envor a missing token is the usual cause. - Updates work but the service is unreachable: DNS is not the whole story. Check the router's port forwarding, the host firewall, and whether the record should be proxied.
Summary
A small hardened container and a scoped API token are enough to keep Cloudflare DNS pointing at a changing home IP. Choose proxied records for ordinary websites and DNS-only records for everything else, verify with dig, and keep the token private. Pair it with a reverse proxy such as Caddy for the web services and a VPN for the rest.