Docker本地DNS容器解析失败,Nginx测试页面无法访问求助
本地DNS+Nginx Docker环境搭建问题排查请求
测试结果
执行docker compose up后,域名解析测试结果如下:
$ nslookup ns.main.com ;; connection timed out; no servers could be reached
$ dig @127.0.0.1 ns.main.com ; <<>> DiG 9.18.1-1ubuntu1.2-Ubuntu <<>> @127.0.0.1 ns.main.com ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 38715 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 1232 ; COOKIE: e9ba5744ce2779c601000000633878c753c784e7d4f38f3e (good) ;; QUESTION SECTION: ;ns.main.com. IN A ;; Query time: 4 msec ;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP) ;; WHEN: Sat Oct 01 11:28:39 CST 2022 ;; MSG SIZE rcvd: 68
测试域名无法解析,对应页面也无法访问,推测环境搭建存在遗漏步骤。
当前环境与问题
操作系统为Ubuntu 22.04.1 LTS,因本地DNS与真实网络DNS冲突,执行docker compose build后执行以下命令禁用本地解析服务:
sudo systemctl stop systemd-resolved sudo systemctl disable systemd-resolved
之后执行docker compose up -d,此时出现两个问题:
- 本地DNS容器无法正常解析域名(返回SERVFAIL)
- 主机无法访问互联网
若恢复互联网访问,需执行以下命令并重启网络/WIFI:
sudo systemctl enable systemd-resolved sudo systemctl start systemd-resolved
但恢复后DNS服务又会出现冲突问题。
Docker Compose配置
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 ports: - 53:53 - 53:53/udp volumes: - ./dns/named.conf:/etc/bind/named.conf - ./dns/zone/:/etc/bind/zone/ command: named -c /etc/bind/named.conf -g -u named
环境文件结构
根目录 ├── docker-compose.yml ├── dns/ │ ├── Dockerfile │ ├── named.conf │ └── zone/ │ ├── main.com │ └── secondary.com └── nginx/ ├── Dockerfile ├── conf.d/ │ └── default.conf └── html/ ├── main/ │ └── index.html └── secondary/ └── index.html
DNS服务配置细节
Dockerfile
FROM alpine:latest RUN apk add bind openrc RUN rc-update -u named
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"; }; zone "secondary.com" IN { type master; file "/etc/bind/zone/secondary.com"; };
zone文件:main.com
$TTL 86400 @ IN SOA ns.main.com. hostmaster.main.com. ( 202 ; Serial 600 ; Refresh 3600 ; Retry 1209600) ; Expire ; 3600) ; Negative Cache TTL @ IN NS ns.main.com. ns IN A 127.0.0.1
zone文件:secondary.com
$TTL 86400 @ IN SOA ns.secondary.com. hostmaster.secondary.com. ( 202 ; Serial 600 ; Refresh 3600 ; Retry 1209600) ; Expire ;3600) ; Negative Cache TTL @ IN NS ns.secondary.com. ns IN A 127.0.0.1
Nginx服务配置细节
Dockerfile
FROM nginx:latest COPY ./html /usr/share/nginx/html RUN apt-get update && apt-get install -y procps
default.conf
server { listen 80; server_name main.com ns.main.com *.main.com; location / { root /usr/share/nginx/html/main; index index.html; } } server { listen 80; server_name secondary.com ns.secondary.com *.secondary.com; location / { root /usr/share/nginx/html/secondary; index index.html; } }
静态页面文件
main/index.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Docker Nginx</title> </head> <body> <h2>Hello from Nginx container!</h2> </body> </html>
secondary/index.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Docker Nginx</title> </head> <body> <h2>Hello from secondary</h2> </body> </html>
恳请帮忙排查问题,谢谢!
内容的提问来源于stack exchange,提问作者dannisis
相关产品推荐
相关产品推荐

