Cannot connect to MySQL docker container created via docker-compose

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.

+4
3

Docker, :

: $db = new \PDO('mysql:host=db;dbname=demoName', 'demoUser', 'demoPass');

: $db = new \PDO('mysql:host=db;port=3306;dbname=demoDb', 'demoUser', 'demoPass');

+3

0.0.0.0 MySql, (WorkBench SequelPro), IP- -. :

user$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v1.12.0

MySQL 192.168.99.100 , . 3306

+1

sameersbn/mysql MySQL, EXPOSE 3306/tcp dockerfile.

, 3306 3306 . , docker-compose.yml:

db:
  image: sameersbn/mysql
  volumes:
   - /var/lib/mysql
  environment:
   - DB_NAME=demoDb
   - DB_USER=demoUser
   - DB_PASS=demoPass
  ports:
   - "3306:3306"

MySQL , .

However, as a complete solution to what you are trying to achieve (Nginx, PHP-FPM and MySQL), consider using LaraDock , this open source project is for Laravel, but you can easily change it to work with your PHP code.

-1
source

All Articles