Skip to content

Add Docker support #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
.vscode/
.idea/
config/
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM alpine:latest

RUN apk add --update --no-cache bash curl yq

COPY d2c.sh /usr/local/bin

ENTRYPOINT ["/usr/local/bin/d2c.sh"]
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ ipv6 = true

When d2c.sh is run, it will process each `*.toml` TOML file in the `/etc/d2c/` directory, updating the records configured in each with the current public IP of the machine. The A / AAAA records must be created from the Cloudflare dashboard first; then d2c.sh will be able to update them with the server's public IP. Make sure to create the correct type of record (A or AAAA) according to IPv4 or IPv6, as d2c.sh will ignore misconfigured records.

### Notifications

If the `APPRISE_SIDECAR_URL` environment variable is set, a notification will be sent out to the
specified [Apprise](https://github.com/caronc/apprise-api) service when a record is updated or if a failure is encountered.

### Usage

```sh
Expand Down Expand Up @@ -139,3 +144,11 @@ bash <(curl -s https://raw.githubusercontent.com/ddries/d2c.sh/master/d2c.sh)

$ crontab -e # set cronjob to run periodically
```

### Method 3: Docker

To run this script in Docker, build the image with the Dockerfile in the repo.

See `docker-compose.yml` for an example config.

Note: you will need to find a way to run it periodically (e.g. [swarm-cronjob](https://github.com/crazy-max/swarm-cronjob) in Swarm mode).
24 changes: 20 additions & 4 deletions d2c.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ print_usage() {
'
}

send_notification() {
if [[ "${APPRISE_SIDECAR_URL}" != "" ]]; then
status=$(curl --no-progress-meter -X POST -H "Content-Type: application/json" \
--data "{\"title\": \"$1\", \"body\": \"$2\", \"type\": \"$3\"}" "${APPRISE_SIDECAR_URL}")
echo "Sending notification: $status"
fi
}

# print usage if requested
if [ "$1" = "help" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
print_usage
Expand Down Expand Up @@ -114,7 +122,7 @@ for config_file in $(ls ${config_file_dir}*.toml 2>/dev/null | sort -V); do
if [ "$name" = "$c_name" ] && [ "$type" = "$c_type" ]; then
if [ "$public_ip" != "$content" ]; then
# update DNS
curl --silent --request PATCH \
result_json=$(curl --no-progress-meter --request PATCH \
--url "${cloudflare_base}/zones/${zone_id}/dns_records/${id}" \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer ${api_key}" \
Expand All @@ -125,9 +133,17 @@ for config_file in $(ls ${config_file_dir}*.toml 2>/dev/null | sort -V); do
"type": "'${c_type}'",
"comment": "Managed by d2c.sh",
"ttl": '${ttl}'
}' > /dev/null

echo "[d2c.sh] OK: ${name}"
}')
update_successful=$(echo "$result_json" | grep -o '"success": *[^,}]*' | awk -F':' '{print $2}')

if [ "$update_successful" == "true" ]; then
echo "[d2c.sh] OK: ${name}"
send_notification "[d2c.sh] DNS updated" "Updated successfully: ${name}" "success"
else
echo "[d2c.sh] Failure: ${name}"
echo $result_json
send_notification "[d2c.sh] DNS update error" "Failed to update: ${name}" "failure"
fi
else
echo "[d2c.sh] ${name} did not change"
fi
Expand Down
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
services:
d2c.sh:
build: .
container_name: d2c.sh
volumes:
- /config:/etc/d2c:ro
environment:
- APPRISE_SIDECAR_URL=http://apprise:8000/notify
depends_on:
- apprise

apprise:
image: caronc/apprise
container_name: apprise
environment:
- APPRISE_STATELESS_URLS=${DISCORD_WEBHOOK} # example
- APPRISE_STATEFUL_MODE=disabled
- APPRISE_WORKER_COUNT=1