繁体   English   中英

如何将 SSH 隧道反向到远程 docker 容器以进行 Xdebug?

[英]How to reverse SSH tunnel to remote docker container for Xdebug?

SO和其他地方有很多关于如何设置的帖子。 到目前为止,我一直没有成功地让它工作。

设置
本地机器 - Windows 10,安装了 Cygwin、git bash 和 WSL2 安装了 Z3D945423F8E9654064C;C 和 MacBook Air(莫哈韦沙漠)
主机 - 运行 Amazon Linux 2 的 AWS EC2 实例
Docker 容器 - CentOS 7.8 运行 PHP 和 Xdebug

目标
利用从本地机器到容器的反向隧道,从本地机器远程调试容器中的 PHP 代码。

当 PHP 代码安装在主机本地时,我已经得到了这个工作,所以问题不在于 Xdebug。 一旦我将 PHP 代码移动到容器中,调试就不再起作用。

我试过的
设置从本地机器到主机 EC2 实例的反向隧道是可行的。 For this I'm doing ssh -vvv -i "aws.pem" -R 9000:localhost:9000 user@ec2instance in terminal, cygwin, or git bash and testing with nc -z localhost 9000 || echo 'no tunnel open' 在主机上nc -z localhost 9000 || echo 'no tunnel open'

当我docker exec -it container bash进入容器并运行nc时,隧道不可用。

我正在使用 docker-compose:

version: '2'
services:
  web:
    image: 'privateregistry/project/container:latest'
    restart: always
    container_name: web
    ports:
      - '8082:80'
      - '447:443'
      - '9000:9000'
    volumes:
      - '.:/var/www/project'

我尝试过映射和不映射 9000 端口。 我尝试了 ssh 隧道的变体:

ssh -vvv -i "aws.pem" -R:9000:localhost:9000 user@ec2instance
ssh -vvv -i "aws.pem" -R 0.0.0.0:9000:localhost:9000 user@ec2instance
ssh -vvv -i "aws.pem" -R \*:9000:localhost:9000 user@ec2instance
ssh -vvv -i "aws.pem" -R 9000:172.20.0.2:9000 user@ec2instance (容器 IP)

我也试过使用ssh -L没有运气。

几篇文章,比如这篇文章,建议在主机上添加GatewayPorts yes 我也试过了,没有任何变化。

我没有尝试使用--network=host ,主要是出于安全考虑。 我也不想使用 ngrok,因为我希望能够将 localhost 或host.docker.internal用于xdebug.remote_host设置。

为了完整起见,这是我为 Xdebug 提供的内容:

[XDebug]
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_handler="dbgp"
xdebug.remote_port=9000
xdebug.remote_host="host.docker.internal"
;xdebug.remote_connect_back=1
xdebug.idekey = VSCODE
xdebug.remote_log = "/var/log/xdebug.log"

我得到了这个工作。 After reading up on the ssh man page and looking over things again, I realized I was binding to the docker container IP not the bridge (docker0) IP.

我使用正确的 IP 将我的连接命令更新为ssh -vvv -i "aws.pem" -R 9000:172.17.0.1:9000 user@ec2instance并且隧道开始工作。 我仍然启用了 GatewayPorts(根据手册页)并删除了 9000:9000 映射。

然后我将我的 xdebug.remote_host 值更新为相同的 IP 并且调试现在正在工作。 不知道为什么 host.docker.internal 不起作用,但那是另一天。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM