Jeg har endelig løst problemet, tak @Dlk for din hjælp.
Årsagen til problemet var fordi i database.php
, jeg henviste til værten for mysql som localhost
i stedet for navnet på MYSQL-tjenesten i docker-compose.yml
fil. Derfor database.php
filen skal se sådan ud :
<?php
$host = 'nairobi_mysql'; // Must be the service name of the database in `docker-compose.yml`
$db_name = 'nairobi';
$user = 'admin';
$pass = '123456';
$charset = 'utf8mb4'; // Always set charset for database
$port = '3306';
$dsn = "mysql:host=$host;dbname=$db_name;port=$port;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>
som korrelerer med min docker-compose.yml
fil
version: '3.7'
services:
php:
container_name: nairobi_php
build:
context: ./
volumes:
- './src:/var/www/html'
depends_on:
- mysql
ports:
- 80:80
mysql:
container_name: nairobi_mysql
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: CUeHpADRmZCtnTFGctxp
MYSQL_DATABASE: nairobi
MYSQL_USER: admin
MYSQL_PASSWORD: 123456
restart: always
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306:3306
adminer:
image: adminer
restart: always
ports:
- 8080:8080