10

I want to get started with docker and created a simple container environment with an nginx container, a PHP-FPM container and a MySQL container.

While the link between the nginx and PHP-FPM container works well I can't seem to link the PHP application server with the database server.

I use docker-compose to minimize the manual terminal work. 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 try to connect to the DB with the following statement:

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

The MySQL container itself is working as I can connect to the containers bash and use the MySQL CLI:

mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | demoDb | | mysql | | performance_schema | +--------------------+ 

I just get a 500 error and can't find a reason why this wouldn't work. Any help or suggestion of what I might have missed is more than appreciated.

10
  • Your connection string should be $db = new \PDO('mysql:host=localhost;dbname=demoName', 'demoUser', 'demoPass'); if connecting locally otherwise you need to expose the port and use the ip of the host machine. Linking only exposes the linked container internally to the application it is linked to. You can't reference it by db, you must reference it by localhost or 127.0.0.1 Commented May 26, 2016 at 18:10
  • @GHETTO.CHiLD thanks for the effort. I commented on the answer you provided. Commented May 27, 2016 at 7:58
  • are you connecting from the nginx box? Commented May 27, 2016 at 12:00
  • @GHETTO.CHiLD I'm not sure if I get your question... As the PHP application server interprets all my PHP code it should connect through this container. Please correct me if I'm mistaken here. Commented May 27, 2016 at 12:36
  • 1
    Try this: $db = new \PDO('mysql:host=db;port=3306;dbname=demoDb', 'demoUser', 'demoPass'); You have DB_NAME=demoDb but are setting 'mysql:host=db;dbname=demoName'. Change that, reconnect and see if it works. I don't think this is a Docker issue but a code issue. Commented May 31, 2016 at 11:40

4 Answers 4

5

This is not a Docker issue but a code issue:

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

If you have 0.0.0.0 host for your container with MySql and you want to connect with the database from outside docker (WorkBench or SequelPro), just use your docker-machine IP. Example:

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

Then your host to MySQL is 192.168.99.100 with proper port e.g. 3306

Comments

1

Be sure to expose the database port to the host network, by setting up your "db" service ports:

 ports: - 127.0.0.1:3306:3306 

Now your mysql 3306 port will be visible from outside. Not having this setting, would make it available only to other services on the same network.

Note: 127.0.0.1 is optional, it specifies that port 3306 is accessible only from localhost, and not from another ip, eg. someone connecting from outside

Comments

-2

You are using sameersbn/mysql as base image for MySQL which exposes EXPOSE 3306/tcp according to this dockerfile.

I think you need expose port 3306 to 3306 in the container. you can do this by adding the following to your docker-compose.yml file:

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

I personally would use the original MySQL image instead of the one you are using.

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

2 Comments

- "3306:3306" <-- what you're doing in here is opening mysql port to the whole world..
This actually makes you MySQL server open to the internet. Bad for security.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.