0

I've been stuck on this problem for the last four hours and I've tried everything I could find on stackoverflow but nothing seems to work.

when visiting the appache server in my container I get greeted with an error: driver not found

Error on index.php

I supsect this is an issue with PDO since the tut I followed used sqlite

In my Dockerfile i did add the PDO extension but to no result?

This is my docker-compose.yml

services:
    php-apache-environment:
        container_name: php-apache
        build:
            context: .
            dockerfile: Dockerfile
        depends_on:
            - db
        volumes:
            - ./src:/var/www/html/
        ports:
            - 8000:80
    db:
        container_name: db
        image: mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: bitchan
            MYSQL_USER: bit_academy
            MYSQL_PASSWORD: bit_academy
        ports:
            - "9906:3306"
    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        ports:
            - '8080:80'
        restart: always
        environment:
            PMA_HOST: db
        depends_on:
            - db

and this is my Dockerfile

FROM php:8.0-apache
RUN apt-get update && apt-get upgrade -y
RUN docker-php-ext-install pdo pdo_mysql  && docker-php-ext-enable pdo pdo_mysql
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

How I connect to the DB on my index.php:


$conn = (require "./db/connectdb.php")();

try {
    $stmt = $conn->prepare("SELECT * FROM boards");
    $stmt->execute();
    $boards = $stmt->fetchAll();
    $stmt = $conn->prepare("SELECT posts.*, b.board_name, b.shorthand FROM posts JOIN boards b on b.id = posts.board_id ORDER BY modified_at DESC LIMIT 5;");
    $stmt->execute();
    $posts = $stmt->fetchAll();
} catch (PDOException $e) {
    echo $e->getMessage();
}

and lastely my connectdb.php:

<?php
return function () {
    $servername = "db:3306";
    $username = "bit_academy";
    $password = "bit_academy";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=bitchan", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
    return $conn;
}
?> 

Also my project dir:

view of project directory

If anyone knows what I'm doing wrong you'd make my day! thanks in advance!

4
  • 1
    driver not found means pdo_mysql is not installed. pdo_mysql is not installed means RUN docker-php-ext-install pdo pdo_mysql didn't run. Commented Apr 8, 2022 at 13:04
  • just adding a command to your dockerfile is not enough. you need to rebuild container. Commented Apr 8, 2022 at 13:06
  • @YourCommonSense I did indeed rebuild the container, But sadly that hasn't worked for me yet. I also don't quite know why it's not installed do I need to change other settings aswell? View of php info PDO section Commented Apr 8, 2022 at 13:21
  • @Artemixed I think it should be just db $servername = "db:3306"; Commented Apr 8, 2022 at 13:46

1 Answer 1

1

In my case, installing php8-pdo_mysql instead of php-pdo_mysql module was enough.

I'm using alpine image, so I added following line to my Dockerfile:

RUN apk add php8-pdo_myql@php
Sign up to request clarification or add additional context in comments.

Comments

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.