Adding PDO pdo_mysql Extension in Apache + PHP Docker Container

Tiempo de lectura: 2 minutos

Reading time: 2 minutes

Today I’m going to show you how to install the PDO pdo_mysql extension to use Codeigniter or RedBeans, etc…

We have the following docker-compose.yml file:

version: "3.1"

services:
  miservicio_mariadb:
    image: mariadb
    container_name: mariadb_container
    env_file:
      - ./Dockerfile/mysql.env
    environment:
      MYSQL_DATABASE: "db_prueba"
      MYSQL_USER: "user_prueba"
      MYSQL_PASSWORD: "pass_mysql"
      MYSQL_ROOT_PASSWORD: "contra@prueba"
    volumes:
      - ./config/mariadb:/var/lib/mysql
    expose:
      - 3306
    ports:
      - 3306:3306
    networks:
      - docker-network
  phpmyadmin:
    image: phpmyadmin
    restart: always
    container_name: phpmyadmin_container
    ports:
      - 8080:80
    environment:
      - PMA_ARBITRARY=1
    links: 
      - miservicio_mariadb:db
    networks:
      - docker-network
  miservicio_php:
    build: 
      context: ./Dockerfile
      dockerfile: php_file
    env_file:
       - ./Dockerfile/php.env
    container_name: php_container
    volumes:
      - ./www:/var/www/html
      - ./logs/apache:/var/log/apache2
      - ./config/letsencrypt/certs:/etc/apache2/ssl/
      - ./config/web/logs:/var/www/logs
    expose:
      - 80
      - 443
    ports:
      - 8000:80
      - 442:443
    links: 
      - miservicio_mariadb
    networks:
      - docker-network
networks:
  docker-network:
    driver: bridge
    external: true

And the php_container file is created from this dockerfile:

# syntax=docker/dockerfile:1
FROM php:7.0-apache
RUN docker-php-ext-install mysqli

#Activate Apache modules:
RUN a2enmod headers
RUN a2enmod rewrite
RUN a2enmod ssl

RUN /etc/init.d/apache2 restart

If we want to install the pdo_mysql module, we have to add the following:

RUN docker-php-ext-install pdo_mysql

Resulting in the following dockerfile:

# syntax=docker/dockerfile:1
FROM php:7.0-apache
RUN docker-php-ext-install mysqli

RUN docker-php-ext-install pdo_mysql

#Activate Apache modules:
RUN a2enmod headers
RUN a2enmod rewrite
RUN a2enmod ssl

RUN /etc/init.d/apache2 restart

And when we run phpinfo(); the installed module appears:

Leave a Comment