1

I am using docker to start Nginx, PHP and mariaDB in seperate containers with these commands:

# DB
docker run  --name db -d -p 3306:3306 --restart=always -v /opt/db:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=<pass> -e MYSQL_USER=dbuser -e MYSQL_PASSWORD=<pass> mariadb

# PHP
docker run --name php -p :9000 -d --restart=always --link=db:db -v /www:/data php:fpm

# WEB
docker run --name web -d -p 80:80 --link php --link=db:db -v /www:/data -v /opt/nginx/default.conf:/etc/nginx/conf.d/default.conf nginx:latest

(of course has my password filled in) So far so good, have a spinning nginx running locally and I can see the index.htm and phpinfo.php in my www folder presented correctly at http://localhost

Next, I created a dbcheck.php page with the following contents:

<?php
$dbh = mysqli_connect('localhost', 'dbuser', '<pass>');
if (!$dbh) {
    die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully to MariaDB database';
mysqli_close($dbh);
?>

The result is that PHP does not have the MySQL extension installed. What am I doing wrong and/or how can I install the MySQL extention (and where)

0

1 Answer 1

5

Please use db for hostname instead of localhost. So, the code will be like this

$dbh = mysqli_connect('db', 'dbuser', '<pass>');

Because you link the php container with mariadb container using this options --link=db:db. It mean you link to db container and name it as db. So, php container just know db as the hostname for mariadb container

Sign up to request clarification or add additional context in comments.

2 Comments

you're right, thanx. However, is does not solve the problem. When I run the checkdb script, I got this error: Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /data/dbcheck.php:2 Stack trace: #0 {main} thrown in /data/dbcheck.php on line 2. That seems to do with the PHP mysql or mariaDB extention not installed somewhere right?
If the error like that, you need to modify your own Dockerfile, then put command to install mysqli extension there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.