title: Docker PHP MySQL connection refused
body: <p>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 <code>mysqli::__construct(): (HY000/2002): Connection refused in system/libraries/drivers/Database/Mysqli.php [54]</code> error.</p>
  
  <p>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?</p>
  
  <p>Here's my <code>docker-compose.yml</code> file:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And the <code>.env</code> with variables:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Oh, and here's the code I try to run:</p>
  
  <pre><code> $this-&gt;link = $socket ? new mysqli(null, $user, $pass, $database, $port, $socket) :
  new mysqli($host, $user, $pass, $database, $port);
  </code></pre>
  
  <p>Output of <code>echo $host.'&lt;br&gt;'.$user.'&lt;br&gt;'.$pass.'&lt;br&gt;'.$database.'&lt;br&gt;'.$port.'&lt;br&gt;'.$socket;</code> is following:</p>
  
  <pre><code>172.26.0.2
  root
  root
  hq_local
  3306
  </code></pre>