1

I'm learning Docker and am still pretty new to it. I'm trying to get PHP 7 FPM, Nginx and MariaDB to work together ( I'm using this version of PHP 7 FPM because it come with MySQLi already installed) but I receive the following error:

Warning: mysqli_connect(): (HY000/2002): No such file or directory in /usr/share/nginx/html/index.php on line 4
Error: Unable to connect to MySQL. Debugging errno: 2002 Debugging error: No such file or directory

My index.php looks like this:

<?php
  header('Content-Type: application/json');

  $link = mysqli_connect("localhost", "admin", "admin", "admin");

  if (!$link) {
      echo "Error: Unable to connect to MySQL." . PHP_EOL;
      echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
      echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
      exit;
  }

  echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
  echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

  mysqli_close($link);

and my Dockerfile looks like this:

version: '2'
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx/site.conf:/etc/nginx/conf.d/default.conf
      - ./logs/nginx-error.log:/var/log/nginx/error.log
      - ./logs/nginx-access.log:/var/log/nginx/access.log
      - ./public:/usr/share/nginx/html
    links:
      - php:php

  php:
    image: danieldent/php-7-fpm
    volumes:
      - ./public:/usr/share/nginx/html
      - ./logs/log.conf:/usr/local/etc/php-fpm.d/zz-log.conf
      - ./PHP/php.ini:/etc/php/7.0/fpm/php.ini
      - ./PHP/php.ini:/usr/local/etc/php/conf.d/php.ini
    links:
      - mariadb:mysqlip

  mariadb:
    image: mariadb:latest
    environment:
      MYSQL_ROOT_PASSWORD: pw
    stdin_open: true
    tty: true
    ports:
      - 3306:3306/tcp
    labels:
      io.rancher.container.pull_image: always
    volumes:
      - ./database:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - mariadb:db

The php_mysqli.dll extension is also enabeled in my php.ini file.

Again I'm still pretty new to docker but really want to get this working, thanks for any suggestions.

James Johnson
  • 311
  • 6
  • 16

2 Answers2

1

Have you tried using 127.0.0.1 instead of localhost? I believe using localhost forces the client to try to connect using the local Unix socket instead of over TCP as discussed here and it's not finding the mysql.sock file which doesn't exist on your php container since MySQL is installed on a different container, hence the "no such file or directory" error.

As a word of advice, I would suggest using networking/linking functions of Docker to accomplish this the Docker way so you can take advantage of some its DNS functionality and make your setup slightly more portable and secure. Currently, you are publishing your MySQL database port to the host machine, and therefore, the outside world, and making your php container rely on this fact so that it can connect through localhost. In reality it only needs to be exposed to your PHP service within the network the containers already share internally by default. Change your compose file to expose those ports instead of publishing them (subtle, I know) like so:

mariadb:
  ...
  expose:
    - "3306"
  ...

Since you're already referring to your mariadb service as mysqlip inside your php service, Docker's built-in container DNS resolution allows you to refer to your container by it's linked alias so your php code can now connect to MySQL using "mysqlip" instead of "localhost" or "127.0.0.1" and Docker will take care of finding the correct container within it's network.

More information on the conceptual difference behind expose vs ports can be found here

Community
  • 1
  • 1
shiv
  • 206
  • 1
  • 7
0

$link = mysqli_connect("localhost", "admin", "admin", "admin");

change the host variable 'localhost' to 'mariadb' - exactly as your container mariadb name