Cannot connect with PHP scripts from php:7.2-apache container to mariadb:10.5 container

Hello everybody! :smile:

I’m newly new to the wonderful world of Docker.

I’ve been trying to put up the following development environment on Windows Docker Desktop:

  • php:7.2-apache container
  • mariadb:10.5 container

I’ve customized the first container in order to have SSL on port 443. Successfully, it seems :sweat_smile:

Now, when I try to connect to MariaDB with a PHP script, I get the following warnings:

Warning: Packets out of order. Expected 0 received 1. Packet size=72 in /var/www/html/index.php on line 3

Warning: mysqli::__construct(): MySQL server has gone away in /var/www/html/index.php on line 3

Warning : mysqli::__construct(): Error while reading greeting packet. PID=20 in /var/www/html/index.php on line 3

Warning : mysqli::__construct(): (HY000/2006): MySQL server has gone away in /var/www/html/index.php on line 3`

and nothing, of course, works.

This is my directory tree and configuration files:
Directory tree

app/
    index.php
database/
docker/
    apache-php/
        000-default.conf
        Dockerfile
        httpd-ssl.conf
        httpd-vhosts.conf
        server.crt
        server.key
    mariadb/
    .env
    docker-compose.yml
logs

app/index.php

<?PHP

$db = new mysqli('mariadb', 'root', 'root', 'test', 3306);

.env

COMPOSE_PROJECT_NAME=test_amp

docker-compose.yml

version: '3'

services:
  apachephp:
    container_name: test_amp_apachephp_con
    image: test_amp_apachephp_img
    build:
      context: ./apache-php
    volumes:
      - ../app:/var/www/html
      - ../logs/apache:/usr/local/apache2/logs
    ports:
      - "80:80"
      - "443:443"
    networks:
      - default
      - mariadb
  mariadb:
    container_name: test_amp_mariadb_con
    image: test_amp_mariadb_img
    build:
      context: ./mariadb
      args:
        - MYSQL_CONTAINER_USER=mysql
        - MYSQL_CONTAINER_GROUP=mysql
        - MYSQL_ROOT_PASSWORD=root
    volumes:
      - ../database:/var/lib/mysql
      - ../logs/mariadb:/usr/local/mysql/logs
    environment:
      - MYSQL_CONTAINER_USER=mysql
      - MYSQL_CONTAINER_GROUP=mysql
      - MYSQL_ROOT_PASSWORD=root
    networks:
      - mariadb

networks:
  default:
  mariadb:
    driver: bridge
    internal: true

apache-php/000-default.conf

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
LoadModule socache_shmcb_module /usr/lib/apache2/modules/mod_socache_shmcb.so
LoadModule ssl_module /usr/lib/apache2/modules/mod_ssl.so

Include /etc/apache2/conf/extra/httpd-ssl.conf
Include /etc/apache2/conf/extra/httpd-vhosts.conf

apache-php/Dockerfile

FROM php:7.2-apache

COPY 000-default.conf /etc/apache2/sites-enabled/000-default.conf
COPY server.crt /etc/apache2/conf/ssl/server.crt
COPY server.key /etc/apache2/conf/ssl/server.key
COPY httpd-ssl.conf /etc/apache2/conf/extra/httpd-ssl.conf
COPY httpd-vhosts.conf /etc/apache2/conf/extra/httpd-vhosts.conf

RUN docker-php-ext-install mysqli

apache-php/httpd-ssl.conf

Listen 443

SSLCipherSuite HIGH:MEDIUM:!MD5:!RC4:!3DES
SSLProxyCipherSuite HIGH:MEDIUM:!MD5:!RC4:!3DES

SSLHonorCipherOrder on 

SSLProtocol all -SSLv3
SSLProxyProtocol all -SSLv3

SSLPassPhraseDialog  builtin

SSLSessionCache "shmcb: /usr/local/apache2/logs/ssl_scache(512000)"
SSLSessionCacheTimeout  300

<VirtualHost *:443>
    DocumentRoot /var/www/html

    ServerName dev.test.com:443
    ServerAdmin admin@dev.test.com
    ErrorLog "/usr/local/apache2/logs/error.log"
    TransferLog "/usr/local/apache2/logs/access.log"

    SSLEngine on

    SSLCertificateFile "/etc/apache2/conf/ssl/server.crt"
    SSLCertificateKeyFile "/etc/apache2/conf/ssl/server.key"

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory "/etc/apache2/cgi-bin">
        SSLOptions +StdEnvVars
    </Directory>

    BrowserMatch "MSIE [2-5]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0

    CustomLog "/usr/local/apache2/logs/ssl_request.log" "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

</VirtualHost>

apache-php/httpd-vhosts.conf

<VirtualHost *:80> 
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /usr/local/apache2/logs/error.log
    CustomLog /usr/local/apache2/logs/access.log common
</VirtualHost>

mariadb/custom.cnf

[mariadb]
log_error=/usr/local/mysql/logs/error.log

mariadb/.000-default.conf

FROM mariadb:10.5

ARG MYSQL_CONTAINER_USER
ARG MYSQL_CONTAINER_GROUP

COPY custom.cnf /etc/mysql/conf.d/custom.cnf

I assure you: I’ve tried and googled for 2 long days to set up this environment before posting here, but I can’t understand what’s going on.

Is there any good soul willing to help me? :pray:

Thank you sooo much!
// please excuse me if I've forgot some information ^^