docker-compose部署coredns如何实现自建DNS服务
docker-compose部署coredns实现自建DNS服务
在系统应用中,经常会遇到需要使用 https 域名通讯的需要,在内网中,我们不需要正式在互联网上注册域名,自建一个 dns 服务就能很好的解决问题。
基本应用
本文内网为使用 docker 运行一个 coredns 服务的代码示例:
- docker-compose.yml 片段内容如下:
version: '3.7'
services:
coredns:
image: coredns/coredns:1.10.0
container_name: coredns
ports:
- 53:53/udp
volumes:
- ./coredns/Corefile:/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 为内部域名解析映射文件,
- 使用 docker-compose 的话你需要对应挂载出来:
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- hostsfile 内容示例如下:
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 来验证内部域名解析是否正常了。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Centos7 安装部署Kubernetes(k8s)集群实现过程
这篇文章主要为大家介绍了Centos7 安装部署Kubernetes(k8s)集群实现过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-11-11
dockerfile报错“/bin/sh -c yum -y install“问
文章主要讨论了解决Docker容器安装vim时出现错误的问题,指出是容器版本不兼容导致,建议使用Centos 7版本的容器,提供了一种解决方案,并鼓励读者参考此经验2026-05-05
docker compose入门helloworld的详细过程
docker-compose是基于docker的,所以我们需要先安装docker才能使用docker-compose,接下来通过本文给大家介绍docker compose入门helloworld的过程,一起看看吧2021-09-09


最新评论