Testing MySQL Connectivity From WordPress Docker Containers
Moving forward with feature development for the Store Locator Plus® SaaS service includes moving forward with our tool kits and environments. Docker containers have become a bigger part of our development toolkit. They make for rapid deployment of isolated environments for testing various concepts without the overhead of virtual machines. While we have been working on Virtualbox virtualized machines for years, Docker is far faster to spin up and far less resource intensive.
Along the way we have found the need to perform deeper dives into the interplay of Docker, WordPress, and MySQL. The latest endeavor, discovering why our WordPress container is not communicating with our AWS hosted Aurora MySQL test data set.
While we can connect to our Aurora MySQL database using the credentials is our docker-compose YAML file, the WordPress instance running in docker on localhost is throwing connectivity errors.
Assumptions
For this article we assume a rudimentary knowledge of Docker, a baseline container with WordPress installed — likely from the default WordPress image, and a GUI interface such as Docker Desktop for managing instances and providing quick access to the shell command line of the container.
We also assume you have a docker-compose.yaml config file that is used to startup the container that looks similar to this:
version: '3'
services:
wp:
image: wordpress:6.0.1-php7.4-apache
container_name: wp-slpsaas
platform: linux/amd64
ports:
- '80:80'
environment:
WORDPRESS_DB_HOST: 'some_rds_instance.c0glwpjjxt7q.us-east-1.rds.amazonaws.com'
WORDPRESS_DB_USER: wp_db_user
WORDPRESS_DB_PASSWORD: '9aDDwTdJenny8675309'
WORDPRESS_DB_NAME: wp_db_name
And you’ve started the container either via Docker Desktop or the command line withdocker-compose up -d
Installing MySQL Client On Docker WordPress Containers
Connect to the shell for the container after the container has been started. The easiest way to do this is with Docker Desktop and clicking the shell icon for the instance.
By default the WordPress Docker image does not contain very many tools. That is to be expected as the whole idea behind the containers is lightweight environments. As such we’ll need to install the MySQL client with apt:
apt-get update
apt-get install default-mysql-client
Checking Connectivity To The WordPress Database
With the MySQL client installed we can now test connectivity from within the container to our AWS database directly. This ensures our network configuration and docker-compose environment variables are set properly to communicate with the database.
mysql -A -h $WORDPRESS_DB_HOST -u $WORDPRESS_DB_USER --password=$WORDPRESS_DB_PASSWORD $WORDPRESS_DB_NAME
MySQL > show tables;
If you get a list of WordPress tables the connectivity is working.