Docker Community Forums

Share and learn in the Docker community.

PHP-Error in dockerized php dev environment

Hi!

I am running a PHP dev env in different docker containers. Unfortunately I do not succeed in connecting from the PHP code to my database container by using PDO.
In a different container I am running PHPMyAdmin which can connect to the database.

The same setup works perfectly on the same docker environment but for another project with a different docker-compose file and different names.

I was able to reproduce the error on a brand new virtual machine successfully :frowning:

The code for that environment is available on GitHub.
And I already discussed the error message on Stackoverflow - unfortunately not successfull.

Anyone here able to help me with that issue?

Regards,
Olaf

A) Running PHP 7.2
Run docker compose and build docker-compose up --build -d (only needed 1st time) Next time you can just run docker-compose up -d

B) Running PHP 5.6
Run docker compose and build docker-compose -f docker-compose-php5.6.yml up --build -d (only needed 1st time) Next time you can just run docker-compose -f docker-compose-php5.6.yml up -d

C) Running HHVM v3.27
Run docker compose and build docker-compose -f docker-compose-hhvm3.27.yml up --build -d (only needed 1st time) Next time you can just run docker-compose -f docker-compose-hhvm3.27.yml up -d

Docker compose will run 2 containers: php_web container and db (mysql) container.

Accessing
Start writing your php app in /web_volume folder
Apache PHP (and your web app) are accessible on http://localhost:10080
Mysql / DB:
hostname db port 3306 from docker container (and php webapp).
hostname localhost port 13306 from outside.
Sample PHP files are included, you can access using web browser:
http://localhost:10080/info.php - php info
http://localhost:10080/adminer.php - database management
Stopping
To stop run docker-compose stop
To stop and remove container docker-compose down
Advanced Access
If you need bash/terminal access to the container (just like ssh), you can run:
Php container docker exec -it php_web bash
Mysql container docker exec -it db bash
The default user for mysql have limited access, to grant all privilege :
Login mysql as root docker-compose exec db sh -c ‘export MYSQL_PWD="$MYSQL_ROOT_PASSWORD"; mysql’.
Grant all DB access GRANT ALL PRIVILEGES ON . TO ‘user1’;

Thanks for your answer!
The issue is not on how to get a stack running.

In my environment everything works fine. But when I try to fire up another environment from the same docker compose file but with other namings of the services I am not able to connect from my PHP code to the database. My dedicated PHPMyAdmin container can connect but my own code, based on PDO, cannot connect.

I also filed that issue at stackoverflow: https://stackoverflow.com/questions/63111681/sqlstatehy000-2002-no-such-file-or-directory-when-connecting-via-pdo-from-ph?noredirect=1#comment111603123_63111681

And you can find the files at https://github.com/okoestner/dockerissue

Any additional idea on how to get that working?