Docker + PHPUnit results in SQLSTATE[HY000] [2002] Connection refused in .... errors

I'm trying to set up PHPStorm + Docker + PHPUnit to easily run my tests from PHPStorm.

Problem is, there seems to be some connection issue I cant figure out because when I run the tests from PHPStorm, I get this error:

Tests run just fine when running the directly from the container.

Any tips? MariaDB settings and user settings should be ok, I've checked them many times.



Is MySQL running in a different container? Is this (MySQL) container running standalone, or as a part of a Docker Compose orchestra?
There's an important moment here - PhpStorm does not use the existing container, it starts a new one with literally no configuration.
So it doesn't inherit any settings (network, mappings, anything else) from the container that's already running, and that's probably the cause why it cannot connect.
I strongly advise that you use Docker Compose with `depends_on` to run tests utilizing more then one container.

I've investigated a similar issue recently, and created a dummy project that demonstrates how it's working in PhpStorm:


MySQL, Apache, etc. are running on the same container.

I assumed PHPStorm attached to the running container rather than starting a new one. That's where the problem is. Not sure what, if anything, I can do to fix it.


PhpStorm starts containers every time, not reusing existing ones. 


Let's say you put your running container down.
What would be the command (docker run blah-blah), and what will you run inside container to bring it to the initial state?
Combine those and you'll get the configuration missing in PhpStorm.
The command is configurable in Docker PHP Interpreter settings, and in-container configuration can only be applied with Dockerfile, so you'll have to actually re-build the image.


I run "docker run -p 80:80 -p 443:443 --name "whatever" -t -i -w /var/www/html -v C:/Users/user/Documents/Docker/www:/var/www/html -v database:/var/lib/mysql kpserver"

MySQL config file being copied in the Dockerfile.


Please sign in to leave a comment.