Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ugurdnlr's avatar

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

I'm getting this error while using laradock.

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

when i run tests from phpstorm I'm getting this. But i didn't get this when i run from terminal like phpunit Tests\ExampleTest

My project .env

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=default
DB_USERNAME=default
DB_PASSWORD=secret

laradock .env

MYSQL_VERSION=5.7
MYSQL_DATABASE=default
MYSQL_USER=default
MYSQL_PASSWORD=secret
MYSQL_PORT=3306

docker-compose.yml

mysql:
      build:
        context: ./mysql
        args:
          - MYSQL_VERSION=${MYSQL_VERSION}
      environment:
        - MYSQL_DATABASE=${MYSQL_DATABASE}
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
        - TZ=${WORKSPACE_TIMEZONE}
      volumes:
        - ${DATA_PATH_HOST}/mysql:/var/lib/mysql
        - ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
      ports:
        - "${MYSQL_PORT}:3306"
      networks:
        - backend
0 likes
8 replies
bugsysha's avatar

Example test is probably not using database.

Check your phpunit.xml or .env.testing file.

You probably do not have testing database defined.

ugurdnlr's avatar

when i exit from docker i can run tests in the testing database from phpstorm in local machine. Tests\ExampleTest this was an example not the test I run.

bugsysha's avatar

Oh, then that is the issue. Docker can not see the database which is not defined within it. Docker is an isolated environment. You need to create a database inside docker for it to use it during testing.

ugurdnlr's avatar

I did what did u say. I created another database in docker for testing but again faced same error. I think it might be related to mysql.

bugsysha's avatar

I'm not using LaraDock, but I basically have same setup and it works just fine. You are probably missing something. Seems like you do not quite understand accessibility within virtual networking. Maybe try to explain better here what is your exact setup so suggestions can be made and hopefully the issue can be resolved.

ugurdnlr's avatar

Thanks,

I found the solution . It was a problem caused by PhpStorm.

My Solution

PhpStorm->Preferences->Languages & Frameworks->PHP->Test Frameworks

Add PHPUnit by Remote Interpreter

Select Docker interpreter

After created you should add link ( Links section ) which one do u want to use ( mysql, redis) to docker container (click Browse in docker container section)

And if you know which network is using in docker container you should write name of network ( docker network ls ) into Network Mode section then click OK . That’s it.

But if you don’t know which network is using . First you should remove all unused networks ( docker network prune ) then run ( docker network ls )

3 likes
danilocolasso's avatar

Thanks guy, you helped me a lot! I was trying to solve it for days!

cmackinlay's avatar

Hi @ugurdnlr , what does this look like in latest PHPstorm for you? There is no option to add links in mine (2021.1)

Please or to participate in this conversation.