Docker多网络场景下如何为pi-hole不同端口绑定指定网络
实现方案
核心逻辑
- 为Pi-hole容器同时接入Traefik专用的反向代理内网,供Traefik访问80端口实现统一域名、TLS配置
- 不对80端口做对外发布,仅允许同代理网络内的Traefik访问该端口
- 53/853 DNS端口以host模式发布到宿主机,绕过Docker默认的NAT机制,保留DNS请求的真实源IP
1. Docker Compose 配置(推荐)
version: '3' # 声明已提前创建的Traefik专用代理网络,Traefik自身也需加入该网络 networks: traefik-proxy: external: true services: pihole: image: pihole/pihole:latest container_name: pihole restart: unless-stopped networks: - traefik-proxy # 仅发布DNS相关端口,长格式配置mode=host可保留请求源IP ports: # 53 TCP端口 - target: 53 published: 53 protocol: tcp mode: host # 53 UDP端口 - target: 53 published: 53 protocol: udp mode: host # 853 DoT端口 - target: 853 published: 853 protocol: tcp mode: host # 按需替换为你自己的Pi-hole环境变量配置 environment: TZ: Asia/Shanghai WEBPASSWORD: 你自己的管理密码 # 按需替换为你自己的持久化目录配置 volumes: - ./pihole/etc:/etc/pihole - ./pihole/dnsmasq.d:/etc/dnsmasq.d # Traefik反向代理标签,和你其他接入Traefik的服务配置逻辑一致 labels: - "traefik.enable=true" - "traefik.http.routers.pihole.rule=Host(`pihole.你的自定义内部域名`)" - "traefik.http.routers.pihole.entrypoints=https" - "traefik.http.routers.pihole.tls=true" - "traefik.http.services.pihole.loadbalancer.server.port=80"
若宿主机存在多网卡,需要将DNS端口仅绑定到指定内网IP,只需将
published字段的值改为内网IP:端口即可,例如published: 192.168.1.10:53。
2. Docker Run 命令示例
docker run -d \ --name pihole \ --restart unless-stopped \ --network traefik-proxy \ --publish mode=host,target=53,published=53,protocol=tcp \ --publish mode=host,target=53,published=53,protocol=udp \ --publish mode=host,target=853,published=853,protocol=tcp \ -e TZ=Asia/Shanghai \ -e WEBPASSWORD=你自己的管理密码 \ -v ./pihole/etc:/etc/pihole \ -v ./pihole/dnsmasq.d:/etc/dnsmasq.d \ -l traefik.enable=true \ -l traefik.http.routers.pihole.rule=Host\(\`pihole.你的自定义内部域名\`\) \ -l traefik.http.routers.pihole.entrypoints=https \ -l traefik.http.routers.pihole.tls=true \ -l traefik.http.services.pihole.loadbalancer.server.port=80 \ pihole/pihole:latest
验证方法
- 访问你配置的Pi-hole自定义域名,确认管理界面可正常打开,TLS证书生效
- 在内网其他设备上设置DNS为宿主机的内网IP,触发几次DNS查询后,查看Pi-hole管理界面的查询日志,确认日志中显示的是客户端设备的真实内网IP,而非172开头的Docker网关IP即配置生效
内容的提问来源于stack exchange,提问作者Jurian Sluiman
相关产品推荐
相关产品推荐

