Home > front end >  Docker-Compose Cannot Connect to Server w/ mysqli_connect
Docker-Compose Cannot Connect to Server w/ mysqli_connect

Time:04-06

I have recently installed docker-compose on Ubuntu. I can connect to my local host, phpmyadmin and stuff. However, when I try to connect to it with mysqli_connect, I can reach the server but I cannot access it with Warning: mysqli_connect(): (HY000/2002).

Here is the php code I am using to try to connect: // username and password belong to the phpmyadmin, before that I tried docker for both username and pwd.

Edit: staj is the name of database i created in phpmyadmin.

<?php


$dbHost="127.0.0.1";
$dbUser="root";
$dbPass="root";
$dbName="staj";

$conn=mysqli_connect($dbHost, $dbUser, $dbPass, $dbName);

if ($conn){

}else{
die("Connection Failed!");
}

?>

And here is my docker-compose.yml:

version: "3"

services:
  webserver:
    build: 
      context: ./bin/${PHPVERSION}
    container_name: '${COMPOSE_PROJECT_NAME}-${PHPVERSION}'
    restart: 'always'
    ports:
      - "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
      - "${HOST_MACHINE_SECURE_HOST_PORT}:443"
    links: 
      - database
    volumes: 
      - ${DOCUMENT_ROOT-./www}:/var/www/html
      - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
      - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
      - ${LOG_DIR-./logs/apache2}:/var/log/apache2
    environment:
      APACHE_DOCUMENT_ROOT: ${APACHE_DOCUMENT_ROOT-/var/www/html}
      PMA_PORT: ${HOST_MACHINE_PMA_PORT}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
  database:
    build:
      context: "./bin/${DATABASE}"
    container_name: '${COMPOSE_PROJECT_NAME}-${DATABASE}'
    restart: 'always'
    ports:
      - "127.0.0.1:${HOST_MACHINE_MYSQL_PORT}:3306"
    volumes: 
      - ${MYSQL_INITDB_DIR-./config/initdb}:/docker-entrypoint-initdb.d
      - ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
      - ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: '${COMPOSE_PROJECT_NAME}-phpmyadmin'
    links:
      - database
    environment:
      PMA_HOST: database
      PMA_PORT: 3306
      PMA_USER: root
      PMA_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      UPLOAD_LIMIT: ${UPLOAD_LIMIT}
      MEMORY_LIMIT: ${MEMORY_LIMIT}
    ports:
      - '${HOST_MACHINE_PMA_PORT}:80'
    volumes: 
      - /sessions
      - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/conf.d/php-phpmyadmin.ini
  redis:
    container_name: '${COMPOSE_PROJECT_NAME}-redis'
    image: redis:latest
    ports:
      - "127.0.0.1:${HOST_MACHINE_REDIS_PORT}:6379"
  

CodePudding user response:

Solution:

   $dbHost="database";
   $dbUser="docker";
   $dbPass="docker";
   $dbName="docker";

It turns out that docker for username and pwd was correct but the host name was incorrect.

I got that "docker" from the .env.

CodePudding user response:

Seems you've already found the way to connect, but here's why it works that way:

You're using distinct docker containers for each service, which is correct, but that means your application isn't trying to connect locally (127.0.0.1) because the database isn't running on the same container.

Docker Compose networking allows hosts defined in the same docker-compose file to communicate with each other by using the host names which are defined as the service name (the next level under service), so in your case you have hosts webserver, database, phpmyadmin, and redis.

Simply updating the host name you're trying to connect to from 127.0.0.1 to database should be all you need to do.

  • Related