gpt4 book ai didi

ubuntu - Ubuntu 上的 Docker 端口冲突

转载 作者:行者123 更新时间:2023-12-02 19:41:13 25 4
gpt4 key购买 nike

  • 我正在使用 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将入口点设置为 sh运行服务的脚本(php-fpm,nginx ...)。我用 docker-compose up运行容器。

    我的问题是:
  • 在 Ubuntu 中使用 Docker 时是否需要公开端口?
  • 如果 docker-proxy,我怎样才能让我的 IDE 调试功能工作?继续监听 9000 端口?
  • 我没有在容器中公开端口 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com