I want to start working with docker and create a simple container environment with a nginx container, a PHP-FPM container and a MySQL container.
As long as the connection between the nginx container and PHP-FPM is working fine, I cannot connect the PHP application server to the database server.
I use docker-compose to minimize manual terminal operation. My docker-compose.yml looks like this:
web:
image: tutorial/nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./src/vhost.conf:/etc/nginx/sites-enabled/vhost.conf
links:
- php
php:
image: nmcteam/php56
volumes:
- ./src/php-fpm.conf:/etc/php5/fpm/php-fpm.conf
- ./src:/var/www
links:
- db
db:
image: sameersbn/mysql
volumes:
- /var/lib/mysql
environment:
- DB_NAME=demoDb
- DB_USER=demoUser
- DB_PASS=demoPass
While I'm trying to connect to the database with the following statement:
$db = new \PDO('mysql:host=db;dbname=demoName', 'demoUser', 'demoPass');
The MySQL container itself works, since I can connect to bash containers and use the MySQL CLI:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| demoDb |
| mysql |
| performance_schema |
+--------------------+
I just get 500 errors and cannot find the reason why this will not work. Any help or suggestion that I might have missed is more than appreciated.