37

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?

Tom
  • 950
  • 3
  • 15
  • 27

4 Answers4

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.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • 1
    I still get the same issue with the extra modules included – Tom Jun 18 '17 at 08: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 '17 at 13:35
  • Yeah I think so. I stopped the active containers then ran `docker rm $(docker ps -a -q)` – Tom Jun 19 '17 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 '17 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. – Tomasz Kapłoński Jan 08 '18 at 09:24
20

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

Ahmad Al ALloush
  • 330
  • 3
  • 10
  • **Confirmed Working** To further add to @Ahmad Al ALloush answer, [php - Official Image | Docker - How to install more PHP extensions](https://hub.docker.com/_/php). PHP extensions need to be both installed and enabled via the `docker-php-ext-install` and `docker-php-ext-enable` functions. – safesploit Jan 08 '23 at 12:55
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 \
    ...
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-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.

Virendra Jadeja
  • 821
  • 1
  • 10
  • 20