Why won’t PHP docker container connect to database on host.docker.internal, while a mysql container does connect?

  docker, mysql, php

As a temporary way to quickly make a transition to docker I want to host some mysql databases on the WSL host for some legacy projects.

I have a mysql container running in a certain project and from that mysql container I am able to do:

mysql -h host.docker.internal -u root -ptest database

I am then successfully connected to a database on the host machine.
So, apparently I have configured mysql correctly on the host machine.

However, when in a legacy PHP project I try this, it fails to connect:

$conn = mysqli_connect('host.docker.internal', 'root', 'test');
if (!$conn) {
    header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
    die('Mysql connection failed');
}

mysqli_close($conn);
die('Mysql OK');

I’m pretty lost here… Any help will be much appreciated.

Source: Docker Questions

LEAVE A COMMENT