【发布时间】:2020-02-21 22:23:50
【问题描述】:
我正在尝试在 docker 中运行 apache2,但是当我公开端口时,该服务只能从主机访问,而不能从外部访问。
我已经通过以下方式执行了容器:
docker run -d -t -p 8080:80 --name ctf ubuntu
之后,我在里面安装了apache2:
apt-get update && apt-get install apache2 -y && service apache2 start
当我运行docker ps 时,我会得到:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c90b8ed41436 ubuntu "/bin/bash" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp ctf
我可以从该主机通过wget <public ip>:8080 访问该服务。但是,如果我尝试从同一网络上的另一台设备上它不起作用。服务无法访问。
同样命令iptables --list会产生:
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:openvpn
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DOCKER-USER all -- anywhere anywhere
DOCKER-ISOLATION-STAGE-1 all -- anywhere anywhere
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
DOCKER all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT all -- 10.8.0.0/24 anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain DOCKER (1 references)
target prot opt source destination
ACCEPT tcp -- anywhere 172.17.0.2 tcp dpt:http
Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target prot opt source destination
DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere
RETURN all -- anywhere anywhere
你知道问题出在哪里吗?
编辑2:
来自netstat -ntlp 的结果,似乎 docker 没有将套接字绑定到 IPv4:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:10022 0.0.0.0:* LISTEN 423/./ts3server
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 13863/mysqld
tcp 0 0 127.0.0.1:37995 0.0.0.0:* LISTEN 15022/containerd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1577/apache2
tcp 0 0 0.0.0.0:30033 0.0.0.0:* LISTEN 423/./ts3server
tcp 0 0 0.0.0.0:8084 0.0.0.0:* LISTEN 1552/mono
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 19145/systemd-resol
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1561/sshd
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1577/apache2
tcp 0 0 0.0.0.0:10011 0.0.0.0:* LISTEN 423/./ts3server
tcp6 0 0 :::10022 :::* LISTEN 423/./ts3server
tcp6 0 0 :::8080 :::* LISTEN 16111/docker-proxy
tcp6 0 0 :::30033 :::* LISTEN 423/./ts3server
tcp6 0 0 :::22 :::* LISTEN 1561/sshd
tcp6 0 0 :::10011 :::* LISTEN 423/./ts3server
但这不应该是问题,因为:
root@ubuntu:~/docker# sysctl net.ipv6.bindv6only
net.ipv6.bindv6only = 0
root@ubuntu:~/docker# sysctl net.ipv6.conf.all.forwarding
net.ipv6.conf.all.forwarding = 1
【问题讨论】:
-
可以暂时禁用主机防火墙然后尝试从远程主机连接(例如
sudo service ufw stop)? -
嗨,我禁用了防火墙,但仍然是同样的问题:imgur.com/a/BAiNiWq
-
您是否尝试将
Listen 0.0.0.0:80指令添加到 /etc/apache2/ports.conf ?我在使用 ng serve 时遇到了同样的问题,并从这个问题中得到了答案:*.com/questions/46778868/… -
参考。您的图像,是否正在尝试使用它的公共 IP 访问主机?例如可以使用同一子网中机器的主机私有 IP 进行连接吗?
-
是的,我将
Listen 80更改为Listen 0.0.0.0:80...重新启动服务service apache2 restart,但没有运气:(