38

I have a basic Dockerfile with the following in:

FROM php:7.1-apache
RUN apt-get update && docker-php-ext-install pdo_mysql
COPY . /var/www
EXPOSE 80

I have a docker-compose.yml file

version: "3"
services:
  app:
    build: .
    ports:
      - "80:80"
    volumes:
      - .:/var/www
    depends_on:
      - mysql
  mysql:
    image: mysql:8
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: "app"
      MYSQL_USER: "app"
      MYSQL_PASSWORD: "app"
      MYSQL_ROOT_PASSWORD: "test"

I then ran docker build -t app . && docker-compose up at the root of the project. Everything seems to build correctly, but when outputting phpinfo I don't see the mysql_pdo extension.

enter image description here

Are there any steps I am missing?

1
  • did you ever get this sorted? Dec 7, 2017 at 9:34

4 Answers 4

52

The docker file I use is...

FROM php:7.1-apache
COPY apache2.conf /etc/apache2
RUN docker-php-ext-install mysqli pdo pdo_mysql

Note the mysqli and pdo in there as well to allow the PDO/mysql bit.

5
  • 1
    I still get the same issue with the extra modules included
    – Tom
    Jun 18, 2017 at 8:45
  • @Tom have you removed the container and started from scratch, this is only built on the first startup of the container.
    – Nigel Ren
    Jun 18, 2017 at 13:35
  • Yeah I think so. I stopped the active containers then ran docker rm $(docker ps -a -q)
    – Tom
    Jun 19, 2017 at 16:49
  • Sorry - the config is based on the image it builds from the base image. So use docker images to find the image for php (mine created 'docker_php') and then use docker rmi IMAGEID (replacing it with the one in your listing). Then restart docker and this should rebuild the image with the new options.
    – Nigel Ren
    Jun 19, 2017 at 18:27
  • @NigelRen I tried it with docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q) && docker rmi -f $(docker images -q) command and yet, still have the problem described above. What's interesting is the fact that mysql works, pdo_mysql not. Jan 8, 2018 at 9:24
21

to enabled PHP PDO and mysqli extensions to connect with MySQL add in Dockerfile :

RUN docker-php-ext-install pdo pdo_mysql
# for mysqli if you want
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

More Information to how to create that visit this Page

1
6

There was a similar error in docker php issue 62:

As it turn out that I need to remove the old images and rebuild them again.

Cause I downloaded the image, then edit it. So I need to remove the old image and rebuild it in order to apply the change.

docker rm only removes containers. Make sure you remove your image as well before.

Also check if a Dockerfile like this one might be more robust/complete:

# PHP extensions
RUN \
    docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd \
    && docker-php-ext-configure mysqli --with-mysqli=mysqlnd \
    && docker-php-ext-install pdo_mysql \
    ...
1
-1

I am using this and works fine for me.

RUN apt-get update \
 && apt-get install -y git zlib1g-dev \
 && docker-php-ext-install pdo pdo_mysql zip

If its not working for your. Please attache docker build output for review.

Not the answer you're looking for? Browse other questions tagged or ask your own question.