我需要帮助在 Docker 中设置 Pi-Hole,以便与 Docker 中的 OctoPrint 一起运行。我在 Docker 中对 Pi-Hole 进行了初始设置:
- 拉出 Pi-Hole 容器
- 运行以下 bash 脚本:
#!/bin/bash
# https://github.com/pi-hole/docker-pi-hole/blob/master/README.md
PIHOLE_BASE="${PIHOLE_BASE:-$(pwd)}"
[[ -d "$PIHOLE_BASE" ]] || mkdir -p "$PIHOLE_BASE" || { echo "Couldn't create storage directory>
# Note: FTLCONF_LOCAL_IPV4 should be replaced with your external ip.
docker run -d \
--name pihole \
-p 53:53/tcp -p 53:53/udp \
-p 8000:8000 \
-e TZ="America/Chicago" \
-v "${PIHOLE_BASE}/etc-pihole:/etc/pihole" \
-v "${PIHOLE_BASE}/etc-dnsmasq.d:/etc/dnsmasq.d" \
--dns=127.0.0.1 --dns=1.1.1.1 \
--net=host \
--restart=unless-stopped \
--hostname pi.hole \
-e VIRTUAL_HOST="pi.hole" \
-e PROXY_LOCATION="pi.hole" \
-e FTLCONF_LOCAL_IPV4="216.212.8.163" \
pihole/pihole:latest
printf 'Starting up pihole container '
for i in $(seq 1 20); do
if [ "$(docker inspect -f "{{.State.Health.Status}}" pihole)" == "healthy" ] ; then
printf ' OK'
echo -e "\n$(docker logs pihole 2> /dev/null | grep 'password:') for your pi-hole: http> exit 0
else
sleep 3
printf '.'
fi
if [ $i -eq 20 ] ; then
echo -e "\nTimed out waiting for Pi-hole start, consult your container logs for more in> exit 1
fi
done;
当我运行 bash 脚本时,它会超时,因此我检查日志以查看可能存在什么问题:
2024-01-03 15:08:28: network.c.369) can't bind to socket: 0.0.0.0:80: Address already in use
Stopping lighttpd
lighttpd: no process found
它说 lighttpd 与端口 80 上的另一个服务发生冲突,该服务将是 OctoPrint,因为我已经验证了这一点。因此,我将 lighttpd 监听的端口更改为端口 8000,方法是将 /etc/lighttpd/lighttpd.conf 编辑为以下内容(并重新启动 lighttpd 服务):
server.modules = (
"mod_indexfile",
"mod_access",
"mod_alias",
"mod_redirect",
)
server.document-root = "/var/www/html"
server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
server.errorlog = "/var/log/lighttpd/error.log"
server.pid-file = "/run/lighttpd.pid"
server.username = "www-data"
server.groupname = "www-data"
server.port = 8000
# features
#https://redmine.lighttpd.net/projects/lighttpd/wiki/Server_feature-flagsDetails
server.feature-flags += ("server.h2proto" => "enable")
server.feature-flags += ("server.h2c" => "enable")
server.feature-flags += ("server.graceful-shutdown-timeout" => 5)
#server.feature-flags += ("server.graceful-restart-bg" => "enable")
# strict parsing and normalization of URL for consistency and security
# https://redmine.lighttpd.net/projects/lighttpd/wiki/Server_http-parseoptsDetails
# (might need to explicitly set "url-path-2f-decode" = "disable"
# if a specific application is encoding URLs inside url-path)
server.http-parseopts = (
"header-strict" => "enable",# default
"host-strict" => "enable",# default
"host-normalize" => "enable",# default
"url-normalize-unreserved"=> "enable",# recommended highly
"url-normalize-required" => "enable",# recommended
"url-ctrls-reject" => "enable",# recommended
"url-path-2f-decode" => "enable",# recommended highly (unless breaks app)
#"url-path-2f-reject" => "enable",
"url-path-dotseg-remove" => "enable",# recommended highly (unless breaks app)
#"url-path-dotseg-reject" => "enable",
#"url-query-20-plus" => "enable",# consistency in query string
)
index-file.names = ( "index.php", "index.html" )
url.access-deny = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
# default listening port for IPv6 falls back to the IPv4 port
include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.conf.pl"
include "/etc/lighttpd/conf-enabled/*.conf"
#server.compat-module-load = "disable"
server.modules += (
"mod_dirlisting",
"mod_staticfile",
)
我得到了完全相同的错误,端口也是相同的,都是 80。
你可能还注意到,我对 bash 脚本做了一些小改动,我将 bash 脚本中的端口更改为 8000:8000,并添加了--net=host
这可能是什么问题?
更新:
我取得了一点进展。错误略有不同,似乎端口 80 上不再有冲突。运行我的启动脚本命令时的新输出sudo ./pihole_startup.sh
如下:
gigoiy@multiserver:~ $ sudo ./pihole_startup.sh
WARNING: Localhost DNS setting (--dns=127.0.0.1) may fail in containers.
aa4cb71f5e1be1b14f4c4744fcb180ae27db68ee86463507ffcedfa071a6acd9
Starting up pihole container .......... OK
for your pi-hole: http:///admin/
当我运行命令时sudo docker container ls
:
gigoiy@multiserver:~ $ sudo docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
750ebaa202b0 pihole/pihole:latest "/s6-init" 47 seconds ago Up 45 seconds (healthy) pihole
5527f2878f24 octoprint/octoprint "/init" 54 minutes ago Up 53 seconds 80/tcp, 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp gigoiy_octoprint_1
ls
请注意,pi-hole 容器的最后一列没有绑定 IP 和端口?
现在当我跑步时sudo docker logs pihole
:
2024-01-10 19:10:05: network.c.369) can't bind to socket: 216.212.8.163:80: Cannot assign requested address
Stopping lighttpd
lighttpd: no process found
2024-01-10 19:10:07: network.c.369) can't bind to socket: 216.212.8.163:80: Cannot assign requested address
Stopping lighttpd
lighttpd: no process found
2024-01-10 19:10:08: network.c.369) can't bind to socket: 216.212.8.163:80: Cannot assign requested address
Stopping lighttpd
lighttpd: no process found
Pi-hole version is v5.17.3 (Latest: v5.17.3)
web version is v5.21 (Latest: v5.21)
FTL version is v5.24 (Latest: v5.24)
Container tag is: 2024.01.0
2024-01-10 19:10:09: network.c.369) can't bind to socket: 216.212.8.163:80: Cannot assign requested address
Stopping lighttpd
lighttpd: no process found
2024-01-10 19:10:10: network.c.369) can't bind to socket: 216.212.8.163:80: Cannot assign requested address
Stopping lighttpd
它没有说“地址已被使用”,而是简单地说“无法分配请求的地址”。不同的错误是进步!
我还发现,我有一些闲置的 docker 容器隐藏起来占用了该端口,这可能是导致之前问题的原因。如果您能看到,它在抛出错误时也给了我正确的 ip,即主机 ip。
以下是更正后的 bash 脚本:
#!/bin/bash
# https://github.com/pi-hole/docker-pi-hole/blob/master/README.md
PIHOLE_BASE="${PIHOLE_BASE:-$(pwd)}"
[[ -d "$PIHOLE_BASE" ]] || mkdir -p "$PIHOLE_BASE" || { echo "Couldn't create storage directory: $PIHOLE_BASE"; exit 1; }
# Note: FTLCONF_LOCAL_IPV4 should be replaced with your external ip.
docker run -d \
--name pihole \
-e TZ="America/Chicago" \
-e DHCP_ACTIVE=true \
-v "${PIHOLE_BASE}/etc-pihole:/etc/pihole" \
-v "${PIHOLE_BASE}/etc-dnsmasq.d:/etc/dnsmasq.d" \
--dns=127.0.0.1 --dns=1.1.1.1 \
--net=host \
--restart=unless-stopped \
--hostname pi.hole \
-e VIRTUAL_HOST="pi.hole" \
-e PROXY_LOCATION="pi.hole" \
-e FTLCONF_LOCAL_IPV4="216.212.8.163" \
pihole/pihole:latest
printf 'Starting up pihole container '
for i in $(seq 1 20); do
if [ "$(docker inspect -f "{{.State.Health.Status}}" pihole)" == "healthy" ] ; then
printf ' OK'
echo -e "\n$(docker logs pihole 2> /dev/null | grep 'password:') for your pi-hole: http://${IP}/admin/"
exit 0
else
sleep 3
printf '.'
fi
if [ $i -eq 20 ] ; then
echo -e "\nTimed out waiting for Pi-hole start, consult your container logs for more info (\`docker logs pihole\`)"
exit 1
fi
done;
我还认为 lighttpd 对我的问题没有影响,原因太多了,我懒得打出来,所以相信我吧。
答案1
如果删除--net=host
,则可以使用以下格式进行端口转发,-p 8000:80/tcp
其中 8000 是外部/主机端口,而 80 仅适用于该 docker 容器。
或者如果你需要保持主机模式:更改 lighttpd 设置里面使用docker环境变量来配置docker容器-e WEB_PORT=8000
,但请注意:
这会破坏 Pi-hole 的“网页被阻止”功能,但是它可能有助于运行 synology 或 --net=host docker 参数的高级设置。
https://github.com/pi-hole/docker-pi-hole/blob/master/README.md