Instantly share code, notes, and snippets.
Last active
October 29, 2022 12:08
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save dginhoux/4360d7d1ec29c6bea16a22a90ad0364e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM alpine:3.16.2 | |
MAINTAINER "[email protected]" | |
LABEL maintainer="[email protected]" | |
LABEL image="ginhoux.net/powerdns-authorative" | |
LABEL tag="4.6.3-alpine-3.16.2" | |
LABEL description="" | |
ENV POWERDNS_VERSION=4.6.3 | |
ENV POWERDNS_BUILD_MODULES="bind gmysql gpgsql gsqlite3" | |
# ENV MYSQL_DEFAULT_AUTOCONF=true | |
# ENV MYSQL_DEFAULT_HOST="mysql" | |
# ENV MYSQL_DEFAULT_PORT="3306" | |
# ENV MYSQL_DEFAULT_USER="root" | |
# ENV MYSQL_DEFAULT_PASS="root" | |
# ENV MYSQL_DEFAULT_DB="pdns" | |
RUN apk --update add bash libpq sqlite-libs libstdc++ libgcc mariadb-client mariadb-connector-c lua-dev curl-dev && \ | |
apk add --virtual build-deps g++ make mariadb-dev postgresql-dev sqlite-dev curl boost-dev mariadb-connector-c-dev && \ | |
curl -sSL https://downloads.powerdns.com/releases/pdns-$POWERDNS_VERSION.tar.bz2 | tar xj -C /tmp && \ | |
cd /tmp/pdns-$POWERDNS_VERSION && \ | |
./configure --prefix="" --exec-prefix=/usr --sysconfdir=/etc/pdns --with-modules="$POWERDNS_BUILD_MODULES" && \ | |
make && \ | |
make install-strip && \ | |
cd / && \ | |
mkdir -p /etc/pdns/conf.d && \ | |
addgroup -S pdns 2>/dev/null && \ | |
adduser -S -D -H -h /var/empty -s /bin/false -G pdns -g pdns pdns 2>/dev/null && \ | |
cp /usr/lib/libboost_program_options.so* /tmp && \ | |
apk del --purge build-deps && \ | |
mv /tmp/lib* /usr/lib/ && \ | |
rm -rf /tmp/pdns-$POWERDNS_VERSION /var/cache/apk/* | |
# EXPOSE 53/tcp 53/udp | |
# ADD schema.sql /etc/pdns/ | |
# ADD pdns.conf /etc/pdns/ | |
ADD entrypoint.sh / | |
ENTRYPOINT ["/entrypoint.sh"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# usage: file_env VAR [DEFAULT] | |
# ie: file_env 'XYZ_DB_PASSWORD' 'example' | |
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of | |
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature) | |
# source: https://github.com/docker-library/mariadb/blob/master/docker-entrypoint.sh | |
file_env() { | |
local var="$1" | |
local fileVar="${var}_FILE" | |
local def="${2:-}" | |
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then | |
echo "Both $var and $fileVar are set (but are exclusive)" | |
exit 1 | |
fi | |
local val="$def" | |
if [ "${!var:-}" ]; then | |
val="${!var}" | |
elif [ "${!fileVar:-}" ]; then | |
val="$(< "${!fileVar}")" | |
fi | |
export "$var"="$val" | |
unset "$fileVar" | |
} | |
# Loads various settings that are used elsewhere in the script | |
docker_setup_env() { | |
# Initialize values that might be stored in a file | |
file_env 'MYSQL_AUTOCONF' $MYSQL_DEFAULT_AUTOCONF | |
file_env 'MYSQL_HOST' $MYSQL_DEFAULT_HOST | |
file_env 'MYSQL_DNSSEC' 'no' | |
file_env 'MYSQL_DB' $MYSQL_DEFAULT_DB | |
file_env 'MYSQL_PASS' $MYSQL_DEFAULT_PASS | |
file_env 'MYSQL_USER' $MYSQL_DEFAULT_USER | |
file_env 'MYSQL_PORT' $MYSQL_DEFAULT_PORT | |
} | |
docker_setup_env | |
# --help, --version | |
[ "$1" = "--help" ] || [ "$1" = "--version" ] && exec pdns_server $1 | |
# treat everything except -- as exec cmd | |
[ "${1:0:2}" != "--" ] && exec "$@" | |
if $MYSQL_AUTOCONF ; then | |
# Set MySQL Credentials in pdns.conf | |
sed -r -i "s/^[# ]*gmysql-host=.*/gmysql-host=${MYSQL_HOST}/g" /etc/pdns/pdns.conf | |
sed -r -i "s/^[# ]*gmysql-port=.*/gmysql-port=${MYSQL_PORT}/g" /etc/pdns/pdns.conf | |
sed -r -i "s/^[# ]*gmysql-user=.*/gmysql-user=${MYSQL_USER}/g" /etc/pdns/pdns.conf | |
sed -r -i "s/^[# ]*gmysql-password=.*/gmysql-password=${MYSQL_PASS}/g" /etc/pdns/pdns.conf | |
sed -r -i "s/^[# ]*gmysql-dbname=.*/gmysql-dbname=${MYSQL_DB}/g" /etc/pdns/pdns.conf | |
sed -r -i "s/^[# ]*gmysql-dnssec=.*/gmysql-dnssec=${MYSQL_DNSSEC}/g" /etc/pdns/pdns.conf | |
MYSQLCMD="mysql --host=${MYSQL_HOST} --user=${MYSQL_USER} --password=${MYSQL_PASS} --port=${MYSQL_PORT} -r -N" | |
# wait for Database come ready | |
isDBup () { | |
echo "SHOW STATUS" | $MYSQLCMD 1>/dev/null | |
echo $? | |
} | |
RETRY=10 | |
until [ `isDBup` -eq 0 ] || [ $RETRY -le 0 ] ; do | |
echo "Waiting for database to come up" | |
sleep 5 | |
RETRY=$(expr $RETRY - 1) | |
done | |
if [ $RETRY -le 0 ]; then | |
>&2 echo Error: Could not connect to Database on $MYSQL_HOST:$MYSQL_PORT | |
exit 1 | |
fi | |
# init database if necessary | |
echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DB;" | $MYSQLCMD | |
MYSQLCMD="$MYSQLCMD $MYSQL_DB" | |
if [ "$(echo "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = \"$MYSQL_DB\";" | $MYSQLCMD)" -le 1 ]; then | |
echo Initializing Database | |
cat /etc/pdns/schema.sql | $MYSQLCMD | |
# Run custom mysql post-init sql scripts | |
if [ -d "/etc/pdns/mysql-postinit" ]; then | |
for SQLFILE in $(ls -1 /etc/pdns/mysql-postinit/*.sql | sort) ; do | |
echo Source $SQLFILE | |
cat $SQLFILE | $MYSQLCMD | |
done | |
fi | |
fi | |
unset -v MYSQL_PASS | |
fi | |
# Run pdns server | |
trap "pdns_control quit" SIGHUP SIGINT SIGTERM | |
pdns_server "$@" & | |
wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment