docker build --rm -t dns PATH_TO_DOCKERFILE
chmod +x dns.sh
mkdir -p /opt/docker && sudo mv dns.sh /opt/docker/dns
Each time you change your dns
/opt/docker/run
#!/usr/bin/env python | |
import subprocess | |
import socket | |
import fcntl | |
import struct | |
import sys | |
import os | |
if os.geteuid() != 0: | |
os.execvp("sudo", ["sudo"] + sys.argv) | |
def get_ip_address(ifname): | |
''' retreive IP from given interface name | |
''' | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
return socket.inet_ntoa(fcntl.ioctl( | |
s.fileno(), | |
0x8915, # SIOCGIFADDR | |
struct.pack('256s', ifname[:15]) | |
)[20:24]) | |
get_ip_address('eth0') # '192.168.0.110' | |
# Retreive Docker0 IP | |
docker_ip = get_ip_address('docker0') | |
if not docker_ip: | |
print('\033[31mNo IP found for interface docker\033[0m') | |
sys.exit(1) | |
print('Docker0 IP %s' % docker_ip) | |
# Restore previous resolv | |
with open('/etc/resolvconf/resolv.conf.d/head', 'r') as f: | |
resolvconf = [x.strip() for x in f if '# docker-dns' not in x] | |
with open('/etc/resolvconf/resolv.conf.d/head', 'w') as f: | |
f.write('\n'.join(resolvconf) + '\n') | |
subprocess.call(['resolvconf', '-u']) | |
# Get default DNS | |
(resolv, _) = subprocess.Popen(['docker', 'run', '--rm', 'busybox', 'cat', '/etc/resolv.conf'], stdout=subprocess.PIPE).communicate() | |
dns_servers = [x for x in [x[11:].strip() for x in resolv.splitlines() if x.startswith('nameserver ')] if x not in ('127.0.0.1', docker_ip)] | |
if not len(dns_servers): | |
print('\033[31mNo name server found. Dis you use 127.0.0.1?\033[0m') | |
sys.exit(1) | |
# Remove previous dns container | |
subprocess.Popen(['docker', 'rm', '-f', 'dns'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() | |
# Start dns container | |
subprocess.call(['docker', 'run', '-td', '--name', 'dns', '-p', '%s:53:53/udp' % docker_ip, '-v', '/var/run/docker.sock:/var/run/docker.sock', 'cc-dns']) | |
# Add container in resolvconf | |
resolvconf.append('nameserver %s # docker-dns' % docker_ip) | |
with open('/etc/resolvconf/resolv.conf.d/head', 'w') as f: | |
f.write('\n'.join(resolvconf) + '\n') | |
subprocess.call(['resolvconf', '-u']) |
{{ range $host, $containers := groupByMulti $ "Env.DOMAIN_NAME" "," }} | |
{{ range $index, $container := $containers }} | |
{{ with $address := index $container.Addresses 0 }} | |
address=/{{ $host }}/{{ $address.IP }} | |
{{ end }} | |
{{ end }} | |
{{ end }} |
FROM debian:latest | |
RUN apt-get update \ | |
&& apt-get install --no-install-recommends -y \ | |
dnsmasq \ | |
supervisor \ | |
&& apt-get clean \ | |
&& rm -r /var/lib/apt/lists/* | |
ENV DOCKER_GEN_VERSION 0.3.6 | |
RUN apt-get update \ | |
&& apt-get install --no-install-recommends -y \ | |
wget \ | |
&& wget --no-check-certificate -qO- https://github.com/jwilder/docker-gen/releases/download/$DOCKER_GEN_VERSION/docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz | tar xvz -C /usr/local/bin \ | |
&& apt-get purge -y wget \ | |
&& apt-get clean \ | |
&& rm -r /var/lib/apt/lists/* | |
ENV DOCKER_HOST unix:///var/run/docker.sock | |
ADD dnsmasq.tmpl /etc/dnsmasq.tmpl | |
ADD supervisord.conf /etc/supervisor/conf.d/docker-gen.conf | |
EXPOSE 53/udp | |
CMD ["/usr/bin/supervisord", "-n"] |
[program:docker-gen] | |
command=docker-gen -watch -only-exposed -notify "supervisorctl restart dnsmasq" /etc/dnsmasq.tmpl /etc/dnsmasq.conf | |
redirect_stderr=true | |
priority=1 | |
[program:dnsmasq] | |
command=dnsmasq -d -k | |
redirect_stderr=true |