'mysqli_connect():(HY000/2002): No such file. Error with cronjob script

I'm using docker. I have 4 containers, in one of these I created the cronjob file and in the compose file i used "link: mysql", which is another container where my db is running. This is my cron-job.

* * * * * cd /var/www/html; /usr/local/bin/php mailscheduler.php >> /var/log/cron.log 
# An empty line is required at the end of this file for a valid cron file.

In cron.log the output is always: error

In EmailModel.php I added these constant:

define('DB_SERVER', getenv()["DB_HOST"]);
define('DB_USERNAME', getenv()["DB_USER"]);
define('DB_PASSWORD', getenv()["DB_PASS"]);
define('DB_DATABASE', getenv()["DB_NAME"]);

From CLI of cron-job container it gives me no error. I don't understand what i have to do.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source