skip to Main Content

So, I’m trying to connect to my database container from my webserver container. I currently use Codeignter 4 for my PHP framework. Everything goes well in terms of communicating between different containers because that container is inside the same networks. Inside webserver container, I’ve tried to ping from and to database container with no problem, All the port is accessible because I can connect PHPMyAdmin which in their own snuggle little container and connect to my DB container with no problem.

This is the backtrace

SYSTEMPATH/Database/BaseConnection.php : 618   —  CodeIgniterDatabaseBaseConnection->initialize ()

I tried to DD from my controller hoping to override any return view

$db = ConfigDatabase::connect();
$apakek = $db->query("SELECT * FROM student_details_dummy");
dd($apakek);

This is my DB Array inside AppDatabase.php

public $default = [
    'DSN'      => '',
    'hostname' => '172.21.0.4',
    'username' => 'kr_rw',
    'password' => 'MrSLwwZvwC1KCRm6',
    'database' => 'kr_main',
    'DBDriver' => 'MySQLi',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'development'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => 21236,
];

I’ve tried using hostname db_mysql Which is pingable with the corresponding port to even check open port on canyouseeme.org returning fine.

How should I resolve this?

PS: all necessary extension is installed and enabled including php_mysqli php_mbstring php_pdo should any miss please do point out.

2

Answers


  1. Chosen as BEST ANSWER

    So, I'm myself not sure why this is the case. The network I created for those 3 containers acted like local network for a bunch of computer. So its basically boil downs to this:

    1. Outside the Docker Network: Use the Port Forwarding.
    2. Inside the Docker Network : Use the default port of the container.

    So in the end I just use 3306 or the default port for accessing mysql.


  2. If it’s on docker you have to open the port outside and the host should be your container name

    ports:
      - "33088:3306"
    

    and

    'hostname' => 'container_name',
    

    If you’re not using .env to pass config params to docker, then specify it on container level. (example below)

    environment:
      DB_DATABASE: db_mysql
      DB_USERNAME: .........
    

    Example code of my project

    In docker-compose.yml

    services:
      ## -----------------------------------------------
      ##           MySql database
      ## -----------------------------------------------
      db_mysql:
        image: mysql:5.7
        restart: always
        volumes:
          - db_mysql:/var/lib/mysql
          - ./mysql:/docker-entrypoint-initdb.d
        command: --default-authentication-plugin=mysql_native_password
        environment:
          MYSQL_ROOT_PASSWORD: root
        deploy:
          mode: global
        ports:
          - "33088:3306"
    

    In Database file (I used Laravel. But in Codeigniter, it’s the same)

    public $default = [
        'DSN'      => '',
        'hostname' => 'db_mysql',
        'username' => 'root',
        'password' => '',
        'database' => 'kr_main',
        'port'     => 33088,
    ];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search