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

marianogomez's avatar

Problem with mysql after installing GD library in docker

What's up guys? me again. I'm working on a project that i started with this code:

#docker-compose.yml
version: '3.8'

services:
    nginx:
        build:
            context: .
            dockerfile: nginx.dockerfile
        ports:
            - 80:80
        volumes:
            - ./src:/var/www/html
        depends_on:
            - mysql
            - php
    mysql:
        image: mysql:5.7.41
        ports:
            - 3306:3306
        environment:
            MYSQL_DATABASE: laravel
            MYSQL_USER: laravel
            MYSQL_PASSWORD: secret
            MYSQL_ROOT_PASSWORD: secret
        volumes:
            - ./mysql:/var/lib/mysql
    php:
        build:
            context: .
            dockerfile: php.dockerfile
        volumes:
            - ./src:/var/www/html
        depends_on:
            - mysql
#php.dockerfile
FROM php:8-fpm-alpine

ENV PHPGROUP=laravel
ENV PHPUSER=laravel

RUN adduser -g ${PHPGROUP} -s /bin/sh -D ${PHPUSER}

RUN sed -i "s/user = www-data/user = ${PHPUSER}/g" /usr/local/etc/php-fpm.d/www.conf
RUN sed -i "s/group = www-data/group = ${PHPGROUP}/g" /usr/local/etc/php-fpm.d/www.conf

RUN mkdir -p /var/www/html/public

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]
#nginx.dockerfile
FROM nginx:stable-alpine

ENV NGINXUSER=laravel
ENV NGINXGROUP=laravel

RUN mkdir -p /var/www/html/public

ADD nginx/default.conf /etc/nginx/conf.d/default.conf

RUN sed -i "s/user www-data/user ${NGINXUSER}/g" /etc/nginx/nginx.conf

RUN adduser -g ${NGINXGROUP} -s /bin/sh -D ${NGINXUSER}

It has been working well. Thing is, i installed GD library adding the following code to the php.dockerfile file:

RUN apk add --no-cache freetype libpng libjpeg-turbo freetype-dev libpng-dev libjpeg-turbo-dev && \
  docker-php-ext-configure gd && \
  NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) && \
  docker-php-ext-install -j${NPROC} gd && \
  apk del --no-cache freetype-dev libpng-dev libjpeg-turbo-dev

It ran ok, i checked with phpinfo() that the library was installed, which it did, but now, all of the sudden, i'm having issues with the DB, i get this error:

could not find driver (Connection: mysql, SQL: select * from `games` order by `title` asc)

I've tried deleting both the nginx and php container, to re-build them using docker-compose up --build (after having removed the GD related lines from the php.dockerfile file), but the problem remains. I've also stashed all my changes in the repo and re-executed composer install, but the problem remains. I can access to the DB with the same credentials i had from before, via MariaDB Does anyone might know what could be happening?

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue might be related to the fact that the GD library installation process might have overwritten the PHP configuration file. Try adding the following line to the php.dockerfile file to ensure that the MySQL driver is enabled:

RUN docker-php-ext-install pdo_mysql

This will install the PDO MySQL driver, which is required for Laravel to connect to the MySQL database. After adding this line, rebuild the containers using docker-compose up --build and check if the issue is resolved.

1 like
marianogomez's avatar

i removed the RUN docker-php-ext-install pdo pdo_mysql when i pasted the new code (facepalm) thanks!!!

Please or to participate in this conversation.