admin管理员组

文章数量:1288987

I’m trying to set up a Docker-based environment for my PHP 7.4.33 application, but I’m unable to install the mysqli and pdo_mysql extensions. Despite following several tutorials and rebuilding the image multiple times, the extensions are not being installed.

Here’s my setup:

Dockerfile

FROM php:7.4.33-apache

# Install necessary extensions
RUN apt-get update && apt-get install -y \
    libmariadb-dev \
    && docker-php-ext-install mysqli pdo pdo_mysql

WORKDIR /var/www/html

docker-compose.yml

version: '3.8'

services:
  apache-php:
    build:
      context: /home/jasogu/docker/lamp-stack
      dockerfile: Dockerfile
    container_name: apache-php
    ports:
      - "8080:80"
    volumes:
      - ./www:/var/www/html
      - ./php-config:/usr/local/etc/php
    networks:
      - lamp-network
    depends_on:
      - mysql

  mysql:
    image: mysql:8.0
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: "password"
      MYSQL_DATABASE: "database"
      MYSQL_USER: "user"
      MYSQL_PASSWORD: "password"
    volumes:
      - ./mysql-data:/var/lib/mysql
    networks:
      - lamp-network

  phpmyadmin:
    image: phpmyadmin:latest
    container_name: phpmyadmin
    environment:
      PMA_HOST: mysql
      PMA_USER: "user"
      PMA_PASSWORD: "password"
    ports:
      - "8081:80"
    networks:
      - lamp-network
    depends_on:
      - mysql

volumes:
  mysql-data:

networks:
  lamp-network:

Steps I followed

  1. Ran docker-compose down --volumes to stop and remove containers.
  2. Used docker-compose up -d --build to rebuild the image with the Dockerfile.
  3. Accessed the container using docker exec -it apache-php bash and checked for the extensions with php -m.

What I see: When I run php -m | grep mysqli or php -m | grep pdo_mysql, there’s no output, meaning the extensions are not installed.

What I tried:

  • Rewriting the Dockerfile to ensure dependencies are correctly installed.
  • Adding phpinfo() to check PHP configuration (no mysqli or pdo_mysql in the output).
  • Cleaning up old images using docker image prune -f and rebuilding everything from scratch.

Error logs: When checking the logs, I don’t see any explicit errors about missing extensions or failed installations.

Question How can I successfully install the mysqli and pdo_mysql extensions in my PHP 7.4.33 container? Is there anything wrong with my Dockerfile or docker-compose.yml? Any help would be greatly appreciated!

I’m trying to set up a Docker-based environment for my PHP 7.4.33 application, but I’m unable to install the mysqli and pdo_mysql extensions. Despite following several tutorials and rebuilding the image multiple times, the extensions are not being installed.

Here’s my setup:

Dockerfile

FROM php:7.4.33-apache

# Install necessary extensions
RUN apt-get update && apt-get install -y \
    libmariadb-dev \
    && docker-php-ext-install mysqli pdo pdo_mysql

WORKDIR /var/www/html

docker-compose.yml

version: '3.8'

services:
  apache-php:
    build:
      context: /home/jasogu/docker/lamp-stack
      dockerfile: Dockerfile
    container_name: apache-php
    ports:
      - "8080:80"
    volumes:
      - ./www:/var/www/html
      - ./php-config:/usr/local/etc/php
    networks:
      - lamp-network
    depends_on:
      - mysql

  mysql:
    image: mysql:8.0
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: "password"
      MYSQL_DATABASE: "database"
      MYSQL_USER: "user"
      MYSQL_PASSWORD: "password"
    volumes:
      - ./mysql-data:/var/lib/mysql
    networks:
      - lamp-network

  phpmyadmin:
    image: phpmyadmin:latest
    container_name: phpmyadmin
    environment:
      PMA_HOST: mysql
      PMA_USER: "user"
      PMA_PASSWORD: "password"
    ports:
      - "8081:80"
    networks:
      - lamp-network
    depends_on:
      - mysql

volumes:
  mysql-data:

networks:
  lamp-network:

Steps I followed

  1. Ran docker-compose down --volumes to stop and remove containers.
  2. Used docker-compose up -d --build to rebuild the image with the Dockerfile.
  3. Accessed the container using docker exec -it apache-php bash and checked for the extensions with php -m.

What I see: When I run php -m | grep mysqli or php -m | grep pdo_mysql, there’s no output, meaning the extensions are not installed.

What I tried:

  • Rewriting the Dockerfile to ensure dependencies are correctly installed.
  • Adding phpinfo() to check PHP configuration (no mysqli or pdo_mysql in the output).
  • Cleaning up old images using docker image prune -f and rebuilding everything from scratch.

Error logs: When checking the logs, I don’t see any explicit errors about missing extensions or failed installations.

Question How can I successfully install the mysqli and pdo_mysql extensions in my PHP 7.4.33 container? Is there anything wrong with my Dockerfile or docker-compose.yml? Any help would be greatly appreciated!

Share Improve this question asked Jan 23 at 20:05 JasoguJasogu 1
Add a comment  | 

1 Answer 1

Reset to default 0

You also have to enable the extensions. Try something like this in your Dockerfile and rebuild the image:

FROM php:7.4.33-apache

# Install necessary extensions
RUN apt-get update && apt-get install -y \
    libmariadb-dev \
    && docker-php-ext-install mysqli pdo pdo_mysql \
    && docker-php-ext-enable mysqli pdo pdo_mysql  # add this line

WORKDIR /var/www/html

本文标签: Cannot install mysqli or pdomysql extensions in PHP 7433 Docker containerStack Overflow