天天看點

Docker下搭建DNS伺服器

1.拉鏡像

docker pull andyshinn/dnsmasq

2.dns挂載配置檔案

mkdir -p /dockerdata/docker-dns

cd /dockerdata/docker-dns

vi resolv.dnsmasq

nameserver 202.96.209.133

nameserver 114.114.114.114

nameserver 8.8.8.8

vi dnsmasqhosts

192.168.14.51 vhost1-vm1 vhost1-vm1.localdomain

192.168.14.52 vhost1-vm2 vhost1-vm2.localdomain

...

注意windows用戶端一定要使用有字尾域名,否則不能使用

vi dnsmasq.conf

resolv-file=/etc/resolv.dnsmasq

addn-hosts=/etc/dnsmasqhosts

3.docker啟動檔案

vi dns.yaml

[plain] 
   ​​view plain​​​
    ​​​copy​​
   
   
 
1. version: "2"
2. services:
3. docker-dns:
4. container_name: docker-dns
5. image: andyshinn/dnsmasq
6. hostname: docker-dns
7. volumes:
8. - /dockerdata/docker-dns/resolv.dnsmasq:/etc/resolv.dnsmasq
9. - /dockerdata/docker-dns/dnsmasqhosts/:/etc/dnsmasqhosts
10. - /dockerdata/docker-dns/dnsmasq.conf/:/etc/dnsmasq.conf
11. - /etc/localtime:/etc/localtime:ro
12. ports:
13. - 53:53/tcp
14. - 53:53/udp
15. cap-add: NET_ADMIN
16. restart: on-failure:1      

4.運作容器

docker-compose -f ./dns.yaml up -d

5.進入容器(不是bash,是/bin/sh)

​​dnsmasq​​​ requires ​​NET_ADMIN​​ capabilities to run correctly. Start it with something like ​​docker run -p 53:53/tcp -p 53:53/udp --cap-add=NET_ADMIN andyshinn/dnsmasq:2.75​​.The configuration is all handled on the command line (no wrapper scripts here). The ​​ENTRYPOINT​​ is ​​dnsmasq -k​​ to keep it running in the foreground. If you wanted to send requests for an internal domain (such as Consul) you can forward the requests upstream using something like ​​docker run -p 53:53/tcp -p 53:53/udp --cap-add=NET_ADMIN andyshinn/dnsmasq:2.75 -S /consul/10.17.0.2​​. This will send a request for ​​redis.service.consul​​ to ​​10.17.0.2​​