简体   繁体   English

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

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

There are many posts on SO and elsewhere on how to set this up. SO和其他地方有很多关于如何设置的帖子。 So far I've been unsuccessful in getting it working.到目前为止,我一直没有成功地让它工作。

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

Goal目标
Remotely debug PHP code in container from local machine by utilizing a reverse tunnel from the local machine to the container.利用从本地机器到容器的反向隧道,从本地机器远程调试容器中的 PHP 代码。

I have gotten this working before when the PHP code was installed locally on the host machine, so the question is not around Xdebug.当 PHP 代码安装在主机本地时,我已经得到了这个工作,所以问题不在于 Xdebug。 As soon as I moved the PHP code into the container, debugging no longer works.一旦我将 PHP 代码移动到容器中,调试就不再起作用。

What I've tried我试过的
Setting up a reverse tunnel from the local machine to the host EC2 instance works.设置从本地机器到主机 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' 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' on the host machine.在主机上nc -z localhost 9000 || echo 'no tunnel open'

When I docker exec -it container bash into the container and run nc, the tunnel is not available.当我docker exec -it container bash进入容器并运行nc时,隧道不可用。

I'm using docker-compose:我正在使用 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'

I have tried with and without mapping the 9000 port.我尝试过映射和不映射 9000 端口。 I have tried variations of the ssh tunnel:我尝试了 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 (container IP) ssh -vvv -i "aws.pem" -R 9000:172.20.0.2:9000 user@ec2instance (容器 IP)

I've also tried using ssh -L with no luck.我也试过使用ssh -L没有运气。

Several posts, like this one suggest adding GatewayPorts yes on the host machine. 几篇文章,比如这篇文章,建议在主机上添加GatewayPorts yes I've tried this as well with no change.我也试过了,没有任何变化。

I have not tried using --network=host , primarily due to security concerns.我没有尝试使用--network=host ,主要是出于安全考虑。 I also would rather not use ngrok, as I'd like to be able to use localhost or host.docker.internal for the xdebug.remote_host setting.我也不想使用 ngrok,因为我希望能够将 localhost 或host.docker.internal用于xdebug.remote_host设置。

For completeness, here is what I have for Xdebug:为了完整起见,这是我为 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"

I got this working.我得到了这个工作。 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. 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.

I updated my connect command to ssh -vvv -i "aws.pem" -R 9000:172.17.0.1:9000 user@ec2instance with the right IP and the tunnel started working.我使用正确的 IP 将我的连接命令更新为ssh -vvv -i "aws.pem" -R 9000:172.17.0.1:9000 user@ec2instance并且隧道开始工作。 I do still have GatewayPorts enabled (per the man page) and removed the 9000:9000 mapping.我仍然启用了 GatewayPorts(根据手册页)并删除了 9000:9000 映射。

I then updated my xdebug.remote_host value to the same IP and debugging is now working.然后我将我的 xdebug.remote_host 值更新为相同的 IP 并且调试现在正在工作。 Not sure why host.docker.internal didn't work, but that's for another day.不知道为什么 host.docker.internal 不起作用,但那是另一天。

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

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