0

I can't display my index.php from my container due to this error:

Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/db/dbh.inc.php:8 Stack trace: #0 /var/www/includes/header.php(6): require() #1 /var/www/index.php(2): require_once('/var/www/includ...') #2 {main} thrown in /var/www/db/dbh.inc.php on line 8

The line 8 is: $conn = mysqli_connect($servername, $db_username, $db_password, $db_name);

I know that mysql_connect is deprecated in PHP 7.0, and I use here mysqli_connect

My Dockerfile from php-fpm run mysqli so I don't know what's wrong here

If my index.php show only a Hello World ererything is working well with Docker. Just to let you know my website is working well from my localhost without Docker.

If I want to connect to my db it's not working well with Docker.

In order to start the containers, I do docker-compose up -d

I have two questions:

1. How can I fix this issue ?

2. How can I start the db container always first before all others containers with docker-compose ?

Here my tree files:

.
├── access
│ --- commented ---
├── assets
| --- commented ---
├── changepass.php
├── contact.php
├── db
│   ├── dbh.inc.php
│   ├── Dockerfile
│   └── mysqldump.all.tables.sql
├── docker
│   ├── docker-compose.yml
│   ├── nginx
│   │   ├── conf.d
│   │   │   └── default.conf
│   │   ├── Dockerfile
│   │   ├── nginx.conf
│   │   └── sites
│   │       └── default.conf
│   └── php-fpm
│       └── Dockerfile
├── editerrors.php
├── includes
│   ├── auth_check.php
│   ├── footer.php
│   ├── functions.inc.php
│   └── header.php
├── index.php
├── login.php
├── log.php
├── profile.php
├── signup.php
├── stb.php
└── userslist.php

/docker/nginx/Dockerfile

# nginx
FROM nginx:alpine

CMD ["nginx"]

EXPOSE 80 443

/docker/php-fpm/Dockerfile

# php-fpm

FROM php:fpm-alpine

RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

CMD ["php-fpm"]

EXPOSE 9000

WORKDIR /var/www/

/db/Dockerfile

# Database MariaDB
FROM mariadb:latest

CMD ["mysqld"]

EXPOSE 3306

/docker/docker-compose.yml

version: '3'

services:
  php-fpm:
    container_name: php
    build:
      context: ./php-fpm
    volumes:
      - ../:/var/www

  nginx:
    container_name: nginx
    build:
      context: ./nginx
    volumes:
      - ../:/var/www
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/sites/:/etc/nginx/sites-available
      - ./nginx/conf.d/:/etc/nginx/conf.d
    depends_on:
      - php-fpm
    ports:
      - "9001:80"
      - "443:443"

  database:
    container_name: db
    build:
      context: .
      dockerfile: ../db/Dockerfile
    environment:
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=myuser
      - MYSQL_PASSWORD=mypassword
      - MYSQL_ROOT_PASSWORD=docker
    volumes:
      - ../db/mysqldump.all.tables.sql:/docker-entrypoint-initdb.d/mysqldump.all.tables.sql
Seth
  • 364
  • 1
  • 16
  • Seems like you forgot to load `php_mysqli.so`. Does this answer your question? [How do I enable mysqli for my PHP script?](https://stackoverflow.com/questions/54500881/how-do-i-enable-mysqli-for-my-php-script) – Ro Achterberg Nov 08 '20 at 17:09
  • you may right but how and where I should do this ? Within Dockerfile from php-fpm ? With a run command ? – Seth Nov 08 '20 at 17:30
  • Locate `php.ini` and uncomment this line: `extension=php_mysqli.so`. If I'm right, it is currently prefixed by a hash (`#`). Don't forget to restart PHP. – Ro Achterberg Nov 08 '20 at 17:34
  • Any warnings or errors at build time? – Zoli Szabó Nov 08 '20 at 17:41
  • Did you add the mysqli extension install commands at a later time to the Dockerfile? Have you rebuilt it (`docker-compose up --build`) afterwards? – Zoli Szabó Nov 08 '20 at 18:11
  • After install manually `RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli` and restart the php container I have another error message `Warning: mysqli_connect(): (HY000/2002)`. I don't know why the RUN command was not taken into account first. – Seth Nov 08 '20 at 18:57
  • Have you consulted [this answer](https://stackoverflow.com/questions/13769504/mysqlimysqli-hy000-2002-cant-connect-to-local-mysql-server-through-sock)? – Ro Achterberg Nov 08 '20 at 19:06
  • here warning/errors from build ` warning: mysqli (mysqli) is already loaded! WARNING: Ignoring APKINDEX.2c4ac24e.tar.gz: No such file or directory WARNING: Ignoring APKINDEX.40a3604f.tar.gz: No such file or directory ` – Seth Nov 08 '20 at 19:36
  • I consulted the answer and changed localhost to 127.0.0.1, now I have another error message `Connection failed: Connection refused` still with `Warning: mysqli_connect(): (HY000/2002):` Just to let you know, manually from the container with `docker exec -ti db sh` I'm able to connect to mariadb – Seth Nov 08 '20 at 19:42
  • ok fix it. From my dbh.inc.php I had to changed `$servername = "127.0.0.1";` by the alias db from docker-compose.yml `$servername = "db";` Now it's working. – Seth Nov 08 '20 at 19:53

0 Answers0