ddev: ddev uses host ip address for host.docker.internal, doesn't work with vscode language server

Is there an existing issue for this?

  • I have searched the existing issues

Run a Diagnostic and Paste Link Here

Click to expand $ ddev debug test Running bash [-c /tmp/test_ddev.sh] OS Information: Linux MURZ_COMP 5.15.0-25-generic #25-Ubuntu SMP Wed Mar 30 15:54:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux User information: uid=1000(alexey_korepov) gid=1000(alexey_korepov) groups=1000(alexey_korepov),4(adm),24(cdrom),27(sudo),30(dip),44(video),46(plugdev),109(render),120(lpadmin),132(lxd),133(sambashare),997(docker) DDEV version: ITEM VALUE DDEV version v1.19.1 architecture amd64 db drud/ddev-dbserver-mariadb-10.3:v1.19.0 dba phpmyadmin:5 ddev-ssh-agent drud/ddev-ssh-agent:v1.19.0 docker 20.10.14 docker-compose v2.3.3 docker-platform MURZ_COMP mutagen 0.12.0 os linux router drud/ddev-router:v1.19.0 web drud/ddev-webserver:v1.19.0-1 ======= DDEV global info ========= Global configuration: instrumentation-opt-in=true omit-containers=[] mutagen-enabled=false nfs-mount-enabled=false router-bind-all-interfaces=false internet-detection-timeout-ms=750 disable-http2=false use-letsencrypt=false letsencrypt-email= table-style=default simple-formatting=false auto-restart-containers=false use-hardened-images=false fail-on-hook-fail=false required-docker-compose-version= use-docker-compose-from-path=false no-bind-mounts=false

======= DOCKER info ========= docker location: -rwxr-xr-x 1 root root 61890504 Mar 24 05:45 /usr/bin/docker ======= Mutagen Info ========= ======= Docker Info ========= Docker platform: MURZ_COMP Using docker context: default (unix:///var/run/docker.sock) docker-compose: v2.3.3 Using DOCKER_HOST=unix:///var/run/docker.sock Docker version: 20.10.14 Able to run simple container that mounts a volume. Able to use internet inside container. Docker disk space: Filesystem Size Used Available Use% Mounted on overlay 105.2G 74.9G 24.9G 75% /

The ddev-ssh-agent container has been removed. When you start it again you will have to use ‘ddev auth ssh’ to provide key authentication again. Existing docker containers: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES Creating a new ddev project config in the current directory (/home/alexey_korepov/tmp/tryddevproject-28837) Once completed, your configuration will be written to /home/alexey_korepov/tmp/tryddevproject-28837/.ddev/config.yaml

Found a php codebase at /home/alexey_korepov/tmp/tryddevproject-28837 Project type has no settings paths configured, so not creating settings file. Configuration complete. You may now run ‘ddev start’. Network ddev_default created Starting tryddevproject-28837… Container ddev-ssh-agent Started ssh-agent container is running: If you want to add authentication to the ssh-agent container, run ‘ddev auth ssh’ to enable your keys. Pushed mkcert rootca certs to ddev-global-cache/mkcert Network ddev-tryddevproject-28837_default Created Container ddev-tryddevproject-28837-web Started Container ddev-tryddevproject-28837-dba Started Container ddev-tryddevproject-28837-db Started Container ddev-router Started Project type has no settings paths configured, so not creating settings file. Project type has no settings paths configured, so not creating settings file. Successfully started tryddevproject-28837 Project can be reached at https://tryddevproject-28837.ddev.site https://127.0.0.1:49161

  • curl --fail -I tryddevproject-28837.ddev.site HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Thu, 28 Apr 2022 06:42:22 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Vary: Accept-Encoding

  • ‘[’ 0 -ne 0 ‘]’

  • set +x Thanks for running the diagnostic. It was successful. Please provide the output of this script in a new gist at gist.github.com Running ddev launch in 5 seconds

Current Behavior

I’ve got the problem with connecting to XDebug within a ddev-web container. It enables well, but PHP script can’t connect to debugger port. Seems this is because the host.docker.internal hostname is attached to a wrong IP.

But I’ve found a workaround how to fix this issue via manually editing /etc/hosts file, it’s described in “Steps To Reproduce”.

Expected Behavior

PHP script should connect to debugger port successfully.

Steps To Reproduce

  1. Create a new test environment via ddev config --project-type drupal9.

  2. Enable xdebug via ddev xdebug enable

  3. Attach VS Code to container, configure XDebug with default settings:

    {
      "name": "Listen for Xdebug",
      "type": "php",
      "request": "launch",
      "port": 9003
    },
  1. Create a test file /var/www/html/index.php with a simple script:
<?php echo "Hello world\n";
  1. Place a breakpoint on the line.

  2. Launch the script from console and see that xdebug can’t connect to the host:

$ php ./index.php 
Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port) :-(
Hello world
  1. Check the active network interfaces and compare it with generated /etc/hosts file:
$ sudo apt update; sudo apt install net-tools; ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.21.0.2  netmask 255.255.0.0  broadcast 172.21.255.255

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.8  netmask 255.255.0.0  broadcast 172.18.255.255

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0

$ cat /etc/hosts 
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.1      host.docker.internal
172.21.0.2      test1-web
172.18.0.8      test1-web

See that the subnet for 172.17.0.1 ip is missing.

  1. Try to move host.docker.internal to the 172.21.0.2 ip via modifying the /etc/hosts file like this:
#172.17.0.1      host.docker.internal
172.21.0.2      test1-web host.docker.internal
  1. Try to launch the php ./index.php - xdebug starts working well!

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (17 by maintainers)

Most upvoted comments

I’d love to know why you’re using the vscode language server inside the container, how it works for you, and your overall experience. And if it has great benefit, it would be wonderful to have a writeup on it somewhere.

@rfay I’ve started describing briefly the benefits here https://murz.notion.site/VS-Code-IDE-inside-Docker-container-fe31affafff747cdac8382167f3a03d9 - will post it on my blog when finished.