No such file or directory in /var/www/html/index.php

I finally got docker/xdebug/ & vs code to play together, now I’m having an issue when I load my page I get the following:

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /var/www/html/index.php on line *5*
PDOException: SQLSTATE[HY000] [2002] No such file or directory in /var/www/html/index.php on line *5*

When I do a google search it seems most of the results are specific to laravel which I’m not using. I know one site suggested that I do something (I don’t remember what at this point but I’ve tried pretty much everything I’ve read) with the ip results after running:
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
which I did try a time or two. But using that seems tricky if I am using the result in my docker-compose.yml file as I would already need to know the correct ip to have it in the yml file to begin with, and it change from one
docker-compose up --build to the next (I am under the impression that anytime I change anything in docker-compose.yml or Dockerfile that the container needs to be rebuilt).

There error is happing when the debugger reaches the PDO instantiation line.


$dsn = "mysql:host=localhost;dbname=product_db;charset=utf8;port=3306";

$pdo = new PDO($dsn, 'product_db_user', 'secret', [

$stmt = $pdo->query('SELECT * FROM product');

$product = $stmt->fetchAll(PDO::FETCH_ASSOC);




version: '3.1'

      context: .
      dockerfile: Dockerfile
    stdin_open: true
    tty: true
    image: mariadb:10.6
    restart: always
      MYSQL_ROOT_PASSWORD: notSecureChangeMe
      MYSQL_USER: product_db_user
      MYSQL_PASSWORD: secret
    image: phpmyadmin
    restart: always
      - 8082:80

php.ini & xdebug.ini








Thanks for any help, this container thing has been a major uphill battle.

Is the PDO object correctly installed in your PHP image? Maybe share the Dockerfile.

Update: searching for “PDOException: SQLSTATE[HY000] [2002]” indicates that the DB can’t be reached. Use the service name instead of localhost inside container.

Thanks. I added:

to my php.ini file and my xdebug.ini file and then changed localhost to my service just as you said and now it’s working, thanks a ton, this has been a major battle for too long now.

Small, relativly unimportant issue left, when I reload my web page, xdebug in vs code walks through the code twice, any ideas why it’s running this script twice on on reload, every time. Not a huge issue. Thanks again!

Are you sure it is not the script that actually runs twice? For example the browser tries to load a favicon, but everything is routed through the index file.

1 Like

I’m not sure what you mean.I know when I do Ctrl-R or Ctrl-Shift-R to load/reload the page, the index.php file tries to run twice.

I mean if you see that the debugger goes through the index file twice, then it is probably called twice. It happened to me only when I swent the HTTP request from a webrowser which got the HTML response, and there were other references in the HTML to CSS, JS or images which were not found so an “404 Not Found” page was loaded. If there is a custom error handler for HTTP 404 routed to the index file, then the index file will be called again. Even if you don’t have HTML output, webbrowsers can try to search for favicon.ico to show that on the browser tab next to the title. If there is no favicon.ico, it and the custom error handler jumps in, you will get a second HTTP request to index.php.

There could be other reasons as well, but these are what I had before.