Install pdo_mysql library using WordPress image in Docker

If you work with the image of Worpress for Docker and need to do something “out of the box” like connecting to the database that uses the WordPress image from a script that is not part of the WordPress theme it will not be possible because you need to install the MySQL pdo library in WordPress container.

To solve this we only need only two commands:

  • A command to install the pdo library that we need
  • A command to restart the WordPress container to load the new installed library

Install pdo library in WordPress container:

docker exec -it container_wordpress_1 docker-php-ext-install pdo_mysql

Restart Apache in WordPress container

docker exec -it container_wordpress_1 /etc/init.d/apache2 reload

Ready, your custom script can now connect to the same database used by the WordPress container.