我正在 Ubuntu 22.04 上配置 Nginx 和本地 DNS 设置。
Docker 上运行的 DNS 服务器出现错误。解析域名时,Docker DNS 服务器返回 SERVFAIL。
nslookup ns.main.com
;; communications error to 127.0.0.1#53: connection refused
;; communications error to 127.0.0.1#53: connection refused
;; communications error to 127.0.0.1#53: connection refused
Server: 192.168.1.1
Address: 192.168.1.1#53
** server can't find ns.main.com: NXDOMAIN
第一个问题发生在使用 docker-compose 构建 Docker 容器时,尝试使用 docker-compose up 启动它们时出现错误。错误表明我的 Ubuntu 主机上的 systemd-resolved 已使用端口 53。
我能够按照概述的步骤解决此冲突并让容器运行Ubuntu:如何释放 systemd-resolved 使用的端口 53这有助于从 systemd-resolved 中释放端口 53。
/etc/resolv.conf
nameserver 127.0.0.1
nameserver 192.168.1.1
nameserver fe80::1%3
search .
sudo lsof-i:53
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 4079 juan 372u IPv6 116841 0t0 UDP *:domain
我仍然不确定解决故障的正确方法。一些信息表明 IPv6 可能导致了问题,我应该避免使用它,但我不知道这是否是正确的做法。我不知道根本原因。
docker-compose.yml
services:
nginx:
build:
context: ./nginx/
ports:
- 80:80
volumes:
- ./nginx/html/:/usr/share/nginx/html/
- ./nginx/conf.d/:/etc/nginx/conf.d/
dns:
build:
context: ./dns/
restart: always
network_mode: host
# ports:
# - 53:53
# - 53:53/udp
volumes:
- ./dns/var/log/named.log:/var/log/named.log
- ./dns/named.conf:/etc/bind/named.conf
- ./dns/zone/:/etc/bind/zone/
command: named -c /etc/bind/named.conf -g -u named
dns/Dockerfile
FROM alpine:latest
RUN apk add bind openrc
RUN rc-update -u named
dns/named.conf
options {
directory "var/bind";
allow-transfer { "none"; };
allow-query { any; };
listen-on { any; };
};
zone "main.com" IN {
type master;
file "/etc/bind/zone/main.com";
};
dns/zone/main.com
$ttl 86400
@ IN SOA ns.main.com. hostmaster.main.com.(
202 ; Serial
600 ; Refresh
3600 ; Retry
12378237) ; Expire
@ IN NS ns.main.com.
ns IN A 127.0.0.1
nginx/conf.d/default.conf
server {
listen 80;
server_name ns.main.com;
location / {
root /usr/share/nginx/html/main;
index index.html;
}
}
nginx/Dockerfile
FROM nginx:latest
COPY ./html /usr/share/nginx/html
RUN apt-get update && apt-get install -y procps
答案1
在您不断的帮助下,我终于可以让它正常工作了。位于“dns/zone/main.com”的区域文件存在错误,但我现在已经解决了该问题。感谢您的帮助!
答案2
我没有看到你映射网络,默认情况下 dicker 创建了自己的网络
一个解决方法也可以包括network_mode:host
在你的docker compose中
https://docs.docker.com/compose/compose-file/compose-file-v3/
对于 DNS 部分,我还将使用官方的 docker 镜像 https://hub.docker.com/r/internetsystemsconsortium/bind9
静态IP
`version: '3'
services:
nginx:
build:
context: ./nginx/
ports:
- "80:80"
volumes:
- ./nginx/html/:/usr/share/nginx/html/
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
dns:
ipv4_address: 10.5.0.2
dns:
build:
context: ./dns/
restart: always
ports:
- "53:53"
- "53:53/udp"
volumes:
- ./dns/var/log/named.log:/var/log/named.log
- ./dns/named.conf:/etc/bind/named.conf
- ./dns/zone/:/etc/bind/zone/
command: named -c /etc/bind/named.conf -g -u named
networks:
dns:
ipv4_address: 10.5.0.5
networks:
dns:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/24
gateway: 10.5.0.1`