我正在使用 Ubuntu 并尝试使用 Xdebug(PHP 调试器)配置远程调试以在 Docker 容器上工作。
trungdq88@ubuntu:~/# ifconfig docker0 Link encap:Ethernet HWaddr 02:42:7b:c2:ec:97 inet addr:172.17.0.1 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::42:7bff:fec2:ec97/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8923 errors:0 dropped:0 overruns:0 frame:0 TX packets:13333 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:5099353 (5.0 MB) TX bytes:16082560 (16.0 MB) eth0 Link encap:Ethernet HWaddr b8:ca:3a:d3:4f:03 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
我的容器公开端口 9000(Xdebug 的工作端口)。
EXPOSE 80 443 9000
由于Ubuntu中的docker机器与真实机器相同,因此
docker-proxy
进程占用9000端口trungdq88@ubuntu:~/# sudo netstat -tulpn | grep :9000 tcp6 0 0 :::9000 :::* LISTEN 10391/docker-proxy
我的 IDE (Sublime Text) 无法启动调试 session ,因为它还需要监听端口 9000。
trungdq88@ubuntu:~/# echo "Assume that this is my IDE" | nc -l -p 9000 nc: Address already in use
我尝试了另一个端口:
在 Ubuntu 中:
trungdq88@ubuntu:~/# echo "Hello" | nc -l -p 12345
Docker 容器内部:
root@919737061a1d:/src# telnet 172.17.0.1 12345 Trying 172.17.0.1... Connected to 172.17.0.1. Escape character is '^]'. Hello Connection closed by foreign host.
(它在端口 12345 上运行)
我的
docker-compose.yml
:web: build: . ports: - "80:80" - "443:443" - "9000:9000"
Dockerfile
将入口点设置为运行服务(php-fpm、nginx...)的 sh
脚本。我使用 docker-compose up 来运行容器。
我的问题是:
- 在 Ubuntu 中使用 Docker 是否需要暴露端口?
- 如果
docker-proxy
继续监听端口 9000,如何才能使 IDE 调试功能正常工作? - 我没有在容器中公开端口 12345,但为什么最后一个示例(netcat 和 telnet)仍然有效?
最佳答案
Is it necessary to expose ports when using Docker in Ubuntu?
是的,否则端口根本不会打开。
请参阅Dockerfile EXPOSE
:
The
EXPOSE
instruction informs Docker that the container listens on the specified network ports at runtime.
EXPOSE
does not make the ports of the container accessible to the host. To do that, you must use the-p
flag to publish a range of port.
因此您还需要发布它才能从主机访问:
docker run -p 9000:9000
(来自 docker run -p hostPort:containerPort
)
如果该端口不方便(因为如果必须由 XDebug 使用),则需要更改主机端口(意味着,在容器内部,您可以继续使用 9000)
docker run -p 1245:9000
关于ubuntu - Ubuntu 上的 Docker 端口冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35532325/