lando / docs

The main docs site for Lando
https://docs.lando.dev/
GNU General Public License v3.0
20 stars 56 forks source link

PHPStorm Unit testing with MySQL error 'getaddrinfo failed: Name or service not known' #7

Closed basepack closed 2 years ago

basepack commented 3 years ago

Getting this error while PHPUnit testing in PHPStorm and use a MySQL test database? Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

Well it is already in the docs here, only did I find it after 8 hours of debugging Laravel/Docker/Lando settings. So I thought it should be fair to document it here too, as this was the place I was looking first to see if someone else had experienced this already.

I cannot count the amount of times I ran lando destroy & lando rebuild -y and docker system prune -a --volumes.

But it was a simple thing after all: your docker appserver container is somehow separated from your database container when running PHPUnit in PHPStorm.

To fix it, see here, or follow the convenient screenshots guide from me below:

For this example I use a Lando project that is called lando-laravel and therefor the network name is called landolaravel_default for me, yours will be different.

Step 1 Run `docker network ls' and get your network name: image

Step 2 Go to PHPStorm Settings > Languages & Frameworks > PHP > Test Frameworks and click the little folder next to Docker container:: image

Step 3 Fill in the networkname you found earlier: image

Great! 😁 image

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions and please check out this if you are wondering why we auto close issues.

DaavisGit commented 3 years ago

You saved my day after hours of debugging the same problem with phpunit + multi docker container setup

reynoldsalec commented 2 years ago

I added a reference to this specific error in the docs, going to close this out but hopefully the combination of this issue and the docs will help others!

firecentaur commented 2 years ago

Thank you so much for this post !! It helped me get my docker working!

ghaskell commented 2 years ago

Thank you so much. This was still a huge help!

paramsave commented 1 year ago

Thank you!!!

ikrs commented 1 year ago

Saved me a lot of time.

Thank you!!

ptelevich commented 1 year ago

Thank you!

ghost commented 1 year ago

I don't see any options for a docker image: jol8zuine22o

This is because by default PHP Storm creates a Local interpreter. Remove this and add a remote interpreter, instead: image

You have to choose the correct Docker container: scrc1gbopw1n

After that you can follow @basepack their instructions and everything works!

franciscorjr commented 1 year ago

thank you very much!!! helped a lot.

franciscorjr commented 1 year ago

I don't see any options for a docker image: jol8zuine22o

This is because by default PHP Storm creates a Local interpreter. Remove this and add a remote interpreter, instead: image

You have to choose the correct Docker container: scrc1gbopw1n

After that you can follow @basepack their instructions and everything works!

try to install and enable Docker and PHP Docker plugin in your PHPStorm IDE.

dgpro commented 1 year ago

I had bridge in Network mode setting which appears in the list but it worked only when I changed it to local_network which is an actual name of the network in docker file.

$ docker network ls
NETWORK ID     NAME                 DRIVER    SCOPE
6cf07ea650a0   bridge               bridge    local
3824a3e9e38c   host                 host      local
1d310e94a7a4   local_network   bridge    local
acfdd57f7273   none                 null      local