I am trying to run a site using multiple container configuration - one for apache, second for mysql and third for myadmin. Everything starts fine, setup runs smooth but when I try to run a PHP application I get mysqli::__construct(): (HY000/2002): Connection refused in system/libraries/drivers/Database/Mysqli.php [54] error.
It seems that there's something wrong with the connection settings but I checked the site through PHP MyAdmin running on separate container and copied the db host IP from there just to be sure. How can I/should I connect from PHP container to MySQL db?
Here's my docker-compose.yml file:
version: '3'
services:
  web:
    build:
      context: ./etc/php
      args:
         - APP_HOST=${APP_HOST}
         - MYSQL_USER=${MYSQL_USER}
         - MYSQL_PASSWORD=${MYSQL_PASSWORD}
         - MYSQL_PORT=${MYSQL_PORT}
         - MYSQL_DATABASE=${MYSQL_DATABASE}
    ports:
      - ${APP_PORT}:80
      - ${APP_PORT_SSL}:443
    volumes:
      - ./var/bin/:/tmp/bin/
      - ./app/:/var/www/html/
      - ./log/apache2/:/var/log/apache2/
      - ./etc/php/conf/:/usr/local/etc/php/conf.d/
    environment:
      - VIRTUAL_HOST=${VIRTUAL_HOST}
  db:
    build:
      context: ./etc/mysql
      args:
        - MYSQL_DATABASE=${MYSQL_DATABASE}
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
        - DUMP_FILE=${DUMP_FILE}
    volumes:
      - ./etc/mysql/conf:/etc/mysql/conf/conf.d
      - ./data:/var/lib/mysql
      - ./log/:/var/log/
    ports:
      - "${MYSQL_PORT}:3306"
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - ${MYADMIN_PORT}:80
    environment:
      - VIRTUAL_HOST=phpmyadmin.localhost
And the .env with variables:
VIRTUAL_HOST=foo.local
APP_PORT=80
APP_PORT_SSL=443
MYADMIN_PORT=8081
APP_HOST=foo.local
ADMIN_APP_HOST=admin-foo.local
MYSQL_DATABASE=foo_local
MYSQL_USER=root
MYSQL_HOST=172.26.0.2
MYSQL_PASSWORD=root
MYSQL_ROOT_PASSWORD=root
MYSQL_PORT=3306
Oh, and here's the code I try to run:
 $this->link = $socket ? new mysqli(null, $user, $pass, $database, $port, $socket) :
new mysqli($host, $user, $pass, $database, $port);
Output of echo $host.'<br>'.$user.'<br>'.$pass.'<br>'.$database.'<br>'.$port.'<br>'.$socket; is following:
172.26.0.2
root
root
hq_local
3306
                check your container IP with a docker inspect [container name] but write IP in a file is not very good. Replace IP by name of your db container.
You can always use host.docker.internal as IP, therefore you can use something like this:
$db = new \PDO('mysql:host=host.docker.internal;port=3306;dbname=db', 'root', 'pass');
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With