MySQL connection works when accessing application via machine (API endpoints are able to use MySQL), but it is not when accessing it from PHPUnit (locally).
I connected PhpStorm to Docker via TCP socket tcp://localhost:2375
Here's what I get after docker ps
executes : 0.0:8083->3306/tcp mysql
Here's how I set up CLI interpreter:
Here's how I set up PHPUnit:
I double checked my .env
:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=databasename
DB_USERNAME=databaseuser
DB_PASSWORD=123456
Also double checked my docker.compose.yml
:
mysql:
image: mysql
container_name: mysql
working_dir: /application
volumes:
- .:/application
- ./storage-db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=databasename
- MYSQL_USER=databaseuser
- MYSQL_PASSWORD=123456
ports:
- "8083:3306"
In addition I read this article and every StackOverflow question related to this error. And yet I still get it after trying User::all()
within test
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No address associated with hostname
This is how my project structure looks:
dockerProject
/docker/
/nginx/
nginx.conf
/php-fpm/
Dockerfile
/server/ (<--Laravel project)
docker-compose.yml
I am frustrated, what else I can do to make it work?
UPDATE
This is whole docker-compose.yml
file:
version: "3.1"
services:
mysql:
image: mysql
container_name: mysql
working_dir: /application
volumes:
- .:/application
- ./storage-db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=databasename
- MYSQL_USER=databaseuser
- MYSQL_PASSWORD=123456
ports:
- "8083:3306"
webserver:
image: nginx:alpine
container_name: webserver
working_dir: /application
volumes:
- .:/application
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8081:80"
php-fpm:
build: docker/php-fpm
container_name: php-fpm
working_dir: /application
volumes:
- ./server:/application
# - .:/application
- ./docker/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
Here's what worked for me.
docker network ls
Similar to the previous answer, for me using not the docker compose network but the host
network shown by docker network ls
did the trick:
NETWORK ID NAME DRIVER SCOPE
...
f63abee19ca2 host host local
File > Settings > PHP > Test Frameworks > Your remote interpreter > In Docker container line click the directory symbol
In network mode add host
.
--net="host"
should appear under Docker container
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With