我如何让xdebug在远程服务器上的Docker中工作

时间:2018-09-18 08:00:25

标签: php docker docker-compose phpstorm xdebug

所以,我花了大约两天的时间,无法使xdebug在远程服务器上的容器(docker-compose config)中工作

我总是得到E: Time-out connecting to the client. :-(

Dev机器:

  • 具有动态IP的Win10
  • 所有代码均与远程服务器实时同步(通过“部署”功能)
  • PhpStorm配置为侦听端口9001,“侦听PHP调试连接”已打开。
  • 没有服务器等,只有零配置调试会话。

远程服务器:

xdebug配置:

xdebug.remote_enable=1
xdebug.remote_port=9001
xdebug.remote_log="/var/www/xdebug.log"
xdebug.remote_connect_back=1

php-fpm,jwilder / nginx-proxy,letsencrypt-companion,nginx,jeroenpeeters / docker-ssh通过docker-compose运行。

这是docker-compose.yml的简化版:

version: "2"

networks:
  default:
    external:
      name: nginx-proxy

services:

  nginx-proxy:
    container_name: nginx-proxy
    image: jwilder/nginx-proxy
    ports:
    - "80:80"
    - "443:443"
    volumes:
    - /var/run/docker.sock:/tmp/docker.sock:ro
    - ./secured/certs_letsencrypt:/etc/nginx/certs:ro
    - /etc/nginx/vhost.d
    - /usr/share/nginx/html
    restart: always

  letsencrypt-companion:
    container_name: letsencrypt-companion
    image: jrcs/letsencrypt-nginx-proxy-companion
    volumes:
    - ./secured/certs_letsencrypt:/etc/nginx/certs:rw
    - /var/run/docker.sock:/var/run/docker.sock:ro
    volumes_from:
    - nginx-proxy
    restart: always

  my_project_nginx:
    container_name: my_project_nginx
    build:
      context: ./containers/nginx_ssl
      args:
        appcontainer: my_project_app
        domain: example.com
    depends_on:
    - nginx-proxy
    links:
    - my_project_app
    - nginx-proxy
    environment:
    - VIRTUAL_HOST=example.com
    - LETSENCRYPT_HOST=example.com
    - LETSENCRYPT_EMAIL=stepan@example.com
    restart: always

  my_project_app:
    container_name: my_project_app
    build:
      context: .
      dockerfile: ./containers/php7_1/Dockerfile
      args:
        idrsafile: ./secured/id_rsa_shared
        php_memory_limit: 6G
    depends_on:
    - nginx-proxy
    restart: always

  my_project_ssh:
    container_name: my_project_ssh
    image: jeroenpeeters/docker-ssh
    depends_on:
    - my_project_app
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - ./secured/authorized_keys:/authorized_keys
    ports:
    - "2227:22"
    environment:
    - FILTERS={"name":["my_project_app"]}
    - AUTH_MECHANISM=publicKey
    - AUTHORIZED_KEYS=/authorized_keys
    restart: always 

问题

xdebug日志:

... 
I: Checking remote connect back address.
I: Remote address found, connecting to 5.18.238.83:9001.
E: Time-out connecting to the client. :-(

我尝试了很多变体(指定xdebug.remote_host,通过Putty建立SSH隧道等),但仍然没有结果。

1 个答案:

答案 0 :(得分:0)

默认情况下,docker容器位于隔离的网络(网络模式网桥)中,出于安全原因,不允许其访问主机。

如果需要从容器连接主机,则必须简化隔离级别。您可以通过将网络模式更改为“主机”来实现。

在docker compose中,您可以这样做:

network_mode:“主机”(doc