-1

I created a php based container and created mysql db as well but when accessing the container from browser give me the following status.

Fatal error: Uncaught Error: Class 'mysqli' not found in /var/www/html/index.php:35 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 35

docker images

REPOSITORY   TAG          IMAGE ID       CREATED         SIZE
myimage      latest       3ec83753ba80   3 minutes ago   368MB
mysql        latest       43fcfca0776d   10 days ago     449MB
phpmyadmin   latest       f56a026be8a5   11 days ago     511MB
php          7.0-apache   aa67a9c9814f   3 years ago     368MB

docker ps

CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
bee25e97e0b2   myimage      "docker-php-entrypoi…"   3 minutes ago    Up 3 minutes    0.0.0.0:81->80/tcp, :::81->80/tcp                      mycontainer
ab550bebc385   phpmyadmin   "/docker-entrypoint.…"   43 minutes ago   Up 43 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp                  phpmyadmin
092d09505f72   mysql        "docker-entrypoint.s…"   49 minutes ago   Up 49 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   basic-mysql

the commands i used for creating the containers:

for mysql-db:

docker run --name basic-mysql --rm -v /tmp/mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=ANSKk08aPEDbFjDO -e MYSQL_DATABASE=testing -p 3306:3306 -it mysql

for phpmyadmin:

docker run --name phpmyadmin -d --link basic-mysql:db -p 8080:80 phpmyadmin

this is my dockerfile for the php based container:

FROM php:7.0-apache
COPY * /var/www/html/
EXPOSE 80

docker command for creating php container

docker run -itd --name mycontainer --link basic-mysql:db -p 81:80 myimage

please suggest me about the docker-compose file based on this stack.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • `php:7.0-apache` is horribly outdated. Also, did you check whether the mysqli extension is enabled within that container? – Nico Haase Sep 25 '22 at 12:08

1 Answers1

0

your dockerfile is the responsable of defining your system (container) where your app will run in this system (as in your localhost) you should install some php extensions to work with a part of these extensions is mysqli so to install this in your system you should add this in your dockerfile

RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli


islemdev
  • 186
  • 1
  • 8
  • i added this to my dockerfile on the third step and build the dockerimage according and ran a container with the image but it still gives me the following error. ----------------------------------------------------------------------------------------- Warning: mysqli::__construct(): (HY000/2002): No such file or directory in /var/www/html/index.php on line 35 Connection failed: – Ashish Lekhak Sep 25 '22 at 11:12
  • @AshishLekhak, have you created the database? – Nigel Ren Sep 25 '22 at 12:32