Docker/Devilbox Database Connection Problem

I have faced this problem when I was using devilbox with Symfony. The problem was connection to database while running the app, especially the the unit tests. Before I give you the solution, let us see if our problem was the same.

When app was running inside the docker container, and not on the host OS, I was getting following error when I was trying to connect to MySQL database via 127.0.0.1

An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused

On the other hand, when I used the IP as localhost, I was getting somewhat different error:

SQLSTATE[HY000] [2002] No such file or directory

The problem was docker network was not shared. To give access to its network, it was needed to be shared with its network name. Devilbox uses “devilbox_app_net” as network name. If you are running PHPUnit tests in PHPStorm, you can provide this in File >> Settings >> PHP >> Docker Container >> Network Mode

Insert “devilbox_app_net” as value of Network Mode.

After this you have to use the IP of MySQL being assigned by devilbox. You can see that in your devilbox/docker-compose.yaml

In my case it is 172.16.238.12

With that your database settings will become as following:

DATABASE_URL=mysql://dbUser:dbPassword@172.16.238.12:3306/dbName

Good luck!

Leave a Reply

avatar
  Subscribe  
Notify of