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
- version: "2"
- services:
- docker-dns:
- container_name: docker-dns
- image: andyshinn/dnsmasq
- hostname: docker-dns
- volumes:
- - /dockerdata/docker-dns/resolv.dnsmasq:/etc/resolv.dnsmasq
- - /dockerdata/docker-dns/dnsmasqhosts/:/etc/dnsmasqhosts
- - /dockerdata/docker-dns/dnsmasq.conf/:/etc/dnsmasq.conf
- - /etc/localtime:/etc/localtime:ro
- ports:
- - 53:53/tcp
- - 53:53/udp
- cap-add: NET_ADMIN
- restart: on-failure:1
4.运行容器
docker-compose -f ./dns.yaml up -d
5.进入容器(不是bash,是/bin/sh)
docker exec -it docker-dns /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