Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ScottTrakker's avatar

Laravel in Docker: Class "PDO" not found

Hello developers,

I try to run Laravel with Docker but when I run php artisan migrate I get the error Class "PDO" not found.

This is docker-compose file that I use:

version: 3.4
services:
  php-apache-environment:
    container_name: php-apache
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - db
    volumes:
      - ./www:/var/www/html:Z
    ports:
      - 8000:80
  db:
    container_name: db
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: zqw36vuahnrk
      MYSQL_DATABASE: laravel-mvc
      MYSQL_USER: laravel-user
      MYSQL_PASSWORD: dw3kngbtd773
    ports:
      - 9906:3306
    volumes:
      - dbdata:/var/lib/mysql
  phpmyadmin:
     image: phpmyadmin/phpmyadmin
     ports:
       - 8080:80
     restart: always
     environment:
       PMA_HOST: db
     depends_on:
       - db

With this Dockerfile

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

I run the command php artisan migrate on the host. When I run php artisan migrate inside the container I get the error Illuminate\Database\QueryException SQLSTATE[HY000] [2002] No such file or directory.

Do somebody have an idea what I could do wrong / what I can do?

0 likes
7 replies
MohamedTammam's avatar

Check if you have the vendor directory in the project directory inside the container to see if your correctly mapped your volumes.

1 like
ScottTrakker's avatar

@MohamedTammam,

Thanks for thinking along! I check the directory vendor from inside the container and I can view all the files and directories properly.

This is the database section in .env:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_mvc
DB_USERNAME=laravel-user
DB_PASSWORD=dw3kngbtd773

I'm clueless at the moment.

ScottTrakker's avatar

@MohamedTammam,

Oh damn, you are right!

Sadly, after changing the name and restarting everything, the error Class "PDO" not found persists :-(.

ScottTrakker's avatar

@MohamedTammam

The result of php -m in the container php-apache is:

[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
sodium
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

I was thinking: the reason why it doesn't work outside of the container, is that PDO is not installed on the host (and I also don't want to). The good way to run php artisan migrate is properly only from inside the container. But when I do that I get the error below.

Illuminate\Database\QueryException 

SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = laravel-mvc and table_name = migrations and table_type = 'BASE TABLE')

Maybe this error message is more important than the class not found error?

Thanks for helping me so far!

ScottTrakker's avatar

I solved it!!!

I changed DB_HOST=localhost to DB_HOST=db and then ran php artisan migrate from inside the container and it worked!!!

Thanks for being with me :-) !

Please or to participate in this conversation.