diff --git a/Dockerfile b/Dockerfile index 9dd1d5c..c2f7f62 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,19 @@ -FROM alpine:3.3 +FROM alpine:3.4 -MAINTAINER Leonid Makarov +MAINTAINER Leonid Makarov -RUN apk add --update \ - ca-certificates \ +RUN apk add --no-cache \ + curl \ supervisor \ dnsmasq \ - && rm -rf /var/cache/apk/* # Install docker-gen -ENV DOCKER_GEN_VERSION 0.7.1 -RUN wget https://github.com/jwilder/docker-gen/releases/download/$DOCKER_GEN_VERSION/docker-gen-linux-i386-$DOCKER_GEN_VERSION.tar.gz && \ - tar -C /usr/local/bin -xvzf docker-gen-linux-i386-$DOCKER_GEN_VERSION.tar.gz && \ - rm /docker-gen-linux-i386-$DOCKER_GEN_VERSION.tar.gz +ENV DOCKER_GEN_VERSION 0.7.3 +ENV DOCKER_GEN_TARFILE docker-gen-alpine-linux-amd64-$DOCKER_GEN_VERSION.tar.gz +RUN curl -sSL https://github.com/jwilder/docker-gen/releases/download/$DOCKER_GEN_VERSION/$DOCKER_GEN_TARFILE -O && \ + tar -C /usr/local/bin -xvzf $DOCKER_GEN_TARFILE && \ + rm $DOCKER_GEN_TARFILE # dnsmasq config dir RUN mkdir -p /etc/dnsmasq.d && \ @@ -23,12 +23,11 @@ COPY conf/dnsmasq.tmpl /etc/dnsmasq.d/dockergen.tmpl COPY conf/supervisord.conf /etc/supervisor.d/docker-gen.ini COPY entrypoint.sh /opt/entrypoint.sh -# Default IP for Drude -ENV DRUDE_IP '192.168.10.10' +# Default domain and IP for wildcard query resolution +ENV DNS_DOMAIN 'docksal' +ENV DNS_IP '192.168.64.100' ENV LOG_QUERIES false -ENV DOCKER_HOST unix:///var/run/docker.sock -VOLUME /var/run EXPOSE 53/udp ENTRYPOINT ["/opt/entrypoint.sh"] diff --git a/conf/dnsmasq.tmpl b/conf/dnsmasq.tmpl index a403846..481b0d6 100644 --- a/conf/dnsmasq.tmpl +++ b/conf/dnsmasq.tmpl @@ -1,19 +1,24 @@ -{{ range $host, $containers := groupByMulti $ "Env.DOMAIN_NAME" "," }} - {{ range $index, $container := $containers }} - {{ $addrLen := len $container.Addresses }} - {{ $address := $container }} - {{ if gt $addrLen 0 }} - {{ $address := index $container.Addresses 0 }} +{{/* template */}} +{{ define "host" }} + {{ $host := .Host }} + {{ range $index, $network := .Container.Networks }} + {{ if ne $network.IP "" }} +address=/{{ $host }}/{{ $network.IP }} {{ end }} -address=/{{ $host }}/{{ $address.IP }} {{ end }} {{ end }} -{{ range $index, $container := $ }} - {{ $addrLen := len $container.Addresses }} - {{ $address := $container }} - {{ if gt $addrLen 0 }} - {{ $address := index $container.Addresses 0 }} +{{/* Configuration via "dns.name" label */}} +{{ range $index, $container := (whereLabelExists . "dns.name") }} + {{ with $container }} + {{ $dnsname := index .Labels "dns.name" }} + {{ template "host" (dict "Container" $container "Host" (print $dnsname) "Tld" "") }} + {{ end }} +{{ end }} + +{{/* Configuration via "DNS_NAME" envitonment variables */}} +{{ range $host, $containers := groupByMulti $ "Env.DNS_NAME" "," }} + {{ range $index, $container := $containers }} + {{ template "host" (dict "Container" $container "Host" (print $host) "Tld" "") }} {{ end }} -address=/{{ $container.Name }}.docker/{{ $address.IP }} {{ end }} diff --git a/entrypoint.sh b/entrypoint.sh index b3ba95d..809f793 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,12 +3,12 @@ set -e # Default command (assuming container start) if [ "$1" = 'supervisord' ]; then - # Drude IP config for dnsmasq - touch /etc/dnsmasq.d/drude.conf - # Resolve *.drude to $DRUDE_IP - echo "address=/drude/${DRUDE_IP}" >> /etc/dnsmasq.d/drude.conf - # Reverse resolution of $DRUDE_IP to 'drude' - echo $DRUDE_IP | awk -F . '{print "ptr-record="$4"."$3"."$2"."$1".in-addr.arpa,drude"}' >> /etc/dnsmasq.d/drude.conf + # $DNS_DOMAIN IP config for dnsmasq + touch /etc/dnsmasq.d/${DNS_DOMAIN}.conf + # Resolve *.$DNS_DOMAIN to $DNS_IP + echo "address=/${DNS_DOMAIN}/${IP}" >> /etc/dnsmasq.d/${DNS_DOMAIN}.conf + # Reverse resolution of $DNS_IP to ${DNS_DOMAIN} + echo $DNS_IP | awk -v dzone=${DNS_DOMAIN} -F . '{print "ptr-record="$4"."$3"."$2"."$1".in-addr.arpa,"dzone}' >> /etc/dnsmasq.d/${DNS_DOMAIN}.conf # Turn query loggin on if [ "$LOG_QUERIES" = true ]; then