简体   繁体   中英

xdebug connection refused to Docker container

error:

2020/04/26 23:43:48 [error] 8#8: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.208.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://192.168.208.3:9000", host: "127.0.0.1", referrer: "http://127.0.0.1/"

Cannot make connection with xdebug. Docker configuration was taken from here https://gitlab.com/martinpham/symfony-5-docker/-/tree/master/docker

xdebug was installed separately and it's recognized by IDE.

Also added this in docker-compose.yml under php-fpm environment:

environment:
- XDEBUG_CONFIG:remote_host=host.docker.internal remote_enable=1 remote_autostart=off xdebug.idekey=PHPSTORM

What else needs to be added/modified?

This is how I setup recently docker + php + xdebug for http-services. I guided my peers through it and it worked out flawlessly.

1. Add ENV PHP_IDE_CONFIG into your docker fpm pool config

You need to add this environment into your php-fpm pool config. It could be www.conf (as an example)

env[PHP_IDE_CONFIG] = "serverName=localhost"

2. Add xdebug.ini to your docker container

Here is an example of the xdebug.ini which I used for the setup:

xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = off
xdebug.remote_host = host.docker.internal
xdebug.remote_port = 9000
xdebug.idekey = PHPSTORM
xdebug.max_nesting_level = 1500

3. [IntelliJ IDEA or PHPStorm] - Setup PHP servers

  1. Open Preferences
  2. Go to Languages & Frameworks -> PHP -> Servers
  3. Set Name to localhost (It's important and should match PHP_IDE_CONFIG value)
  4. Set Host to localhost
  5. Enable use path mappings
  6. Map your project root path to the docker workdir (eg /var/www/html ) so IntelliJ can correctly map the paths.

4. [IntelliJ IDEA or PHPStorm] - Setup IDE key

  1. Open Preferences
  2. Go to Languages & Frameworks -> PHP -> Debug -> DGBp proxy
  3. Set IDE key to PHPSTORM

5. Add XDEBUG_SESSION=PHPSTORM to your url/cookie..

Finally:

  • add a ?XDEBUG_SESSION=PHPSTORM to your url OR
  • add a cookie with the name XDBEUG_SESSION and the value PHPSTORM

Issue was in: environment:

- XDEBUG_CONFIG:remote_host=host.docker.internal remote_enable=1 remote_autostart=off xdebug.idekey=PHPSTORM
  1. it's not correctly parsed.
  2. if i pass only remote_host=host.docker.internal then it will pass "localhost" instead of host IP address.

I know the post is for PHPStorm users, but if any VSCode users stumble upon here then two things that need to be done different to PHPStorm (More in this answer for PHPStorm https://stackoverflow.com/a/61561910/3056278 ) -

  1. Pass IP address of your host (192.168...) instead of host.docker.internal
  2. Configure pathMappings in your debug launch configuration -
{
    "name": "Debug Docker",
    "type": "php",
    "request": "launch",
    "port": 9000,
    "pathMappings": {
        "/var/www/app": "${workspaceFolder}"
    }
},

Replace /var/www/app with your own path!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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