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.
index.php

<?php

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

$pdo = new PDO($dsn, 'product_db_user', 'secret', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

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

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

print_r($prodcuts);

?>

docker-compose.yml

version: '3.1'

services:
  php:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      [./:/var/www/html,
      ./docker/php/conf.d/php.ini:/usr/local/etc/php/php.ini,
      ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini,
      ./docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
      ]
    ports:
      ["8081:80"]
    stdin_open: true
    tty: true
  db:
    image: mariadb:10.6
    restart: always
    volumes:
      ["./mariadb/data:/var/lib/mysql"]
    ports:
      ["3306:3306"]
    environment:
      DATASOURCES_DEFAULT_HOST: db
      MYSQL_ROOT_PASSWORD: notSecureChangeMe
      MYSQL_USER: product_db_user
      MYSQL_PASSWORD: secret
  phpmyadmin:
    image: phpmyadmin
    restart: always
    ports:
      - 8082:80
    environment:
      - PMA_ARBITRARY=1

php.ini & xdebug.ini

zend_extension=xdebug.so

xdebug.mode=develop,coverage,debug,profile

xdebug.idekey=docker

xdebug.start_with_request=yes

xdebug.log=/dev/stdout

xdebug.log_level=0

xdebug.client_port=9003

xdebug.client_host=192.168.1.14

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:

extension=pdo.so
extension=pdo_mysql.so

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.