1

Host: ubuntu 22.04, docker image: php:7.2-fpm (+nginx:alpine), laravel: 5.2.

For some legacy laravel project I need to test some feature that uses sphinx. It's not configured locally so it's easier to connect to server where it's already working. I use docker for my local setup and I want to connect from docker container to sphinx that is on remote server using ssh port forwarding ssh -f -N <username>@<host> -L <port>:<ip>:<port>

In my docker-compose.yaml there is extra_hosts setting for accessing local machine from inside of the php container.

extra_hosts:
 - "host.docker.internal:host-gateway"

Inside the vendor code it's trying to connect like this:

 $conn = mysqli_init();
 ...
 $conn->real_connect($data['host'], null, null, null, (int) $data['port']);

host is http://host.docker.internal.

It gives me an error:

mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution. Without http:// doesn't work as well. 

If I try this directly on my local machine (host: 127.0.0.1) after forwarding port - connection is established, real_connect works just fine.

I feel like there should be a special service ip for my local host (additionally to host.docker.internal) or a way to set it up for that kind of thing.

UPD: if I use just host.docker.internal, mysqli gives me "Connection refused" error.

UPD2: Tried host gateway ip (172.25.0.1) from docker network inspect. Same "Connection refused".

root@e279887dc67b:/var/www# nslookup host.docker.internal
Server:     127.0.0.11
Address:    127.0.0.11#53

** server can't find host.docker.internal: NXDOMAIN

root@e279887dc67b:/var/www# nslookup 172.25.0.1
1.0.25.172.in-addr.arpa name = maksim-linux-laptop.
1.0.25.172.in-addr.arpa name = maksim-linux-laptop.local.
Phil
  • 157,677
  • 23
  • 242
  • 245
barmaxon
  • 199
  • 1
  • 15
  • What is `host-gateway`? You shouldn't need any special mapping for `host.docker.internal`, by default it maps to the host IP. You also most definitely do not want to include `http://` in your MySQLi _host_ parameter – Phil May 30 '23 at 05:50
  • @Phil https://stackoverflow.com/a/43541681/9696145 seems like mapping is needed, no? – barmaxon May 30 '23 at 06:14
  • Are you running this on a Linux host? – Phil May 30 '23 at 06:17
  • @Phil yes, I am – barmaxon May 30 '23 at 06:19
  • Right, sorry. Not many questions on here using Linux hosts so I just assume Mac or WIndows. Just to confirm, are you only using the `docker` CLI tools or have you installed Docker Desktop for Linux? – Phil May 30 '23 at 06:21
  • Only CLI, didn't even know about Docker desktop for linux. – barmaxon May 30 '23 at 06:23
  • "Without http:// doesn't work as well" - what do you mean by that? MySQL does not communicate over HTTP – Nico Haase May 30 '23 at 06:23
  • @NicoHaase Yea my bad, I guess I was just trying anything. – barmaxon May 30 '23 at 06:23
  • What details are you using for the ports? What is `$data['port']` and what are you using in your `ssh` command `-L` option – Phil May 30 '23 at 06:25
  • can you connect to the cli in the image and `ping` the remote host? have you tried with the ip address? or maybe a `nslookup hostname` (the docker container may not have dns resolution set up correctly) – JoSSte May 30 '23 at 06:29
  • @Phil Well, I'm not ready to share here my productions creds. $data['port'] = in my ssh forwarding. For -L it's @, the standard way of connecting to ssh. I don't think it's an ssh connection since, as I said, it is working directly from the local machine setup (php, nginx) using the same php version. – barmaxon May 30 '23 at 06:31
  • I'm not asking for credentials, just the syntax you're using for `-L`. Is it `-L 3306:localhost:3306`, `-L localhost:3306:localhost:3306`, etc? – Phil May 30 '23 at 06:34
  • @Phil first one. I guess I'll try second one? But instead of localhost though ip: 3306::3306 – barmaxon May 30 '23 at 06:37
  • I'm not asking you to try anything! Just please clarify what you **are using**. You most definitely do not want the second one which is why I was asking. – Phil May 30 '23 at 06:38
  • @Phil Ok ok, gotcha :D – barmaxon May 30 '23 at 06:43
  • 1
    @JoSSte Updated the question to show output of nslookup. I tried host gateway ip as well. maksim-linux-laptop is my linux machine name. – barmaxon May 30 '23 at 06:46
  • 1
    Honestly don't think this is too broad (have voted to re-open). Seems straight forward... 1) For some reason, `host.docker.internal` is not resolving in the container. This is probably a simple fix regarding docker network modes or something. 2) The container is not able to connect through the host's SSH tunnel. This is the real question here – Phil May 30 '23 at 08:18

0 Answers0