Docker下搭建DNS服务器

时间:2022-01-04 14:49:42
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