服务器 > 服务器 > 云虚拟主机

docker-compose部署coredns如何实现自建DNS服务

31人参与 2025-02-14 云虚拟主机

docker-compose部署coredns实现自建dns服务

在系统应用中,经常会遇到需要使用 https 域名通讯的需要,在内网中,我们不需要正式在互联网上注册域名,自建一个 dns 服务就能很好的解决问题。

基本应用

本文内网为使用 docker 运行一个 coredns 服务的代码示例:

version: '3.7'
services:
        
	coredns:
		image: coredns/coredns:1.10.0
		container_name: coredns
		ports:
           - 53:53/udp
        volumes:
           - ./coredns/corefile:/corefile
.:53 {
    hosts {
        192.168.1.11 test.com
        192.168.1.12 test1.com
        fallthrough
    }
    forward . 8.8.8.8:53 114.114.114.114:53
    log
}

其中 forward 指向上级 dns 服务

独立hosts文件方式

我们还可以将 hosts 独立出来为一个单独的文件,

.:53 {
    hosts /etc/coredns/hostsfile {
        fallthrough
    }
    forward . 8.8.8.8:53 114.114.114.114:53
    log
}

其中 /etc/coredns/hostsfile 为内部域名解析映射文件,

version: '3.7'
services:

  coredns:
    image: coredns/coredns:1.10.0
    container_name: coredns
    ports:
      - 53:53/udp
    volumes:
      - ./coredns/hostsfile:/etc/coredns/hostsfile
      - ./coredns/corefile:/corefile
192.168.1.11 test.com
192.168.1.12 test1.com

使用

以 linux 为例,修改配置文件 cat /etc/resolv.conf 设置 nameserver 为运行的这个 dns 服务ip地址即可,

[root@localhost /]# cat /etc/resolv.conf 

# generated by networkmanager

nameserver 192.168.1.2

保持后就可以使用 nslookup 或者 ping 来验证内部域名解析是否正常了。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

解决docker容器设置DNS不生效的问题

02-14

Docker容器间通信之BIP使用及配置方式

02-14

如何使用Docker部署最新版JupyterHub

02-14

docker如何为容器指定虚拟网卡或IP

02-14

Docker中安装和配置Apache Pulsar实现

02-14

docker-compose如何自定义network

02-14

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论