Doesn't work when connecting Apache with PHP with MySQL

Hello folks,
Snímek obrazovky 2021-03-07 v 13.31.05

I pulled latest HTTPD, PHP, MYSQL. Now I want to make everything working together.

I created apache with php using this

docker run -d -p 8080:80 --mount type=bind,source="my_local_folder",target=/var/www/html php:apache

This works perfectly. Now I created a MySQL DB. Using Docker Adminer, I can connect to that DB. That is OK.

But when I’m trying to connect via PHP function mysqli

$mysqli = mysqli_connect($server, $user, $pass, $db);

Server is the container name of MySQL Docker, user is root, pass is my pass and DB is the name for my DB. Where I’m making the mistake?

**Warning** : mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in **/var/www/html/temp_log.php** on line **8**

**Warning** : mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in **/var/www/html/temp_log.php** on line **8**
Error: Unable to connect to MySQL. Debugging errno: 2002 Debugging error: php_network_getaddresses: getaddrinfo failed: Name or service not known

Thanks a lot!

You need to create a docker network and join your container to that network so they can communicate with each other. In your current setup the containers don’t know how to connect to each other.

P.S. You might want to take a look at docker-compose to simplify / automate your infrastructure setup.