docker-compose.yml
services:
ddns:
environment:
- API_TOKEN=
- ZONE_ID=
- RECORD_ID=
- DOMAIN_NAME=
image: python:slim
restart: unless-stopped
stop_grace_period: 0s
container_name: ddns
entrypoint: python -c
command:
- |
import os; os.system('pip install -q requests 2>/dev/null')
import json, time, requests
def get_current_ip():
return requests.get('https://api64.ipify.org').text
def update_dns(ip):
zone_id = os.environ['ZONE_ID']
record_id = os.environ['RECORD_ID']
api_token = os.environ['API_TOKEN']
domain_name = os.environ['DOMAIN_NAME']
url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}"
headers = {
'Authorization': f'Bearer {api_token}'
}
data = {
'type': 'A',
'name': domain_name,
'content': ip,
'ttl': 1,
'proxied': False
}
response = requests.put(url, headers=headers, json=data)
if response.status_code == 200:
print(f"DNS 레코드가 성공적으로 업데이트되었습니다. 새 IP: {ip}", flush=True)
else:
print("DNS 레코드 업데이트에 실패했습니다.", flush=True)
print(response.text, flush=True)
last_ip = None
check_interval = 6 * 3600 # 6시간
while True:
current_ip = get_current_ip()
if current_ip != last_ip:
print(f"IP 변경 감지: {last_ip} -> {current_ip}", flush=True)
update_dns(current_ip)
last_ip = current_ip
else:
print(f"IP 변경 없음: {current_ip}", flush=True)
time.sleep(check_interval)